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) {
|
||||
const errorData = await response.json().catch(() => ({ error: 'Invalid username or password.' }));
|
||||
authStore.set({ token: undefined, error: errorData.error || 'Invalid username or password.' });
|
||||
console.log(response);
|
||||
const errorData = await response
|
||||
.json()
|
||||
.catch(() => ({ error: 'Invalid username or password.' }));
|
||||
authStore.set({
|
||||
token: undefined,
|
||||
error: errorData.error || 'Invalid username or password.'
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,48 @@
|
||||
import type { ServerInit } from '@sveltejs/kit';
|
||||
import type { Handle } from '@sveltejs/kit';
|
||||
import { redisSessionManager } from '$stores/redisSessionManager';
|
||||
import { env } from '$env/dynamic/private';
|
||||
import type Redis from 'ioredis';
|
||||
|
||||
const redisClient: Redis = redisSessionManager['redisClient'];
|
||||
export const init: ServerInit = async () => {
|
||||
console.log(redisClient.status);
|
||||
if (redisClient.status == 'connect') return;
|
||||
export const handle: Handle = async ({ event, resolve }) => {
|
||||
// Ensure redis is connected
|
||||
const redisClient: Redis = redisSessionManager['redisClient'];
|
||||
if (redisClient.status !== 'connect' && redisClient.status !== 'ready') {
|
||||
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">
|
||||
<h1 class="text-2xl font-bold">ACC Server Manager</h1>
|
||||
<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">
|
||||
<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">
|
||||
<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
|
||||
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>
|
||||
<span class="ml-1 hidden sm:inline">Users</span>
|
||||
</a>
|
||||
|
||||
Reference in New Issue
Block a user