@@ -2,10 +2,12 @@ import { useLoading } from '@sa/hooks';
2
2
3
3
import { getIsLogin , selectUserInfo } from '@/features/auth/authStore' ;
4
4
import { useRoute , useRouter } from '@/features/router' ;
5
+ import { fetchGetUserInfo , fetchLogin } from '@/service/api' ;
6
+ import { localStg } from '@/utils/storage' ;
5
7
6
8
import { useCacheTabs } from '../tab/tabHooks' ;
7
9
8
- import { login , resetAuth as resetAuthAction } from './authStore' ;
10
+ import { resetAuth as resetAuthAction , setToken , setUserInfo } from './authStore' ;
9
11
import { clearAuthStorage } from './shared' ;
10
12
11
13
export function useAuth ( ) {
@@ -43,27 +45,38 @@ export function useInitAuth() {
43
45
44
46
const redirectUrl = searchParams . get ( 'redirect' ) ;
45
47
46
- async function toLogin ( params : { password : string ; userName : string } , redirect = true ) {
48
+ async function toLogin ( { password , userName } : { password : string ; userName : string } , redirect = true ) {
47
49
if ( loading ) return ;
48
50
49
51
startLoading ( ) ;
50
- const res = await dispatch ( login ( params ) ) ;
52
+ const { data : loginToken , error } = await fetchLogin ( userName , password ) ;
51
53
52
- const info = res . payload as Api . Auth . Info ;
54
+ if ( ! error ) {
55
+ localStg . set ( 'token' , loginToken . token ) ;
56
+ localStg . set ( 'refreshToken' , loginToken . refreshToken ) ;
53
57
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
+ }
60
73
}
61
- }
62
74
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
+ }
67
80
}
68
81
69
82
endLoading ( ) ;
@@ -79,8 +92,8 @@ export function useResetAuth() {
79
92
const dispatch = useAppDispatch ( ) ;
80
93
81
94
const {
82
- fullPath ,
83
- handle : { constant }
95
+ handle : { constant } ,
96
+ redirect
84
97
} = useRoute ( ) ;
85
98
86
99
const cacheTabs = useCacheTabs ( ) ;
@@ -97,7 +110,7 @@ export function useResetAuth() {
97
110
cacheTabs ( ) ;
98
111
99
112
if ( ! constant ) {
100
- push ( '/login' , { redirect : fullPath } ) ;
113
+ push ( '/login' , { redirect : redirect ?. fullPath } ) ;
101
114
}
102
115
}
103
116
0 commit comments