diff --git a/src/api/apiService.ts b/src/api/apiService.ts index 6116c1a..0ab9874 100644 --- a/src/api/apiService.ts +++ b/src/api/apiService.ts @@ -44,7 +44,7 @@ export async function fetchAPIEvent( data: { token } } = await redisSessionManager.getSession(event.cookies); - return fetchAPI(endpoint, method, body, { Authorization: `Basic ${token}` }); + return fetchAPI(endpoint, method, body, { Authorization: `Bearer ${token}` }); } export default fetchAPI; diff --git a/src/api/authService.ts b/src/api/authService.ts index 4ca9b46..c9aa6b3 100644 --- a/src/api/authService.ts +++ b/src/api/authService.ts @@ -1,19 +1,35 @@ -import fetchAPI, { fetchAPIEvent } from '$api/apiService'; +import { fetchAPIEvent } from '$api/apiService'; +import { env } from '$env/dynamic/private'; import { authStore } from '$stores/authStore'; import { redisSessionManager } from '$stores/redisSessionManager'; import type { RequestEvent } from '@sveltejs/kit'; import { v4 as uuidv4 } from 'uuid'; export const login = async (event: RequestEvent, username: string, password: string) => { - const token = btoa(`${username}:${password}`); - await redisSessionManager.createSession(event.cookies, { token }, uuidv4()); - if (!(await checkAuth(event))) { - { - authStore.set({ token: undefined, error: 'Invalid username or password.' }); + try { + const response = await fetch(`${env.API_BASE_URL}/auth/login`, { + method: 'POST', + body: JSON.stringify({ username, password }), + headers: { + 'Content-Type': 'application/json' + } + }); + + 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.' }); return false; } + + const { token } = await response.json(); + + await redisSessionManager.createSession(event.cookies, { token }, uuidv4()); + + return true; + } catch (err) { + authStore.set({ token: undefined, error: 'Login failed. Please try again.' }); + return false; } - return true; }; export const logout = (event: RequestEvent) => { diff --git a/src/app.d.ts b/src/app.d.ts index da08e6d..ce6e612 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -3,7 +3,9 @@ declare global { namespace App { // interface Error {} - // interface Locals {} + interface Locals { + user: import('$models/user').User | null; + } // interface PageData {} // interface PageState {} // interface Platform {} diff --git a/src/models/user.ts b/src/models/user.ts new file mode 100644 index 0000000..b2810d3 --- /dev/null +++ b/src/models/user.ts @@ -0,0 +1,17 @@ +export interface Permission { + id: string; + name: string; +} + +export interface Role { + id: string; + name: string; + permissions: Permission[]; +} + +export interface User { + id: string; + username: string; + role_id: string; + role: Role; +} diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts new file mode 100644 index 0000000..ec238fd --- /dev/null +++ b/src/routes/+layout.server.ts @@ -0,0 +1,7 @@ +import type { LayoutServerLoad } from './$types'; + +export const load: LayoutServerLoad = async ({ locals }) => { + return { + user: locals.user + }; +}; diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 19926df..bfed911 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,6 +1,12 @@ -