@@ -5,21 +5,39 @@ import {hostname, userPath} from "./api";
5
5
6
6
import { AddUser , UserState } from "../redux/actions/userTypes" ;
7
7
import store from "../redux/store" ;
8
- import { addAccessToken , addRefreshToken } from "../redux/actions/tokens" ;
8
+ import { addAccessToken , addRefreshToken , checkedCookies , removeTokens } from "../redux/actions/tokens" ;
9
9
import { addUser } from "../redux/actions/user" ;
10
- import { AccessToken , AddAccessToken , AddRefreshToken , TokensState } from "../redux/actions/tokenTypes" ;
10
+ import { AccessToken , AddAccessToken , AddRefreshToken , CheckedCookies , RemoveTokens , TokensState } from "../redux/actions/tokenTypes" ;
11
+ import { deleteCookie , getCookie , setCookie } from "../methods/cookies" ;
11
12
12
13
13
14
// reference: https://daveceddia.com/access-redux-store-outside-react/
14
15
16
+
17
+ const cookieName :string = 'refreshToken' ;
18
+
15
19
export interface BackendLoginData {
16
20
refreshToken : string ,
17
21
user : UserState
18
22
19
23
}
20
24
21
- export const loginWithUsernameAndPassword = ( userName : string , password : string ) : Promise < BackendLoginData > => {
22
25
26
+ export const checkForCookie = ( ) => {
27
+ let refreshTokenCookieValue = getCookie ( cookieName )
28
+ if ( refreshTokenCookieValue ) {
29
+ store . dispatch ( addRefreshToken ( refreshTokenCookieValue ) as AddRefreshToken )
30
+ getAccessTokenWithRefreshToken ( ) ;
31
+ }
32
+ store . dispatch ( checkedCookies ( true ) as CheckedCookies )
33
+
34
+
35
+ }
36
+
37
+
38
+
39
+ export const loginWithUsernameAndPassword = ( userName : string , password : string , stayLoggedIn :boolean ) : Promise < BackendLoginData > => {
40
+ console . log ( "[Auth] loginWithUsernameAndPassword" )
23
41
return new Promise < BackendLoginData > ( ( resolve , reject ) => {
24
42
let config = {
25
43
headers : {
@@ -29,9 +47,15 @@ export const loginWithUsernameAndPassword = (userName: string, password: string)
29
47
30
48
return Axios . get ( hostname + userPath + '/login' , config )
31
49
. then ( ( data ) => {
32
- store . dispatch ( addRefreshToken ( data . data . refreshToken ) as AddRefreshToken )
50
+ console . log ( data . data )
51
+ store . dispatch ( addRefreshToken ( data . data . tokenValue ) as AddRefreshToken )
33
52
store . dispatch ( addUser ( data . data . user as UserState ) as AddUser )
34
53
54
+ if ( stayLoggedIn ) {
55
+ setCookie ( cookieName , data . data . tokenValue , 60 )
56
+ }
57
+
58
+
35
59
getAccessTokenWithRefreshToken ( )
36
60
} )
37
61
. catch ( ( ( error ) => {
@@ -57,17 +81,25 @@ export const getAccessTokenWithRefreshToken = () => {
57
81
58
82
Axios . get ( hostname + userPath + '/auth' , config )
59
83
. then ( ( data ) => {
60
- setAuthHeaderToAxios ( data . data . token )
84
+ setAuthHeaderToAxios ( data . data . tokenValue )
61
85
62
- store . dispatch ( addAccessToken ( { token : data . data . token , timestamp : data . data . validUntil } as AccessToken ) as AddAccessToken ) ;
86
+ store . dispatch ( addAccessToken ( { token : data . data . tokenValue , timestamp : data . data . validUntil } as AccessToken ) as AddAccessToken ) ;
63
87
64
88
} )
65
89
. catch ( ( ( error ) => {
90
+ store . dispatch ( removeTokens ( ) as RemoveTokens ) ;
91
+
66
92
console . log ( error )
93
+ //you probably want to notify the user, maybe with a toast or similar
67
94
} ) ) ;
68
95
69
96
}
70
97
98
+ export const logout = ( ) => {
99
+ store . dispatch ( removeTokens ( ) as RemoveTokens ) ;
100
+ deleteCookie ( cookieName ) ;
101
+ }
102
+
71
103
function setAuthHeaderToAxios ( accessToken : string ) {
72
104
Axios . defaults . headers . common [ 'Authorization' ] =
73
105
`Bearer ${ accessToken } ` ;
0 commit comments