Files
xinfra/frontend/src/composables/useAuth.ts
T

37 lines
830 B
TypeScript
Raw Normal View History

2026-07-14 15:39:19 +08:00
import { useAuthStore } from '@/stores/auth'
import { authApi } from '@/api/auth'
import { redirectToSSO } from '@/utils/sso'
2026-07-14 15:39:19 +08:00
2026-07-15 15:52:27 +08:00
const SSO_SIGNOUT_URL = 'https://bo-staging-sso-internal.jfcs-k8s-qa1.qiniu.io/signout'
2026-07-14 15:39:19 +08:00
export function useAuth() {
const authStore = useAuthStore()
const login = async (username: string, password: string) => {
const response = await authApi.login({ username, password })
const { token, user } = response.data
authStore.setAuth(token, user)
return response
}
const logout = async () => {
await authApi.logout()
authStore.clearAuth()
2026-07-15 15:52:27 +08:00
window.location.assign(SSO_SIGNOUT_URL)
2026-07-14 15:39:19 +08:00
}
const checkAuth = () => {
if (!authStore.isLoggedIn()) {
redirectToSSO()
2026-07-14 15:39:19 +08:00
return false
}
return true
}
return {
login,
logout,
checkAuth,
}
}