Skip to content

Commit 71de3af

Browse files
author
Jovert Lota Palonpon
committed
wip
1 parent 4d4c034 commit 71de3af

File tree

3 files changed

+54
-39
lines changed

3 files changed

+54
-39
lines changed

resources/js/Backoffice.js

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Backoffice extends Component {
1313
navigating: true,
1414
authenticated: false,
1515
nightMode: false,
16-
auth: {},
16+
token: {},
1717
user: {},
1818
username: '',
1919
};
@@ -28,10 +28,22 @@ class Backoffice extends Component {
2828

2929
try {
3030
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+
});
3547
} catch (error) {}
3648
};
3749

@@ -42,19 +54,14 @@ class Backoffice extends Component {
4254
*
4355
* @return {undefined}
4456
*/
45-
authenticate = async (tokenString = null) => {
46-
const auth = JSON.parse(tokenString);
57+
authenticate = async tokenString => {
58+
const token = JSON.parse(tokenString);
4759

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);
5462

55-
this.setAuthData(auth);
56-
57-
await this.fetchAuthUser();
63+
await this.fetchUser();
64+
}
5865
};
5966

6067
/**
@@ -69,8 +76,8 @@ class Backoffice extends Component {
6976
const response = await axios.post('/api/auth/signout');
7077

7178
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');
7481

7582
this.setState({
7683
loading: false,
@@ -143,59 +150,68 @@ class Backoffice extends Component {
143150
*
144151
* @return {object}
145152
*/
146-
getAuthData = () => {
147-
const authString = window.localStorage.getItem('auth');
148-
const auth = JSON.parse(authString);
153+
token = () => {
154+
const tokenString = window.localStorage.getItem('token');
149155

150-
if (!authString) {
156+
if (!tokenString) {
151157
return {};
152158
}
153159

154-
this.setState({ auth });
160+
const token = JSON.parse(tokenString);
161+
162+
this.setState({ token });
155163

156-
return auth;
164+
return token;
157165
};
158166

159167
/**
160168
* Store the authentication object as string into a persistent storage.
161169
*
162-
* @param {object} data
170+
* @param {object} token
163171
*
164172
* @return {undefined}
165173
*/
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));
169184
};
170185

171186
/**
172187
* Fetch the authenticated user.
173188
*
174189
* @return {undefined}
175190
*/
176-
fetchAuthUser = async () => {
191+
fetchUser = async () => {
192+
this.setState({ loading: true });
193+
177194
try {
178195
const response = await axios.post('/api/auth/user');
179196

180197
if (response.status === 200) {
181198
this.setState({
199+
loading: false,
182200
authenticated: true,
183201
user: response.data,
184202
});
185203
}
186-
} catch (error) {
187-
//
188-
}
204+
} catch (error) {}
189205
};
190206

191207
async componentDidMount() {
192-
const auth = await this.getAuthData();
208+
this.setNightMode();
193209

194-
if (auth) {
195-
await this.authenticate(JSON.stringify(auth));
196-
}
210+
const token = this.token();
197211

198-
this.setNightMode();
212+
if (token) {
213+
await this.authenticate(JSON.stringify(token));
214+
}
199215

200216
this.setState({ loading: false, navigating: false });
201217
}

resources/js/views/__backoffice/layouts/Master.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ class Master extends Component {
256256
</AppBar>
257257

258258
<main className={classes.content}>
259-
{loading ? (
259+
{loading || navigating ? (
260260
renderLoading
261261
) : (
262262
<Grid container>{children}</Grid>

resources/js/views/__backoffice/users/List.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
IconButton,
77
Tooltip,
88
Typography,
9-
withStyles,
109
} from '@material-ui/core';
1110

1211
import { Delete as DeleteIcon, Edit as EditIcon } from '@material-ui/icons';

0 commit comments

Comments
 (0)