Skip to content

Commit 179ed04

Browse files
committed
fix: Add token expiration check and remove unused utility
1 parent 5f6f320 commit 179ed04

4 files changed

Lines changed: 36 additions & 61 deletions

File tree

frontend/src/hooks/profiles/use-create-profile.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type {
1010
} from "@/lib/types/api";
1111
import { API_BASE_URL } from "@/lib/constants/apiConstants";
1212
import { getToken } from "@/lib/utils/jwt";
13-
import { useAuthHeader } from "@/hooks/use-auth-header";
1413

1514
async function postCreateProfile(
1615
input: CreateProfileInput,

frontend/src/hooks/use-auth-header.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useCallback, useState } from "react";
22
import { useAccount, useSignMessage } from "wagmi";
3-
import { getToken, saveToken, clearToken } from "@/lib/utils/jwt";
3+
import { getToken, saveToken, clearToken, isTokenValid } from "@/lib/utils/jwt";
44
import { useGetNonce } from "./profiles/use-get-nonce";
55
import { generateSiweMessage } from "@/lib/utils/siwe";
66
import { API_BASE_URL } from "@/lib/constants/apiConstants";
@@ -16,13 +16,14 @@ export function useAuthHeader() {
1616
const [isRefreshing, setIsRefreshing] = useState(false);
1717

1818
const getAuthHeaders = useCallback((): AuthHeaders => {
19-
const token = getToken();
20-
21-
// If JWT token exists, use it
22-
if (token) {
23-
return {
24-
Authorization: `Bearer ${token}`,
25-
};
19+
// Check if token is valid and not expired
20+
if (isTokenValid()) {
21+
const token = getToken();
22+
if (token) {
23+
return {
24+
Authorization: `Bearer ${token}`,
25+
};
26+
}
2627
}
2728

2829
// Fall back to empty headers (caller will add SIWE headers)
@@ -74,6 +75,6 @@ export function useAuthHeader() {
7475
getAuthHeaders,
7576
refreshToken,
7677
isRefreshing,
77-
hasToken: () => getToken() !== null,
78+
hasToken: isTokenValid,
7879
};
7980
}

frontend/src/lib/utils/fetch-with-jwt.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.

frontend/src/lib/utils/jwt.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
const JWT_TOKEN_KEY = "jwt_token";
22
const JWT_ADDRESS_KEY = "jwt_address";
3+
const JWT_EXPIRY_KEY = "jwt_expiry";
34

45
export interface JwtToken {
56
token: string;
67
address: string;
78
}
89

9-
export function saveToken(token: JwtToken): void {
10+
export function saveToken(token: JwtToken, expiresInSeconds = 86400): void {
11+
const expiryTime = Date.now() + expiresInSeconds * 1000;
1012
localStorage.setItem(JWT_TOKEN_KEY, token.token);
1113
localStorage.setItem(JWT_ADDRESS_KEY, token.address);
14+
localStorage.setItem(JWT_EXPIRY_KEY, expiryTime.toString());
1215
}
1316

1417
export function getToken(): string | null {
18+
if (!isTokenValid()) {
19+
clearToken();
20+
return null;
21+
}
1522
return localStorage.getItem(JWT_TOKEN_KEY);
1623
}
1724

@@ -22,8 +29,24 @@ export function getAddress(): string | null {
2229
export function clearToken(): void {
2330
localStorage.removeItem(JWT_TOKEN_KEY);
2431
localStorage.removeItem(JWT_ADDRESS_KEY);
32+
localStorage.removeItem(JWT_EXPIRY_KEY);
2533
}
2634

2735
export function isTokenValid(): boolean {
28-
return getToken() !== null;
29-
}
36+
const token = localStorage.getItem(JWT_TOKEN_KEY);
37+
const expiryStr = localStorage.getItem(JWT_EXPIRY_KEY);
38+
39+
if (!token || !expiryStr) {
40+
return false;
41+
}
42+
43+
const expiryTime = parseInt(expiryStr, 10);
44+
const isExpired = Date.now() >= expiryTime;
45+
46+
if (isExpired) {
47+
clearToken();
48+
return false;
49+
}
50+
51+
return true;
52+
}

0 commit comments

Comments
 (0)