|
Last change
on this file since 42da64d was 42da64d, checked in by Andrej <asumanovski@…>, 8 weeks ago |
|
Fixed auth issue
|
-
Property mode
set to
100644
|
|
File size:
989 bytes
|
| Line | |
|---|
| 1 | export const AUTH_TOKEN_KEY = "authToken";
|
|---|
| 2 | export const AUTH_USER_KEY = "authUser";
|
|---|
| 3 |
|
|---|
| 4 | function decodeJwtPayload(token) {
|
|---|
| 5 | try {
|
|---|
| 6 | const parts = token.split(".");
|
|---|
| 7 | if (parts.length < 2) return null;
|
|---|
| 8 | const base64 = parts[1].replace(/-/g, "+").replace(/_/g, "/");
|
|---|
| 9 | const normalized = base64.padEnd(Math.ceil(base64.length / 4) * 4, "=");
|
|---|
| 10 | const json = atob(normalized);
|
|---|
| 11 | return JSON.parse(json);
|
|---|
| 12 | } catch {
|
|---|
| 13 | return null;
|
|---|
| 14 | }
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | export function isTokenExpired(token) {
|
|---|
| 18 | if (!token) return true;
|
|---|
| 19 | const payload = decodeJwtPayload(token);
|
|---|
| 20 | if (!payload || typeof payload.exp !== "number") return true;
|
|---|
| 21 | return payload.exp * 1000 <= Date.now();
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | export function getAuthToken() {
|
|---|
| 25 | return localStorage.getItem(AUTH_TOKEN_KEY);
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | export function clearAuthSession() {
|
|---|
| 29 | localStorage.removeItem(AUTH_TOKEN_KEY);
|
|---|
| 30 | localStorage.removeItem(AUTH_USER_KEY);
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | export function logoutAndRedirect(path = "/dashboard") {
|
|---|
| 34 | clearAuthSession();
|
|---|
| 35 | window.location.assign(path);
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.