Skip to content

Commit 782198b

Browse files
committed
feat: auth仓库不在做发出网络请求 只存储信息 用hooks的方式完成登录逻辑
1 parent a882490 commit 782198b

File tree

3 files changed

+45
-63
lines changed

3 files changed

+45
-63
lines changed

src/features/auth/auth.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { useLoading } from '@sa/hooks';
22

33
import { getIsLogin, selectUserInfo } from '@/features/auth/authStore';
44
import { useRoute, useRouter } from '@/features/router';
5+
import { fetchGetUserInfo, fetchLogin } from '@/service/api';
6+
import { localStg } from '@/utils/storage';
57

68
import { useCacheTabs } from '../tab/tabHooks';
79

8-
import { login, resetAuth as resetAuthAction } from './authStore';
10+
import { resetAuth as resetAuthAction, setToken, setUserInfo } from './authStore';
911
import { clearAuthStorage } from './shared';
1012

1113
export function useAuth() {
@@ -43,27 +45,38 @@ export function useInitAuth() {
4345

4446
const redirectUrl = searchParams.get('redirect');
4547

46-
async function toLogin(params: { password: string; userName: string }, redirect = true) {
48+
async function toLogin({ password, userName }: { password: string; userName: string }, redirect = true) {
4749
if (loading) return;
4850

4951
startLoading();
50-
const res = await dispatch(login(params));
52+
const { data: loginToken, error } = await fetchLogin(userName, password);
5153

52-
const info = res.payload as Api.Auth.Info;
54+
if (!error) {
55+
localStg.set('token', loginToken.token);
56+
localStg.set('refreshToken', loginToken.refreshToken);
5357

54-
if (info.token) {
55-
if (redirect) {
56-
if (redirectUrl) {
57-
navigate(redirectUrl);
58-
} else {
59-
navigate('/');
58+
const { data: info, error: userInfoError } = await fetchGetUserInfo();
59+
60+
if (!userInfoError) {
61+
// 2. store user info
62+
localStg.set('userInfo', info);
63+
64+
dispatch(setToken(loginToken.token));
65+
dispatch(setUserInfo(info));
66+
67+
if (redirect) {
68+
if (redirectUrl) {
69+
navigate(redirectUrl);
70+
} else {
71+
navigate('/');
72+
}
6073
}
61-
}
6274

63-
window.$notification?.success({
64-
description: t('page.login.common.welcomeBack', { userName: info.userInfo.userName }),
65-
message: t('page.login.common.loginSuccess')
66-
});
75+
window.$notification?.success({
76+
description: t('page.login.common.welcomeBack', { userName: info.userName }),
77+
message: t('page.login.common.loginSuccess')
78+
});
79+
}
6780
}
6881

6982
endLoading();
@@ -79,8 +92,8 @@ export function useResetAuth() {
7992
const dispatch = useAppDispatch();
8093

8194
const {
82-
fullPath,
83-
handle: { constant }
95+
handle: { constant },
96+
redirect
8497
} = useRoute();
8598

8699
const cacheTabs = useCacheTabs();
@@ -97,7 +110,7 @@ export function useResetAuth() {
97110
cacheTabs();
98111

99112
if (!constant) {
100-
push('/login', { redirect: fullPath });
113+
push('/login', { redirect: redirect?.fullPath });
101114
}
102115
}
103116

src/features/auth/authStore.ts

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { createSelector } from '@reduxjs/toolkit';
2-
3-
import { fetchGetUserInfo, fetchLogin } from '@/service/api';
4-
import { createAppSlice } from '@/store/createAppSlice';
5-
import { localStg } from '@/utils/storage';
1+
import type { PayloadAction } from '@reduxjs/toolkit';
2+
import { createSelector, createSlice } from '@reduxjs/toolkit';
63

74
import { getToken, getUserInfo } from './shared';
85

@@ -11,53 +8,27 @@ const initialState = {
118
userInfo: getUserInfo()
129
};
1310

14-
export const authSlice = createAppSlice({
11+
export const authSlice = createSlice({
1512
initialState,
1613
name: 'auth',
17-
reducers: create => ({
18-
login: create.asyncThunk(
19-
async ({ password, userName }: { password: string; userName: string }) => {
20-
const { data: loginToken, error } = await fetchLogin(userName, password);
21-
// 1. stored in the localStorage, the later requests need it in headers
22-
if (!error) {
23-
localStg.set('token', loginToken.token);
24-
localStg.set('refreshToken', loginToken.refreshToken);
25-
26-
const { data: info, error: userInfoError } = await fetchGetUserInfo();
27-
28-
if (!userInfoError) {
29-
// 2. store user info
30-
localStg.set('userInfo', info);
31-
return {
32-
token: loginToken.token,
33-
userInfo: info
34-
};
35-
}
36-
}
37-
38-
return initialState;
39-
},
40-
41-
{
42-
fulfilled: (state, { payload }) => {
43-
if (payload) {
44-
state.token = payload.token;
45-
state.userInfo = payload.userInfo;
46-
}
47-
}
48-
}
49-
),
50-
resetAuth: create.reducer(() => initialState)
51-
}),
14+
reducers: {
15+
resetAuth: () => initialState,
16+
setToken: (state, { payload }: PayloadAction<string>) => {
17+
state.token = payload;
18+
},
19+
setUserInfo: (state, { payload }: PayloadAction<Api.Auth.UserInfo>) => {
20+
state.userInfo = payload;
21+
}
22+
},
5223
selectors: {
5324
selectToken: auth => auth.token,
5425
selectUserInfo: auth => auth.userInfo
5526
}
5627
});
5728

58-
export const { selectToken, selectUserInfo } = authSlice.selectors;
29+
export const { resetAuth, setToken, setUserInfo } = authSlice.actions;
5930

60-
export const { login, resetAuth } = authSlice.actions;
31+
export const { selectToken, selectUserInfo } = authSlice.selectors;
6132

6233
/** Is login */
6334
export const getIsLogin = createSelector([selectToken], token => Boolean(token));

src/features/router/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
export * from './router';
22

3-
export * from './router-context';
4-
53
export * from './RouterProvider';
64

75
export * from './useRoute';

0 commit comments

Comments
 (0)