Skip to content

Commit f7edb2c

Browse files
committed
Managing the Token Expiration - final
1 parent 2c5609f commit f7edb2c

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

backend/events.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"events":[{"title":"Another event","image":"https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse1.mm.bing.net%2Fth%3Fid%3DOIP.pzsIeXOfUfiGxZUFwJ_VwwHaE4%26pid%3DApi&f=1&ipt=e469e7b3657ed87d395649be2b4cc18e3794727ecebc5f8ddf2f69333c75be69&ipo=images","date":"2024-01-27","description":"Another awesome event.","id":"b5b592cf-c1cc-4c36-bd00-7670d1494ff6"},{"title":"A new event","image":"https://cdn.muenchen-p.de/fl_progressive,q_65/.imaging/stk/responsive/teaser300/dms/va-2016/muenchner-christkindlmarkt/christkindlmarkt-marienplat-logo-hp/document/christkindlmarkt-marienplat-logo-hp.jpg","date":"2022-10-01","description":"Some awesome event!","id":"2a42fcc4-ea21-4bdd-abf6-c40006dc66a9"}],"users":[{"email":"test2@test.com","password":"$2a$12$DpO//Ez6vBSVTwqTCDVQpOsrJPEMZ1GphoJMTy8.g7TWHiBP97dQ.","id":"0eb4897f-2f6d-4b30-bd04-9ce763f96697"},{"email":"test@test.com","password":"$2a$12$3m6KUOiNwanEyfkY61rBkeADcW2rW3FSIAd9bS4M5grlHMQkMDV/S","id":"e54095a1-a14c-46d6-b7dc-b316c3a6ccfd"},{"email":"eve.holt@reqres.in","password":"$2a$12$J9lpeDvXxDB3onD8FpMcGu2srQyzCpiMrFPdp/BezkQCSjXCZQlfC","id":"538c7b46-8b5c-482d-a4ab-106cd9e63b75"}]}
1+
{"events":[{"title":"Another event","image":"https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse1.mm.bing.net%2Fth%3Fid%3DOIP.pzsIeXOfUfiGxZUFwJ_VwwHaE4%26pid%3DApi&f=1&ipt=e469e7b3657ed87d395649be2b4cc18e3794727ecebc5f8ddf2f69333c75be69&ipo=images","date":"2024-01-27","description":"Another awesome event.\n","id":"b5b592cf-c1cc-4c36-bd00-7670d1494ff6"},{"title":"A new event","image":"https://cdn.muenchen-p.de/fl_progressive,q_65/.imaging/stk/responsive/teaser300/dms/va-2016/muenchner-christkindlmarkt/christkindlmarkt-marienplat-logo-hp/document/christkindlmarkt-marienplat-logo-hp.jpg","date":"2022-10-01","description":"Some awesome event!","id":"2a42fcc4-ea21-4bdd-abf6-c40006dc66a9"}],"users":[{"email":"test2@test.com","password":"$2a$12$DpO//Ez6vBSVTwqTCDVQpOsrJPEMZ1GphoJMTy8.g7TWHiBP97dQ.","id":"0eb4897f-2f6d-4b30-bd04-9ce763f96697"},{"email":"test@test.com","password":"$2a$12$3m6KUOiNwanEyfkY61rBkeADcW2rW3FSIAd9bS4M5grlHMQkMDV/S","id":"e54095a1-a14c-46d6-b7dc-b316c3a6ccfd"},{"email":"eve.holt@reqres.in","password":"$2a$12$J9lpeDvXxDB3onD8FpMcGu2srQyzCpiMrFPdp/BezkQCSjXCZQlfC","id":"538c7b46-8b5c-482d-a4ab-106cd9e63b75"}]}

frontend/src/pages/Authentication.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export async function action({ request }) {
4141
const token = resData.token;
4242

4343
localStorage.setItem('token', token);
44+
const expiration = new Date();
45+
expiration.setHours(expiration.getHours() + 1);
46+
localStorage.setItem('expiration', expiration.toISOString());
4447

4548
return redirect('/');
4649
}

frontend/src/pages/Logout.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ import { redirect } from 'react-router-dom';
22

33
export function action() {
44
localStorage.removeItem('token');
5+
localStorage.removeItem('expiration');
56
return redirect('/');
67
}

frontend/src/pages/Root.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Outlet, useLoaderData, useSubmit } from 'react-router-dom';
22

33
import MainNavigation from '../components/MainNavigation';
44
import { useEffect } from 'react';
5+
import { getTokenDuration } from '../util/auth';
56

67
function RootLayout() {
78
const token = useLoaderData();
@@ -12,11 +13,18 @@ function RootLayout() {
1213
return;
1314
}
1415

15-
const timeout = 1 * 60 * 60 * 1000; // 1 hour
16+
if (token === 'EXPIRED') {
17+
submit(null, { action: '/logout', method: 'post' });
18+
return;
19+
}
20+
21+
// const timeout = 1 * 60 * 60 * 1000; 1 hour
22+
const tokenDuration = getTokenDuration();
23+
console.log(tokenDuration);
1624

1725
setTimeout(() => {
1826
submit(null, { action: '/logout', method: 'post' });
19-
}, timeout);
27+
}, tokenDuration);
2028
}, [token, submit]);
2129

2230
return (

frontend/src/util/auth.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
import { redirect } from 'react-router-dom';
22

3+
export function getTokenDuration() {
4+
const storedExpirationDate = localStorage.getItem('expiration');
5+
const expirationDate = new Date(storedExpirationDate);
6+
const now = new Date();
7+
const duration = expirationDate.getTime() - now.getTime();
8+
return duration;
9+
}
10+
311
export function getAuthToken() {
412
const token = localStorage.getItem('token');
13+
14+
if (!token) {
15+
return null;
16+
}
17+
18+
const tokenDuration = getTokenDuration();
19+
20+
if (tokenDuration < 0) {
21+
return 'EXPIRED';
22+
}
23+
524
return token;
625
}
726

0 commit comments

Comments
 (0)