feat: 重构用户认证逻辑,添加导航重定向功能,更新相关组件和路由
This commit is contained in:
@@ -2,24 +2,41 @@ import type { UserData, UserProfileData } from "@/api/types";
|
||||
import { client, safeClient } from "@/api";
|
||||
|
||||
interface State {
|
||||
user: UserData | null;
|
||||
userProfile: UserProfileData | null;
|
||||
session: UserData | null;
|
||||
}
|
||||
|
||||
export const useUserStore = defineStore("user", () => {
|
||||
const token = useStorage<string | null>("user-token", null);
|
||||
const state = reactive<State>({
|
||||
user: null,
|
||||
userProfile: null,
|
||||
session: null,
|
||||
});
|
||||
|
||||
const isAuthenticated = computed(() => token.value !== null);
|
||||
|
||||
async function updateProfile() {
|
||||
const { data } = await safeClient(client.api.user.profile.get(), { silent: true });
|
||||
state.userProfile = data.value?.userProfile || null;
|
||||
state.session = data.value?.user || null;
|
||||
state.user = data.value?.user || null;
|
||||
}
|
||||
|
||||
function setToken(value: string) {
|
||||
token.value = value;
|
||||
}
|
||||
|
||||
function signOut() {
|
||||
token.value = null;
|
||||
state.userProfile = null;
|
||||
state.user = null;
|
||||
}
|
||||
|
||||
return {
|
||||
state,
|
||||
...toRefs(state),
|
||||
token,
|
||||
isAuthenticated,
|
||||
signOut,
|
||||
setToken,
|
||||
updateProfile,
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user