Skip to content

Commit e5b6515

Browse files
committed
deleting token 10 days after from local storage.
1 parent 83bb19d commit e5b6515

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

BankApplicationFrontend/src/components/pages/LoginPage/LoginPage.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { connect } from 'react-redux'
55
import FormElement from '../../FormElement/FormElement'
66
import Request from '../../../services/Request'
77
import { login, showDialog } from '../../../actions'
8-
import { ERROR_ALLFIELDSMANDATORY, URL_LOGIN, LSKEY_USERNAME, LSKEY_TOKEN } from '../../../config/constants'
8+
import { ERROR_ALLFIELDSMANDATORY, URL_LOGIN, LSKEY_USERNAME, LSKEY_TOKEN, LSKEY_TOKEN_EXPIRATIONTIME, TOKEN_EXPIRATION_TIME } from '../../../config/constants'
99

1010
const mapStateToProps = state => ({
1111
isLoggedIn: state.login != null
@@ -54,9 +54,16 @@ class LoginPage extends React.Component {
5454
password: this.state.password
5555
}).then((response) => {
5656
if(response != null && response.data != null) {
57-
localStorage.setItem(LSKEY_USERNAME, this.state.username);
57+
const username = this.state.username;
58+
59+
localStorage.setItem(LSKEY_USERNAME, username);
5860
localStorage.setItem(LSKEY_TOKEN, response.data);
59-
this.props.login(this.state.username, response.data);
61+
62+
var tokenExpirationTime = new Date();
63+
tokenExpirationTime.setHours(tokenExpirationTime.getHours() + TOKEN_EXPIRATION_TIME);
64+
localStorage.setItem(LSKEY_TOKEN_EXPIRATIONTIME, tokenExpirationTime);
65+
66+
this.props.login(username, response.data);
6067
}
6168
}).catch((error) => {
6269
console.log(error.response);

BankApplicationFrontend/src/components/pages/MainPage/MainPage.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import RegisterPage from '../RegisterPage/RegisterPage'
1717
import LoginPage from '../LoginPage/LoginPage'
1818

1919
import { login } from '../../../actions'
20-
import { LSKEY_USERNAME, LSKEY_TOKEN } from '../../../config/constants'
20+
import { LSKEY_USERNAME, LSKEY_TOKEN, LSKEY_TOKEN_EXPIRATIONTIME } from '../../../config/constants'
2121

2222
const mapDispatchToProps = dispatch => ({
2323
login: (username, token) => dispatch(login(username, token))
@@ -30,7 +30,14 @@ class MainPage extends React.Component {
3030

3131
const storedUsername = localStorage.getItem(LSKEY_USERNAME);
3232
if (storedUsername != null) {
33-
this.props.login(storedUsername, localStorage.getItem(LSKEY_TOKEN));
33+
const tokenExpirationTime = localStorage.getItem(LSKEY_TOKEN_EXPIRATIONTIME);
34+
if (new Date() > tokenExpirationTime) {
35+
localStorage.removeItem(LSKEY_USERNAME);
36+
localStorage.removeItem(LSKEY_TOKEN);
37+
localStorage.removeItem(LSKEY_TOKEN_EXPIRATIONTIME);
38+
} else {
39+
this.props.login(storedUsername, localStorage.getItem(LSKEY_TOKEN));
40+
}
3441
}
3542
}
3643

BankApplicationFrontend/src/components/pages/UsersPage/UsersPage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class UsersPage extends React.Component {
2929
console.log(response);
3030
this.setState({ users: response.data });
3131
}).catch((error) => {
32-
console.log(error);
32+
console.log(error.response);
3333
var errorMessage = 'Network error.';
3434
if (error != null && error.response != null && error.response.data != null && error.response.data.message != null) {
3535
errorMessage = error.response.data.message;

BankApplicationFrontend/src/config/constants.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@ export const URL_RETRIEVEWEALTH = 'wealth/retrieve';
2828

2929
// Local storage keys
3030
export const LSKEY_USERNAME = 'username';
31-
export const LSKEY_TOKEN = 'token';
31+
export const LSKEY_TOKEN = 'token';
32+
export const LSKEY_TOKEN_EXPIRATIONTIME = 'tokenExpirationTime';
33+
34+
export const TOKEN_EXPIRATION_TIME = 24 * 10; // 10 days.

0 commit comments

Comments
 (0)