feat: add login expiry check

This commit is contained in:
Damillora 2022-04-15 03:49:59 +07:00
parent afbdc56b71
commit 2c56336c4f
4 changed files with 14 additions and 3 deletions

View File

@ -1,9 +1,10 @@
<script> <script>
import { token } from "./stores.js"; import { token } from "./stores.js";
import { isTokenExpired } from "./login-check.js";
let loggedIn = false; let loggedIn = false;
token.subscribe((value) => { token.subscribe((value) => {
loggedIn = value !== ""; loggedIn = !isTokenExpired(value);
}); });
</script> </script>

View File

@ -2,10 +2,11 @@
import { token } from "./stores.js"; import { token } from "./stores.js";
import { navigate } from "svelte-routing"; import { navigate } from "svelte-routing";
import { onMount } from "svelte"; import { onMount } from "svelte";
import { isTokenExpired } from "./login-check.js";
let loggedIn = false; let loggedIn = false;
token.subscribe((value) => { token.subscribe((value) => {
loggedIn = value !== ""; loggedIn = !isTokenExpired(value);
}); });
onMount(() => { onMount(() => {

View File

@ -1,12 +1,13 @@
<script> <script>
import { Link } from "svelte-routing"; import { Link } from "svelte-routing";
import { token } from "./stores.js"; import { token } from "./stores.js";
import { isTokenExpired } from "./login-check.js";
let menu_shown = false; let menu_shown = false;
let loggedIn = false; let loggedIn = false;
token.subscribe((value) => { token.subscribe((value) => {
loggedIn = value !== ""; loggedIn = !isTokenExpired(value);
}); });
const toggleMenu = () => { const toggleMenu = () => {

View File

@ -0,0 +1,8 @@
const isTokenExpired = (token) => {
if (token === "") return true;
const expiry = (JSON.parse(atob(token.split('.')[1]))).exp;
return (Math.floor((new Date).getTime() / 1000)) >= expiry;
}
export { isTokenExpired }