@@ -13,7 +13,7 @@ class Backoffice extends Component {
13
13
navigating : true ,
14
14
authenticated : false ,
15
15
nightMode : false ,
16
- auth : { } ,
16
+ token : { } ,
17
17
user : { } ,
18
18
username : '' ,
19
19
} ;
@@ -28,10 +28,22 @@ class Backoffice extends Component {
28
28
29
29
try {
30
30
const response = await axios . post ( '/api/auth/refresh' ) ;
31
-
32
- await this . authenticate ( JSON . stringify ( response . data ) ) ;
33
-
34
- this . setState ( { navigating : false } ) ;
31
+ const token = response . data ;
32
+
33
+ this . setToken ( token ) ;
34
+
35
+ this . setState ( prevState => {
36
+ return {
37
+ navigating : false ,
38
+ token,
39
+
40
+ // Update the user's auth_token after refreshing it in the API
41
+ user : {
42
+ ...prevState . user ,
43
+ auth_token : token . auth_token ,
44
+ } ,
45
+ } ;
46
+ } ) ;
35
47
} catch ( error ) { }
36
48
} ;
37
49
@@ -42,19 +54,14 @@ class Backoffice extends Component {
42
54
*
43
55
* @return {undefined }
44
56
*/
45
- authenticate = async ( tokenString = null ) => {
46
- const auth = JSON . parse ( tokenString ) ;
57
+ authenticate = async tokenString => {
58
+ const token = JSON . parse ( tokenString ) ;
47
59
48
- // We will set a default Authorization header, this will
49
- // eliminate the need to include the Authorization header
50
- // for almost every AJAX requests.
51
- window . axios . defaults . headers . common [ 'Authorization' ] = `Bearer ${
52
- auth . auth_token
53
- } `;
60
+ if ( token ) {
61
+ this . setToken ( token ) ;
54
62
55
- this . setAuthData ( auth ) ;
56
-
57
- await this . fetchAuthUser ( ) ;
63
+ await this . fetchUser ( ) ;
64
+ }
58
65
} ;
59
66
60
67
/**
@@ -69,8 +76,8 @@ class Backoffice extends Component {
69
76
const response = await axios . post ( '/api/auth/signout' ) ;
70
77
71
78
if ( response . status === 200 ) {
72
- // remove auth data stored in localStorage.
73
- await localStorage . removeItem ( 'auth ' ) ;
79
+ // remove token data stored in localStorage.
80
+ await localStorage . removeItem ( 'token ' ) ;
74
81
75
82
this . setState ( {
76
83
loading : false ,
@@ -143,59 +150,68 @@ class Backoffice extends Component {
143
150
*
144
151
* @return {object }
145
152
*/
146
- getAuthData = ( ) => {
147
- const authString = window . localStorage . getItem ( 'auth' ) ;
148
- const auth = JSON . parse ( authString ) ;
153
+ token = ( ) => {
154
+ const tokenString = window . localStorage . getItem ( 'token' ) ;
149
155
150
- if ( ! authString ) {
156
+ if ( ! tokenString ) {
151
157
return { } ;
152
158
}
153
159
154
- this . setState ( { auth } ) ;
160
+ const token = JSON . parse ( tokenString ) ;
161
+
162
+ this . setState ( { token } ) ;
155
163
156
- return auth ;
164
+ return token ;
157
165
} ;
158
166
159
167
/**
160
168
* Store the authentication object as string into a persistent storage.
161
169
*
162
- * @param {object } data
170
+ * @param {object } token
163
171
*
164
172
* @return {undefined }
165
173
*/
166
- setAuthData = data => {
167
- // Store it locally for the authentication data to persist.
168
- window . localStorage . setItem ( 'auth' , JSON . stringify ( data ) ) ;
174
+ setToken = token => {
175
+ // We will set a default Authorization header, this will
176
+ // eliminate the need to include the Authorization header
177
+ // for almost every AJAX requests.
178
+ window . axios . defaults . headers . common [ 'Authorization' ] = `Bearer ${
179
+ token . auth_token
180
+ } `;
181
+
182
+ // Store it locally for the authentication token to persist.
183
+ window . localStorage . setItem ( 'token' , JSON . stringify ( token ) ) ;
169
184
} ;
170
185
171
186
/**
172
187
* Fetch the authenticated user.
173
188
*
174
189
* @return {undefined }
175
190
*/
176
- fetchAuthUser = async ( ) => {
191
+ fetchUser = async ( ) => {
192
+ this . setState ( { loading : true } ) ;
193
+
177
194
try {
178
195
const response = await axios . post ( '/api/auth/user' ) ;
179
196
180
197
if ( response . status === 200 ) {
181
198
this . setState ( {
199
+ loading : false ,
182
200
authenticated : true ,
183
201
user : response . data ,
184
202
} ) ;
185
203
}
186
- } catch ( error ) {
187
- //
188
- }
204
+ } catch ( error ) { }
189
205
} ;
190
206
191
207
async componentDidMount ( ) {
192
- const auth = await this . getAuthData ( ) ;
208
+ this . setNightMode ( ) ;
193
209
194
- if ( auth ) {
195
- await this . authenticate ( JSON . stringify ( auth ) ) ;
196
- }
210
+ const token = this . token ( ) ;
197
211
198
- this . setNightMode ( ) ;
212
+ if ( token ) {
213
+ await this . authenticate ( JSON . stringify ( token ) ) ;
214
+ }
199
215
200
216
this . setState ( { loading : false , navigating : false } ) ;
201
217
}
0 commit comments