Update token handling (added expiration time)
This commit is contained in:
@@ -4,7 +4,17 @@ export const API_URL = env.PUBLIC_API_URL || 'http://localhost:8000';
|
||||
|
||||
// Helper to get token from localStorage
|
||||
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
|
||||
|
||||
@@ -30,7 +30,11 @@
|
||||
}
|
||||
|
||||
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());
|
||||
} catch (err) {
|
||||
loginError = 'Network error - ' + err;
|
||||
|
||||
@@ -122,6 +122,11 @@
|
||||
<button class="p-2 w-full text-left" onclick={() => openNotificationPopup(notification)}>
|
||||
<div>
|
||||
<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">
|
||||
{new Date(notification.created_at).toLocaleString()}
|
||||
</p>
|
||||
|
||||
Reference in New Issue
Block a user