add session manager

This commit is contained in:
Fran Jurmanović
2025-02-10 18:35:10 +01:00
parent 3ad4b95656
commit 9557aeb755
8 changed files with 299 additions and 9 deletions

View File

@@ -1,8 +1,9 @@
import { authStore } from '$stores/authStore';
import { redirect } from '@sveltejs/kit';
import type { RequestEvent } from '../routes/$types';
import { redisSessionManager } from '$stores/redisSessionManager';
import { API_BASE_URL } from '$env/static/private';
const BASE_URL = import.meta.env.VITE_API_BASE_URL || 'https://acc-api.jurmanovic.com/v1';
const BASE_URL = API_BASE_URL;
async function fetchAPI(endpoint: string, method: string = 'GET', body?: object, hdr?: object) {
const headers = {
@@ -39,7 +40,9 @@ export async function fetchAPIEvent(
method: string = 'GET',
body?: object
) {
const token = event.cookies.get('token');
const {
data: { token }
} = await redisSessionManager.getSession(event.cookies);
return fetchAPI(endpoint, method, body, { Authorization: `Basic ${token}` });
}

View File

@@ -1,10 +1,11 @@
import fetchAPI, { fetchAPIEvent } from '$api/apiService';
import { authStore } from '$stores/authStore';
import type { RequestEvent } from '../routes/$types';
import { redisSessionManager } from '$stores/redisSessionManager';
import { v4 as uuidv4 } from 'uuid';
export const login = async (event: object, username: string, password: string) => {
const token = btoa(`${username}:${password}`);
event.cookies.set('token', token, { path: '/' });
await redisSessionManager.createSession(event.cookies, { token }, uuidv4());
if (!(await checkAuth(event))) {
{
authStore.set({ token: undefined, error: 'Invalid username or password.' });
@@ -15,7 +16,7 @@ export const login = async (event: object, username: string, password: string) =
};
export const logout = (event) => {
event.cookies.delete('token', { path: '/' });
return redisSessionManager.deleteCookie(event.cookies);
};
export const checkAuth = async (event: object) => {