Add cookie parsing part of apiFetch and add optional cookies parameter in login function
This commit is contained in:
parent
00d7ac15a2
commit
8bd291beec
|
@ -1,5 +1,61 @@
|
||||||
// src/lib/api.ts
|
// src/lib/api.ts
|
||||||
|
|
||||||
|
import type { Cookies } from "@sveltejs/kit";
|
||||||
|
|
||||||
|
// Cookie Helpers {{{
|
||||||
|
//
|
||||||
|
interface CookieOptions {
|
||||||
|
httpOnly: boolean;
|
||||||
|
secure: boolean;
|
||||||
|
sameSite: 'strict' | 'lax' | 'none';
|
||||||
|
maxAge: number;
|
||||||
|
path: string;
|
||||||
|
domain?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to parse set-cookie header
|
||||||
|
function parseSetCookieHeader(setCookieHeader: string) {
|
||||||
|
const [cookie, ...attributes] = setCookieHeader.split(';').map(part => part.trim());
|
||||||
|
const [name, value] = cookie.split('=');
|
||||||
|
|
||||||
|
const options: CookieOptions = {
|
||||||
|
httpOnly: false,
|
||||||
|
secure: process.env.NODE_ENV === 'production',
|
||||||
|
sameSite: 'strict',
|
||||||
|
path: '/',
|
||||||
|
maxAge: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
attributes.forEach(attr => {
|
||||||
|
const [key, val] = attr.split('=');
|
||||||
|
|
||||||
|
switch (key.toLowerCase()) {
|
||||||
|
case 'httponly':
|
||||||
|
options.httpOnly = true;
|
||||||
|
break;
|
||||||
|
case 'secure':
|
||||||
|
options.secure = true;
|
||||||
|
break;
|
||||||
|
case 'samesite':
|
||||||
|
options.sameSite = val?.toLowerCase() as 'strict' | 'lax' | 'none';
|
||||||
|
break;
|
||||||
|
case 'max-age':
|
||||||
|
options.maxAge = parseInt(val, 10);
|
||||||
|
break;
|
||||||
|
case 'path':
|
||||||
|
options.path = val || '/';
|
||||||
|
break;
|
||||||
|
case 'domain':
|
||||||
|
options.domain = val;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return { name, value, options };
|
||||||
|
}
|
||||||
|
// }}}
|
||||||
|
//
|
||||||
|
|
||||||
// Define the base URL of your API
|
// Define the base URL of your API
|
||||||
const BASE_API_URL = import.meta.env.VITE_API_BASE_URL_SERVER || 'http://localhost:3111/api';
|
const BASE_API_URL = import.meta.env.VITE_API_BASE_URL_SERVER || 'http://localhost:3111/api';
|
||||||
|
|
||||||
|
@ -18,7 +74,11 @@ interface UserData {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generic function for making API requests
|
// Generic function for making API requests
|
||||||
export async function apiFetch<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
|
export async function apiFetch<T>(
|
||||||
|
endpoint: string,
|
||||||
|
options: RequestInit = {},
|
||||||
|
cookies?: Cookies,
|
||||||
|
): Promise<T> {
|
||||||
const url = `${BASE_API_URL}${endpoint}`;
|
const url = `${BASE_API_URL}${endpoint}`;
|
||||||
console.log("Fetching ", url)
|
console.log("Fetching ", url)
|
||||||
|
|
||||||
|
@ -34,6 +94,17 @@ export async function apiFetch<T>(endpoint: string, options: RequestInit = {}):
|
||||||
try {
|
try {
|
||||||
const response = await fetch(url, config);
|
const response = await fetch(url, config);
|
||||||
|
|
||||||
|
console.log(response.headers);
|
||||||
|
if (response.headers) {
|
||||||
|
const setCookieHeader = response.headers.get("Set-Cookie");
|
||||||
|
if (setCookieHeader) {
|
||||||
|
const parsedCookie = parseSetCookieHeader(setCookieHeader);
|
||||||
|
if (parsedCookie) {
|
||||||
|
cookies?.set(parsedCookie.name, parsedCookie.value, parsedCookie.options)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorData = await response.json(); // Parse error response as JSON
|
const errorData = await response.json(); // Parse error response as JSON
|
||||||
const errorMessage = errorData.message || `Error: ${response.status}`;
|
const errorMessage = errorData.message || `Error: ${response.status}`;
|
||||||
|
@ -48,11 +119,11 @@ export async function apiFetch<T>(endpoint: string, options: RequestInit = {}):
|
||||||
}
|
}
|
||||||
|
|
||||||
// Login function with type annotation for the response
|
// Login function with type annotation for the response
|
||||||
export async function login(username: string, password: string): Promise<LoginResponse> {
|
export async function login(username: string, password: string, cookies?: Cookies): Promise<LoginResponse> {
|
||||||
return apiFetch<LoginResponse>('/user/login', {
|
return apiFetch<LoginResponse>('/user/login', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ username, password }),
|
body: JSON.stringify({ username, password }),
|
||||||
});
|
}, cookies);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get user data function with type annotation for the response
|
// Get user data function with type annotation for the response
|
||||||
|
|
Loading…
Reference in New Issue
Block a user