Skip to content

Commit 7bbb4a4

Browse files
author
Jovert Lota Palonpon
committed
wip
1 parent c68234c commit 7bbb4a4

File tree

9 files changed

+301
-123
lines changed

9 files changed

+301
-123
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
}
5050
},
5151
"scripts": {
52+
"test": [
53+
"vendor/bin/phpunit"
54+
],
5255
"format": [
5356
"vendor/bin/php-cs-fixer fix ."
5457
],

resources/js/Backoffice.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,30 @@ import { Loading } from './views';
1010
class Backoffice extends Component {
1111
state = {
1212
loading: true,
13+
navigating: true,
1314
authenticated: false,
1415
auth: {},
1516
user: {},
1617
username: '',
1718
};
1819

20+
/**
21+
* Refresh the user's session.
22+
*
23+
* @return {undefined}
24+
*/
25+
refresh = async () => {
26+
this.setState({ navigating: true });
27+
28+
try {
29+
const response = await axios.post('/api/auth/refresh');
30+
31+
await this.authenticate(JSON.stringify(response.data));
32+
33+
this.setState({ navigating: false });
34+
} catch (error) {}
35+
};
36+
1937
/**
2038
* Authenticate the user.
2139
*
@@ -33,8 +51,7 @@ class Backoffice extends Component {
3351
auth.auth_token
3452
}`;
3553

36-
// Store it locally for the authentication data to persist.
37-
window.localStorage.setItem('auth', tokenString);
54+
this.setAuthData(auth);
3855

3956
await this.fetchAuthUser();
4057
};
@@ -92,8 +109,8 @@ class Backoffice extends Component {
92109
*
93110
* @return {object}
94111
*/
95-
getAuthData = async () => {
96-
const authString = await window.localStorage.getItem('auth');
112+
getAuthData = () => {
113+
const authString = window.localStorage.getItem('auth');
97114
const auth = JSON.parse(authString);
98115

99116
if (!authString) {
@@ -105,6 +122,18 @@ class Backoffice extends Component {
105122
return auth;
106123
};
107124

125+
/**
126+
* Store the authentication object as string into a persistent storage.
127+
*
128+
* @param {object} data
129+
*
130+
* @return {undefined}
131+
*/
132+
setAuthData = data => {
133+
// Store it locally for the authentication data to persist.
134+
window.localStorage.setItem('auth', JSON.stringify(data));
135+
};
136+
108137
/**
109138
* Fetch the authenticated user.
110139
*
@@ -130,7 +159,7 @@ class Backoffice extends Component {
130159
await this.authenticate(JSON.stringify(auth));
131160
}
132161

133-
this.setState({ loading: false });
162+
this.setState({ loading: false, navigating: false });
134163
}
135164

136165
render() {
@@ -151,6 +180,7 @@ class Backoffice extends Component {
151180
width,
152181
environment: 'backoffice',
153182
routes: ROUTES,
183+
refresh: this.refresh,
154184
authenticate: this.authenticate,
155185
handleLock: this.handleLock,
156186
handleSignout: this.handleSignout,

resources/js/core/Navigator.js

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,82 @@
1-
import React from 'react';
1+
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import { withRouter, Route, Switch, Redirect } from 'react-router-dom';
44

55
import * as NavigationUtils from '../utils/Navigation';
66
import * as UrlUtils from '../utils/URL';
77

8-
const Navigator = props => {
9-
const { authenticated, username, environment, routes } = props.pageProps;
8+
class Navigator extends Component {
9+
async componentDidUpdate(prevProps) {
10+
const { location, pageProps } = this.props;
1011

11-
return (
12-
<Switch>
13-
{routes.map((route, i) => {
14-
const View = route.component;
12+
// notify the parent component that the user is navigating...
13+
if (location.pathname !== prevProps.location.pathname) {
14+
await pageProps.refresh();
15+
}
16+
}
1517

16-
return (
17-
<Route
18-
key={i}
19-
path={route.path}
20-
exact
21-
render={routeProps => {
22-
if (route.auth) {
23-
if (!authenticated) {
24-
return (
25-
<Redirect
26-
to={{
27-
search: UrlUtils._queryString({
28-
username,
29-
}),
30-
pathname: NavigationUtils._route(
31-
'auth.signin',
32-
),
33-
}}
34-
/>
35-
);
18+
render() {
19+
const {
20+
authenticated,
21+
username,
22+
environment,
23+
routes,
24+
} = this.props.pageProps;
25+
26+
return (
27+
<Switch>
28+
{routes.map((route, i) => {
29+
const View = route.component;
30+
31+
return (
32+
<Route
33+
key={i}
34+
path={route.path}
35+
exact
36+
render={routeProps => {
37+
if (route.auth) {
38+
if (!authenticated) {
39+
return (
40+
<Redirect
41+
to={{
42+
search: UrlUtils._queryString(
43+
{
44+
username,
45+
},
46+
),
47+
pathname: NavigationUtils._route(
48+
'auth.signin',
49+
),
50+
}}
51+
/>
52+
);
53+
}
3654
}
37-
}
3855

39-
if (!route.auth) {
40-
if (authenticated) {
41-
return (
42-
<Redirect
43-
to={NavigationUtils._route(
44-
`${environment}.home`,
45-
)}
46-
/>
47-
);
56+
if (!route.auth) {
57+
if (authenticated) {
58+
return (
59+
<Redirect
60+
to={NavigationUtils._route(
61+
`${environment}.home`,
62+
)}
63+
/>
64+
);
65+
}
4866
}
49-
}
5067

51-
return <View {...props} {...routeProps} />;
52-
}}
53-
/>
54-
);
55-
})}
56-
</Switch>
57-
);
58-
};
68+
return <View {...this.props} {...routeProps} />;
69+
}}
70+
/>
71+
);
72+
})}
73+
</Switch>
74+
);
75+
}
76+
}
5977

6078
Navigator.propTypes = {
61-
pageProps: PropTypes.object,
79+
pageProps: PropTypes.object.isRequired,
6280
};
6381

6482
export default withRouter(Navigator);

resources/js/ui/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { default as Modal } from './Modal';
2+
export { default as Skeleton } from './Skeleton';
23
export { default as Snackbar } from './Snackbar';
34
export { default as Table } from './Table';
4-
export { default as TableToolbar } from './TableToolbar/TableToolbar';
55
export { default as TablePaginationActions } from './TablePaginationActions';
6+
export { default as TableToolbar } from './TableToolbar/TableToolbar';

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class Master extends Component {
5555
children,
5656
history,
5757
location,
58+
pageProps,
5859
loading,
5960
message,
6061
alert,
@@ -83,6 +84,7 @@ class Master extends Component {
8384
<nav className={classes.drawer}>
8485
<Hidden smUp implementation="js">
8586
<Sidebar
87+
pageProps={pageProps}
8688
PaperProps={{ style: { width: drawerWidth } }}
8789
variant="temporary"
8890
open={mobileOpen}
@@ -94,6 +96,7 @@ class Master extends Component {
9496

9597
<Hidden xsDown implementation="css">
9698
<Sidebar
99+
pageProps={pageProps}
97100
PaperProps={{ style: { width: drawerWidth } }}
98101
locationPathname={location.pathname}
99102
navigate={path => history.push(path)}

0 commit comments

Comments
 (0)