fix user data fetch
This commit is contained in:
@@ -16,8 +16,14 @@ export const login = async (event: RequestEvent, username: string, password: str
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorData = await response.json().catch(() => ({ error: 'Invalid username or password.' }));
|
console.log(response);
|
||||||
authStore.set({ token: undefined, error: errorData.error || 'Invalid username or password.' });
|
const errorData = await response
|
||||||
|
.json()
|
||||||
|
.catch(() => ({ error: 'Invalid username or password.' }));
|
||||||
|
authStore.set({
|
||||||
|
token: undefined,
|
||||||
|
error: errorData.error || 'Invalid username or password.'
|
||||||
|
});
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,48 @@
|
|||||||
import type { ServerInit } from '@sveltejs/kit';
|
import type { Handle } from '@sveltejs/kit';
|
||||||
import { redisSessionManager } from '$stores/redisSessionManager';
|
import { redisSessionManager } from '$stores/redisSessionManager';
|
||||||
|
import { env } from '$env/dynamic/private';
|
||||||
import type Redis from 'ioredis';
|
import type Redis from 'ioredis';
|
||||||
|
|
||||||
const redisClient: Redis = redisSessionManager['redisClient'];
|
export const handle: Handle = async ({ event, resolve }) => {
|
||||||
export const init: ServerInit = async () => {
|
// Ensure redis is connected
|
||||||
console.log(redisClient.status);
|
const redisClient: Redis = redisSessionManager['redisClient'];
|
||||||
if (redisClient.status == 'connect') return;
|
if (redisClient.status !== 'connect' && redisClient.status !== 'ready') {
|
||||||
await redisClient.connect();
|
try {
|
||||||
|
await redisClient.connect();
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Redis connection failed', err);
|
||||||
|
// We can still continue without a user session, but log the error.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get session from cookie
|
||||||
|
const session = await redisSessionManager.getSession(event.cookies);
|
||||||
|
|
||||||
|
if (session && session.data && session.data.token) {
|
||||||
|
try {
|
||||||
|
// Fetch user data from /api/me
|
||||||
|
const response = await fetch(`${env.API_BASE_URL}/auth/me`, {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${session.data.token}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
const user = await response.json();
|
||||||
|
event.locals.user = user;
|
||||||
|
} else {
|
||||||
|
console.log(await response.text(), response.status);
|
||||||
|
// Token might be invalid/expired, clear it
|
||||||
|
event.locals.user = null;
|
||||||
|
await redisSessionManager.deleteCookie(event.cookies);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to fetch user:', error);
|
||||||
|
event.locals.user = null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
event.locals.user = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolve(event);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -17,10 +17,21 @@
|
|||||||
<div class="mx-auto flex max-w-7xl items-center justify-between px-4 py-4 sm:px-6 lg:px-8">
|
<div class="mx-auto flex max-w-7xl items-center justify-between px-4 py-4 sm:px-6 lg:px-8">
|
||||||
<h1 class="text-2xl font-bold">ACC Server Manager</h1>
|
<h1 class="text-2xl font-bold">ACC Server Manager</h1>
|
||||||
<div class="flex items-center space-x-4">
|
<div class="flex items-center space-x-4">
|
||||||
{#if hasPermission($user, 'membership.view')}
|
{#if false && hasPermission($user, 'membership.view')}
|
||||||
<a href="/dashboard/membership" class="flex items-center text-gray-300 hover:text-white">
|
<a href="/dashboard/membership" class="flex items-center text-gray-300 hover:text-white">
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
|
<svg
|
||||||
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M15 21a6 6 0 00-9-5.197m0 0A5.975 5.975 0 0112 13a5.975 5.975 0 01-3-1.197" />
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
class="h-6 w-6"
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
stroke="currentColor"
|
||||||
|
stroke-width="2"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
stroke-linecap="round"
|
||||||
|
stroke-linejoin="round"
|
||||||
|
d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M15 21a6 6 0 00-9-5.197m0 0A5.975 5.975 0 0112 13a5.975 5.975 0 01-3-1.197"
|
||||||
|
/>
|
||||||
</svg>
|
</svg>
|
||||||
<span class="ml-1 hidden sm:inline">Users</span>
|
<span class="ml-1 hidden sm:inline">Users</span>
|
||||||
</a>
|
</a>
|
||||||
|
|||||||
Reference in New Issue
Block a user