Skip to content

Commit

Permalink
feat: auto log out if token is expired
Browse files Browse the repository at this point in the history
  • Loading branch information
mbetancurt committed Jun 29, 2020
1 parent 31b7bce commit e1bf678
Show file tree
Hide file tree
Showing 19 changed files with 39 additions and 3 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified cypress/videos/dsh_list_users.js.mp4
Binary file not shown.
Binary file modified cypress/videos/dsh_newvehicle.js.mp4
Binary file not shown.
Binary file modified cypress/videos/dsh_sidemenu.js.mp4
Binary file not shown.
Binary file modified cypress/videos/map_createnewoperation.js.mp4
Binary file not shown.
Binary file modified cypress/videos/map_layers.js.mp4
Binary file not shown.
Binary file modified cypress/videos/map_operations.js.mp4
Binary file not shown.
Binary file modified cypress/videos/map_quickfly.js.mp4
Binary file not shown.
Binary file modified cypress/videos/sp_login.js.mp4
Binary file not shown.
Binary file modified cypress/videos/sp_registration.js.mp4
Binary file not shown.
Binary file modified cypress/videos/sp_simulator.js.mp4
Binary file not shown.
Binary file modified cypress/videos/sp_verify.js.mp4
Binary file not shown.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"async-mutex": "^0.1.4",
"axios": "^0.19.1",
"i18next": "^19.4.2",
"jwt-decode": "^2.2.0",
"leaflet": "^1.6.0",
"leaflet-geometryutil": "^0.9.3",
"leaflet-hotline": "^0.4.0",
Expand Down
9 changes: 9 additions & 0 deletions src/Ades.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/Ades.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import S from 'sanctuary';
import { useTranslation } from 'react-i18next';
import './css/animate.css';
import jwtDecode from 'jwt-decode';

/*
* CSS Styling
Expand Down Expand Up @@ -66,8 +67,18 @@ import VerificationScreen from './VerificationScreen';
}*/

const MasterPage = ({leftIsExpanded = false, children}) => {
const [cookies, ] = useCookies(['jwt', 'lang']);
const [state, ] = useAdesState();
const decoded = jwtDecode(cookies.jwt);
const expDate = new Date(0);
expDate.setUTCSeconds(decoded.exp);
return(
<>
{state.debug &&
<div className='timeLeftOverlay'>
Token expires at {expDate.toLocaleTimeString()}
</div>
}
<LeftArea>
{/* <NotificationCenter/> */}
</LeftArea>
Expand Down Expand Up @@ -118,6 +129,15 @@ function Ades() {
}
} else {
setLoggedIn(true);
console.log('jwt', jwtDecode(cookies.jwt));
const decoded = jwtDecode(cookies.jwt);
setRole(decoded.role);

setTimeout(() => {
setLoggedIn(false);
removeCookie('user', {path: '/'});
removeCookie('jwt', {path: '/'});
}, (decoded.exp * 1000) - new Date().getTime());
if (S.isNothing(state.auth.user)) {
actions.auth.info(cookies['jwt'], cookies['user'], (user) => {
setRole(user.role);
Expand Down
4 changes: 2 additions & 2 deletions src/dashboard/vehicle/NewVehicle.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, {useEffect, useState} from 'react';
import {useTranslation} from 'react-i18next';
import {Button, FormGroup, InputGroup, Radio, RadioGroup} from '@blueprintjs/core';
import {fM} from '../../libs/SaferSanctuary';
import useAdesState from '../../state/AdesState';
import { useHistory, useParams } from 'react-router-dom';
import {fM} from '../../libs/SaferSanctuary';

const Text = ({name, label, description, placeholder = '', ...props}) => (
<FormGroup
Expand Down Expand Up @@ -132,7 +132,7 @@ function NewVehicle({userId}) {
accessType: '',
vehicleTypeId: '',
'org-uuid': '',
registeredBy: document.getElementById('text-registeredBy').value,
registeredBy: fM(state.auth.user).username,
};
setSubmitting(true);
actions.vehicles.post(
Expand Down
2 changes: 1 addition & 1 deletion src/map/hooks/useEditorLogic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {useEffect, useState} from 'react';
import S from 'sanctuary';
import _, {fM} from '../../libs/SaferSanctuary';
import _ from '../../libs/SaferSanctuary';
import {useHistory} from 'react-router-dom';
import useAdesState from '../../state/AdesState';

Expand Down
1 change: 1 addition & 0 deletions src/state/AdesState.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ const actions = {
info: (store, token, username, okCallback, errorCallback) => {
Axios.get('user/' + username, {headers: {'auth': token}})
.then(result => {
print(store.state, false, 'InfoState', result.data);
store.setState({auth: {...store.state.auth, token: S.Just(token), user: S.Just(result.data)}});
const socket = io(API, {
query: {
Expand Down

0 comments on commit e1bf678

Please sign in to comment.