feat: 重构用户认证逻辑,添加导航重定向功能,更新相关组件和路由
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
export function beforeApp() {
|
||||
const { updateProfile } = useUserStore();
|
||||
const { initializeWallet } = useWalletStore();
|
||||
|
||||
return new Promise<void>((resolve) => {
|
||||
useAuth().then((session) => {
|
||||
if (session) {
|
||||
updateProfile();
|
||||
initializeWallet();
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
import type { UnwrapRef } from "vue";
|
||||
import { safeClient } from "@/api";
|
||||
import { authClient } from "@/auth";
|
||||
|
||||
type User = UnwrapRef<ReturnType<typeof authClient.useSession> extends Promise<infer R extends { data: any }> ? R["data"] : never>;
|
||||
|
||||
export function useAuth() {
|
||||
const session = ref<User | null>(null);
|
||||
|
||||
if (session.value === undefined) {
|
||||
safeClient(authClient.getSession()).then((res) => {
|
||||
session.value = res.data.value;
|
||||
});
|
||||
}
|
||||
|
||||
const user = computed(() => session.value?.user);
|
||||
const isAuthenticated = computed(() => !!session.value);
|
||||
|
||||
return {
|
||||
user,
|
||||
session,
|
||||
isAuthenticated,
|
||||
then(onfulfilled: (value: User | null) => void) {
|
||||
return safeClient(authClient.getSession()).then((res) => {
|
||||
session.value = res.data.value;
|
||||
onfulfilled(session.value);
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
12
src/composables/useNavigateToRedirect.ts
Normal file
12
src/composables/useNavigateToRedirect.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { LocationQueryValue } from "vue-router";
|
||||
import { router } from "@/router";
|
||||
|
||||
export function useNavigateToRedirect(redirect: LocationQueryValue): void;
|
||||
|
||||
export function useNavigateToRedirect(redirect: LocationQueryValue[], index: number): void;
|
||||
|
||||
export function useNavigateToRedirect(redirect: LocationQueryValue | LocationQueryValue[], index?: number) {
|
||||
const _redirect = Array.isArray(redirect) ? redirect[index || 0] as string : redirect as string;
|
||||
const path = decodeURIComponent(_redirect || "/");
|
||||
router.replace(path);
|
||||
}
|
||||
Reference in New Issue
Block a user