useAuth.js
1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { ref } from 'vue'
import axios from 'axios'
const user = ref(null)
const isChecked = ref(false) // 是否已经问过一次 /api/auth/me,避免路由守卫重复请求
let checkPromise = null
export function useAuth() {
/** 查一次当前登录态,结果缓存到下次显式 refresh。未登录返回 null,不算错误。 */
async function checkAuth({ force = false } = {}) {
if (isChecked.value && !force) return user.value
if (checkPromise && !force) return checkPromise
checkPromise = axios.get('/api/auth/me')
.then(({ data }) => {
user.value = data.user || null
isChecked.value = true
return user.value
})
.catch(() => {
user.value = null
isChecked.value = true
return null
})
.finally(() => { checkPromise = null })
return checkPromise
}
async function login(email, password) {
const { data } = await axios.post('/api/auth/login', { email, password })
user.value = data.user
isChecked.value = true
return data.user
}
async function logout() {
try { await axios.post('/api/auth/logout') } catch { /* cookie 清不掉也要清本地状态 */ }
user.value = null
isChecked.value = true
}
/** 401 拦截器触发时调用:本地登录态失效,不用再打一次 /me 去确认 */
function clearAuth() {
user.value = null
isChecked.value = true
}
return { user, isChecked, checkAuth, login, logout, clearAuth }
}