1
- import Axios , { AxiosResponse } from "axios" ;
1
+ import Axios , { AxiosResponse } from "axios" ;
2
2
3
- import { hostname , userPath } from "./api" ;
3
+ import { hostname , userPath } from "./api" ;
4
4
5
- import { UserState } from "../redux/actions/userTypes" ;
5
+ import { UserState } from "../redux/actions/userTypes" ;
6
6
import store from "../redux/store" ;
7
- import {
8
- addAccessToken ,
9
- addRefreshToken ,
10
- checkedCookies ,
11
- removeTokens
12
- } from "../redux/actions/tokens" ;
13
- import { AccessToken , CookieStatus } from "../redux/actions/tokenTypes" ;
14
- import { deleteCookie , getCookie , setCookie } from "../methods/cookies" ;
7
+ import { addAccessToken , addRefreshToken , checkedCookies , removeTokens } from "../redux/actions/tokens" ;
8
+ import { AccessToken , CookieStatus } from "../redux/actions/tokenTypes" ;
9
+ import { deleteCookie , getCookie , setCookie } from "../methods/cookies" ;
15
10
import { updateUser } from "../redux/actions/user" ;
16
11
import { hashPassword } from "../methods/passwords" ;
17
12
@@ -20,109 +15,110 @@ import {hashPassword} from "../methods/passwords";
20
15
const cookieName : string = "refreshToken" ;
21
16
22
17
export interface BackendLoginData {
23
- tokenValue : string ;
24
- user : UserState ;
18
+ tokenValue : string ;
19
+ user : UserState ;
25
20
}
26
21
27
22
export interface BackendAuthData {
28
- tokenValue : string ;
29
- userId : number ;
30
- validUntil : number ;
23
+ tokenValue : string ;
24
+ userId : number ;
25
+ validUntil : number ;
31
26
}
32
27
33
28
export const checkForCookie = ( ) => {
34
- let refreshTokenCookieValue = getCookie ( cookieName ) ;
35
- if ( refreshTokenCookieValue ) {
29
+ let refreshTokenCookieValue = getCookie ( cookieName ) ;
30
+ if ( ! refreshTokenCookieValue ) {
31
+ return store . dispatch ( checkedCookies ( CookieStatus . FINISHED ) ) ;
32
+ }
33
+
36
34
store . dispatch ( addRefreshToken ( refreshTokenCookieValue ) ) ;
37
35
store . dispatch ( checkedCookies ( CookieStatus . LOADING ) ) ;
38
36
getAccessTokenWithRefreshToken ( ) ;
39
- } else {
40
- store . dispatch ( checkedCookies ( CookieStatus . FINISHED ) ) ;
41
- }
37
+
42
38
} ;
43
39
44
- export const loginWithUsernameAndPassword = async (
45
- userName : string ,
46
- password : string ,
47
- stayLoggedIn : boolean
40
+ export const loginWithUsernameAndPassword = async (
41
+ userName : string ,
42
+ password : string ,
43
+ stayLoggedIn : boolean
48
44
) : Promise < BackendLoginData > => {
49
- console . log ( "[Auth] loginWithUsernameAndPassword" , userName ) ;
50
- let hashed = await hashPassword ( password ) ;
51
- return new Promise < BackendLoginData > ( ( resolve , reject ) => {
52
- let config = {
53
- headers : {
54
- Authorization : `Basic ${ btoa ( userName + ":" + hashed ) } `
55
- }
56
- } ;
57
-
58
- return Axios . get < BackendLoginData > ( hostname + userPath + "/login" , config )
59
- . then ( ( data : AxiosResponse < BackendLoginData > ) => {
60
- console . log ( data . data ) ;
61
- store . dispatch ( addRefreshToken ( data . data . tokenValue ) ) ;
62
- store . dispatch ( updateUser ( data . data . user as UserState ) ) ;
63
-
64
- if ( stayLoggedIn ) {
65
- setCookie ( cookieName , data . data . tokenValue , 60 ) ;
66
- }
67
-
68
- getAccessTokenWithRefreshToken ( ) ;
69
- } )
70
- . catch ( ( error ) => {
71
- reject ( error ) ;
72
- } ) ;
73
- } ) ;
45
+ console . log ( "[Auth] loginWithUsernameAndPassword" , userName ) ;
46
+ let hashed = await hashPassword ( password ) ;
47
+ return new Promise < BackendLoginData > ( ( resolve , reject ) => {
48
+ let config = {
49
+ headers : {
50
+ Authorization : `Basic ${ btoa ( userName + ":" + hashed ) } `
51
+ }
52
+ } ;
53
+
54
+ return Axios . get < BackendLoginData > ( hostname + userPath + "/login" , config )
55
+ . then ( ( data : AxiosResponse < BackendLoginData > ) => {
56
+ console . log ( data . data ) ;
57
+ store . dispatch ( addRefreshToken ( data . data . tokenValue ) ) ;
58
+ store . dispatch ( updateUser ( data . data . user as UserState ) ) ;
59
+
60
+ if ( stayLoggedIn ) {
61
+ setCookie ( cookieName , data . data . tokenValue , 60 ) ;
62
+ }
63
+
64
+ getAccessTokenWithRefreshToken ( ) ;
65
+ } )
66
+ . catch ( ( error ) => {
67
+ reject ( error ) ;
68
+ } ) ;
69
+ } ) ;
74
70
} ;
75
71
76
72
export const getAccessTokenWithRefreshToken = ( ) => {
77
- console . log ( "getAccessTokenWithRefreshToken" ) ;
73
+ console . log ( "getAccessTokenWithRefreshToken" ) ;
78
74
79
- let refreshToken : string | null = store . getState ( ) . tokens . refreshToken ;
75
+ let refreshToken : string | null = store . getState ( ) . tokens . refreshToken ;
80
76
81
- let config = {
82
- headers : {
83
- Authorization : `Bearer ${ refreshToken } `
84
- }
85
- } ;
86
-
87
- Axios . get < BackendAuthData > ( hostname + userPath + "/auth" , config )
88
- . then ( ( data : AxiosResponse < BackendAuthData > ) => {
89
- store . dispatch ( checkedCookies ( CookieStatus . FINISHED ) ) ;
90
- setAuthHeaderToAxios ( data . data . tokenValue ) ;
91
-
92
- store . dispatch (
93
- addAccessToken ( {
94
- token : data . data . tokenValue ,
95
- timestamp : data . data . validUntil
96
- } as AccessToken )
97
- ) ;
98
- if ( ! store . getState ( ) . user . username ) {
99
- getOwnUserData ( data . data . userId ) ;
100
- }
101
- } )
102
- . catch ( ( error ) => {
103
- store . dispatch ( removeTokens ( ) ) ;
104
- store . dispatch ( checkedCookies ( CookieStatus . FINISHED ) ) ;
105
-
106
- console . log ( error ) ;
107
- //you probably want to notify the user, maybe with a toast or similar
108
- } ) ;
77
+ let config = {
78
+ headers : {
79
+ Authorization : `Bearer ${ refreshToken } `
80
+ }
81
+ } ;
82
+
83
+ Axios . get < BackendAuthData > ( hostname + userPath + "/auth" , config )
84
+ . then ( ( data : AxiosResponse < BackendAuthData > ) => {
85
+ store . dispatch ( checkedCookies ( CookieStatus . FINISHED ) ) ;
86
+ setAuthHeaderToAxios ( data . data . tokenValue ) ;
87
+
88
+ store . dispatch (
89
+ addAccessToken ( {
90
+ token : data . data . tokenValue ,
91
+ timestamp : data . data . validUntil
92
+ } as AccessToken )
93
+ ) ;
94
+ if ( ! store . getState ( ) . user . username ) {
95
+ getOwnUserData ( data . data . userId ) ;
96
+ }
97
+ } )
98
+ . catch ( ( error ) => {
99
+ store . dispatch ( removeTokens ( ) ) ;
100
+ store . dispatch ( checkedCookies ( CookieStatus . FINISHED ) ) ;
101
+
102
+ console . log ( error ) ;
103
+ //you probably want to notify the user, maybe with a toast or similar
104
+ } ) ;
109
105
} ;
110
106
111
107
const getOwnUserData = ( userId : number ) => {
112
- Axios . get < UserState > ( `${ hostname } ${ userPath } /${ userId } /info` )
113
- . then ( ( response : AxiosResponse < UserState > ) => {
114
- store . dispatch ( updateUser ( response . data ) ) ;
115
- } )
116
- . catch ( ( error ) => {
117
- console . log ( error ) ;
118
- } ) ;
108
+ Axios . get < UserState > ( `${ hostname } ${ userPath } /${ userId } /info` )
109
+ . then ( ( response : AxiosResponse < UserState > ) => {
110
+ store . dispatch ( updateUser ( response . data ) ) ;
111
+ } )
112
+ . catch ( ( error ) => {
113
+ console . log ( error ) ;
114
+ } ) ;
119
115
} ;
120
116
121
117
export const logout = ( ) => {
122
- store . dispatch ( removeTokens ( ) ) ;
123
- deleteCookie ( cookieName ) ;
118
+ store . dispatch ( removeTokens ( ) ) ;
119
+ deleteCookie ( cookieName ) ;
124
120
} ;
125
121
126
122
function setAuthHeaderToAxios ( accessToken : string ) {
127
- Axios . defaults . headers . common [ "Authorization" ] = `Bearer ${ accessToken } ` ;
123
+ Axios . defaults . headers . common [ "Authorization" ] = `Bearer ${ accessToken } ` ;
128
124
}
0 commit comments