feat(auth): implement user data fetching with react-query and refactor auth store
This commit is contained in:
1
src/pages/auth/hooks/index.ts
Normal file
1
src/pages/auth/hooks/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export * from './use-api-query';
|
||||||
55
src/pages/auth/hooks/use-api-query.ts
Normal file
55
src/pages/auth/hooks/use-api-query.ts
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||||
|
import { queryLogin } from '@/modules/query';
|
||||||
|
import { toast } from 'sonner';
|
||||||
|
import type { UserInfo } from '../store';
|
||||||
|
|
||||||
|
export const authQueryKeys = {
|
||||||
|
me: ['auth', 'me'] as const,
|
||||||
|
token: ['auth', 'token'] as const,
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
export const useMe = () => {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: authQueryKeys.me,
|
||||||
|
queryFn: async () => {
|
||||||
|
const res = await queryLogin.getMe();
|
||||||
|
if (res.code === 200) {
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
throw new Error(res.message || 'Failed to fetch user info');
|
||||||
|
},
|
||||||
|
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useSwitchOrg = () => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
return useMutation({
|
||||||
|
mutationFn: async (username?: string) => {
|
||||||
|
const res = await queryLogin.switchUser(username || '');
|
||||||
|
if (res.code === 200) {
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
throw new Error(res.message || 'Switch failed');
|
||||||
|
},
|
||||||
|
onSuccess: () => {
|
||||||
|
toast.success('切换成功');
|
||||||
|
queryClient.invalidateQueries({ queryKey: authQueryKeys.me });
|
||||||
|
setTimeout(() => {
|
||||||
|
window.location.reload();
|
||||||
|
}, 1000);
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
toast.error(error.message || '请求失败');
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useGetToken = () => {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: authQueryKeys.token,
|
||||||
|
queryFn: () => queryLogin.getToken(),
|
||||||
|
staleTime: Infinity,
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -5,6 +5,7 @@ import { LogIn, LockKeyhole } from "lucide-react"
|
|||||||
export { BaseHeader } from './modules/BaseHeader'
|
export { BaseHeader } from './modules/BaseHeader'
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { useLocation, useNavigate } from '@tanstack/react-router';
|
import { useLocation, useNavigate } from '@tanstack/react-router';
|
||||||
|
import { useMe } from "./hooks"
|
||||||
|
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
@@ -26,6 +27,8 @@ export const AuthProvider = ({ children, mustLogin }: Props) => {
|
|||||||
return store.openLinkList.some(item => location.pathname.startsWith(item))
|
return store.openLinkList.some(item => location.pathname.startsWith(item))
|
||||||
}, [location.pathname])
|
}, [location.pathname])
|
||||||
const loginUrl = '/root/login/?redirect=' + encodeURIComponent(window.location.href);
|
const loginUrl = '/root/login/?redirect=' + encodeURIComponent(window.location.href);
|
||||||
|
const me = useMe()
|
||||||
|
console.log('me', me, me.error);
|
||||||
if (mustLogin && !store.me && !isOpen) {
|
if (mustLogin && !store.me && !isOpen) {
|
||||||
return (
|
return (
|
||||||
<div className="w-full h-full min-h-screen flex items-center justify-center bg-background">
|
<div className="w-full h-full min-h-screen flex items-center justify-center bg-background">
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
|
|
||||||
import { queryLogin } from '@/modules/query';
|
import { queryLogin, stackQueryClient } from '@/modules/query';
|
||||||
import { create } from 'zustand';
|
import { create } from 'zustand';
|
||||||
import { toast } from 'sonner';
|
import { toast } from 'sonner';
|
||||||
type UserInfo = {
|
import { authQueryKeys } from './hooks';
|
||||||
|
export type UserInfo = {
|
||||||
id?: string;
|
id?: string;
|
||||||
username?: string;
|
username?: string;
|
||||||
nickname?: string | null;
|
nickname?: string | null;
|
||||||
@@ -58,16 +59,23 @@ export const useLayoutStore = create<LayoutStore>((set, get) => ({
|
|||||||
set({ me: undefined, isAdmin: false });
|
set({ me: undefined, isAdmin: false });
|
||||||
},
|
},
|
||||||
getMe: async () => {
|
getMe: async () => {
|
||||||
const res = await queryLogin.getMe();
|
const data = await stackQueryClient.fetchQuery({
|
||||||
if (res.code === 200) {
|
queryKey: authQueryKeys.me,
|
||||||
set({ me: res.data });
|
queryFn: async () => {
|
||||||
set({ isAdmin: res.data.orgs?.includes?.('admin') || false });
|
const res = await queryLogin.getMe();
|
||||||
}
|
if (res.code === 200) {
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
throw new Error(res.message || 'Failed to fetch user info');
|
||||||
|
},
|
||||||
|
});
|
||||||
|
set({ me: data, isAdmin: data?.orgs?.includes?.('admin') || false });
|
||||||
},
|
},
|
||||||
switchOrg: async (username?: string) => {
|
switchOrg: async (username?: string) => {
|
||||||
const res = await queryLogin.switchUser(username || '');
|
const res = await queryLogin.switchUser(username || '');
|
||||||
if (res.code === 200) {
|
if (res.code === 200) {
|
||||||
toast.success('切换成功');
|
toast.success('切换成功');
|
||||||
|
stackQueryClient.invalidateQueries({ queryKey: authQueryKeys.me });
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
}, 1000);
|
}, 1000);
|
||||||
@@ -80,14 +88,18 @@ export const useLayoutStore = create<LayoutStore>((set, get) => ({
|
|||||||
init: async () => {
|
init: async () => {
|
||||||
const token = await queryLogin.getToken();
|
const token = await queryLogin.getToken();
|
||||||
if (token) {
|
if (token) {
|
||||||
set({ me: {} })
|
set({ me: {} });
|
||||||
const me = await queryLogin.getMe();
|
try {
|
||||||
// const user = await queryLogin.checkLocalUser() as UserInfo;
|
const data = await stackQueryClient.fetchQuery({
|
||||||
const user = me.code === 200 ? me.data : undefined;
|
queryKey: authQueryKeys.me,
|
||||||
if (user) {
|
}) as UserInfo;
|
||||||
set({ me: user });
|
console.log('Fetched user info:', data);
|
||||||
set({ isAdmin: user.orgs?.includes?.('admin') || false });
|
if (data) {
|
||||||
} else {
|
set({ me: data, isAdmin: data.orgs?.includes?.('admin') || false });
|
||||||
|
} else {
|
||||||
|
set({ me: undefined, isAdmin: false });
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
set({ me: undefined, isAdmin: false });
|
set({ me: undefined, isAdmin: false });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user