Handle server authentication through fetch hook.
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
import { serverHostname, serverPort } from '#lib/data/index.ts';
|
||||||
|
import type { Cookies } from '@sveltejs/kit';
|
||||||
|
import type { Token } from '#lib/models/auth.ts';
|
||||||
|
import type { HandleFetch } from '@sveltejs/kit/hooks';
|
||||||
|
|
||||||
|
export const handleFetch: HandleFetch = async ({ event, request, fetch }) => {
|
||||||
|
if (isServerApiRequest(request)) {
|
||||||
|
const token = getTokenCookie(event.cookies);
|
||||||
|
if (token) setAuthorizationHeader(request.headers, token);
|
||||||
|
}
|
||||||
|
return fetch(request);
|
||||||
|
};
|
||||||
|
|
||||||
|
function isServerApiRequest(request: Request): boolean {
|
||||||
|
const url = new URL(request.url);
|
||||||
|
return (
|
||||||
|
url.hostname === serverHostname && url.port === serverPort && url.pathname.startsWith('/api')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTokenCookie(cookies: Cookies): Token | null {
|
||||||
|
const tokenJson = cookies.get('token');
|
||||||
|
return tokenJson ? JSON.parse(tokenJson) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setAuthorizationHeader(headers: Headers, token: Token): void {
|
||||||
|
headers.set('Authorization', `Bearer ${token.access_token}`);
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { serverBasePath } from '.';
|
import { serverBasePath } from '.';
|
||||||
import type { TokenRequest, TokenResponse } from '../models/auth';
|
import type { TokenRequest, Token } from '../models/auth';
|
||||||
|
|
||||||
export async function login(token: TokenRequest, f: typeof fetch): Promise<TokenResponse> {
|
export async function login(token: TokenRequest, f: typeof fetch): Promise<Token> {
|
||||||
return f(`${serverBasePath}/auth/token`, {
|
return f(`${serverBasePath}/auth/token`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
export const serverHostname = 'http://localhost';
|
export const serverHostname = 'localhost';
|
||||||
export const serverPort = 3000;
|
export const serverPort = '3000';
|
||||||
export const serverBasePath = `${serverHostname}:${serverPort}/api`;
|
export const serverBasePath = `http://${serverHostname}:${serverPort}/api`;
|
||||||
|
|||||||
@@ -4,9 +4,7 @@ export type ClientCredentials = {
|
|||||||
client_secret: string;
|
client_secret: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TokenRequest = ClientCredentials;
|
export type Token = {
|
||||||
|
|
||||||
export type TokenResponse = {
|
|
||||||
access_token: string;
|
access_token: string;
|
||||||
token_type: string;
|
token_type: string;
|
||||||
expires_in: number;
|
expires_in: number;
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
export function authenticatedFetch(token: string, f: typeof fetch): typeof fetch {
|
|
||||||
return (input: URL | RequestInfo, init?: RequestInit | undefined) => {
|
|
||||||
const headers = new Headers(init?.headers || {});
|
|
||||||
headers.set('Authorization', `Bearer ${token}`);
|
|
||||||
return f(input, { ...init, headers });
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,18 +1,8 @@
|
|||||||
import type { PageServerLoad } from './$types';
|
import type { PageServerLoad } from './$types';
|
||||||
import { listScheduledTransactions } from '#lib/data/scheduled-transactions.ts';
|
import { listScheduledTransactions } from '#lib/data/scheduled-transactions.ts';
|
||||||
import type { TokenResponse } from '#lib/models/auth.ts';
|
|
||||||
import { authenticatedFetch } from '#lib/utilities/fetch.ts';
|
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ cookies, fetch }) => {
|
|
||||||
const token = cookies.get('token');
|
|
||||||
if (!token) throw new Error('Not authenticated');
|
|
||||||
|
|
||||||
const parsedToken = JSON.parse(token) as TokenResponse;
|
|
||||||
if (!parsedToken.access_token) throw new Error('Not authenticated');
|
|
||||||
|
|
||||||
|
export const load: PageServerLoad = async ({ fetch }) => {
|
||||||
return {
|
return {
|
||||||
scheduledTransactions: listScheduledTransactions(
|
scheduledTransactions: listScheduledTransactions(fetch)
|
||||||
authenticatedFetch(parsedToken.access_token, fetch)
|
|
||||||
)
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user