generated from Newton-School/html-css-js-project-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authvalidation.js
70 lines (43 loc) · 1.89 KB
/
authvalidation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// (function(){
// const hash = window.location.hash;
// const queryParams = {};
// hash.split('&').forEach((pair) => {
// const [key, value] = pair.split('=');
// queryParams[key] = value;
// });
// const accessToken = queryParams["#access_token"];
// const tokenType = queryParams.token_type;
// const expiresIn = queryParams.expires_in;
// localStorage.setItem('accessToken', accessToken);
// localStorage.setItem('tokenType', tokenType);
// localStorage.setItem('expiresIn', expiresIn);
// // localStorage.getItem('accessToken', accessToken);
// // localStorage.getItem('tokenType', tokenType);
// // localStorage.getItem('expiresIn', expiresIn);
// })();
// localStorage.getItem('accessToken', accessToken);
// localStorage.getItem('tokenType', tokenType);
// localStorage.getItem('expiresIn', expiresIn);
const isTokenValid = ()=> {
const expiresIn = localStorage.getItem('expiresIn');
const expirationTime = new Date(parseInt(expiresIn, 10) * 1000); // Convert expiresIn to milliseconds
// Get the current time
const currentTime = new Date();
return currentTime < expirationTime;
}
const isTokenExists = ()=>{
const expiresIn = localStorage.getItem('expiresIn');
const accessToken = localStorage.getItem('accessToken');
const tokenType = localStorage.getItem('tokenType');
if(tokenType != undefined && accessToken != undefined && expiresIn != undefined){
// return true;
window.location.href = `http://127.0.0.1:5500/index.html?#access_token=${accessToken}&token_type=${tokenType}&expires_in=${expiresIn}`;
}
else{
// return false;
window.location.href = "./login_page.html";
}
}
console.log("token exists"+isTokenExists());
// console.log("token valid"+isTokenValid());
isTokenExists();