Update token handling (added expiration time)

This commit is contained in:
Sami Abuzakuk
2025-11-01 21:58:20 +01:00
parent dff07ef340
commit 45b78d5faf
3 changed files with 21 additions and 2 deletions

View File

@@ -4,7 +4,17 @@ export const API_URL = env.PUBLIC_API_URL || 'http://localhost:8000';
// Helper to get token from localStorage // Helper to get token from localStorage
export function getToken(): string | null { export function getToken(): string | null {
return localStorage.getItem('token'); const tokenData = localStorage.getItem('token');
if (tokenData) {
const { value, expiresAt } = JSON.parse(tokenData);
if (Date.now() > expiresAt) {
localStorage.removeItem('token');
location.reload();
return null;
}
return value;
}
return null;
} }
// Helper to add Authorization header if token exists // Helper to add Authorization header if token exists

View File

@@ -30,7 +30,11 @@
} }
const data = await response.json(); const data = await response.json();
localStorage.setItem('token', data.access_token); const expirationTime = Date.now() + 60 * 60 * 1000; // 60 minutes in milliseconds
localStorage.setItem(
'token',
JSON.stringify({ value: data.access_token, expiresAt: expirationTime })
);
goto('/').then(() => location.reload()); goto('/').then(() => location.reload());
} catch (err) { } catch (err) {
loginError = 'Network error - ' + err; loginError = 'Network error - ' + err;

View File

@@ -122,6 +122,11 @@
<button class="p-2 w-full text-left" onclick={() => openNotificationPopup(notification)}> <button class="p-2 w-full text-left" onclick={() => openNotificationPopup(notification)}>
<div> <div>
<p class="font-semibold">{notification.title}</p> <p class="font-semibold">{notification.title}</p>
<p>
{notification.message.split('\n')[0].length > 75
? `${notification.message.split('\n')[0].slice(0, 75)}...`
: notification.message.split('\n')[0]}
</p>
<p class="text-sm text-gray-500"> <p class="text-sm text-gray-500">
{new Date(notification.created_at).toLocaleString()} {new Date(notification.created_at).toLocaleString()}
</p> </p>