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

35 lines
717 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
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()
redirectToSSO()
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,
}
}