Basic login page.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import { serverBasePath } from '.';
|
||||
import type { TokenRequest, TokenResponse } from '../models/auth';
|
||||
|
||||
export async function login(token: TokenRequest, f: typeof fetch): Promise<TokenResponse> {
|
||||
return f(`${serverBasePath}/auth/token`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: new URLSearchParams(token)
|
||||
}).then((res) => {
|
||||
if (!res.ok) throw new Error('Failed to login');
|
||||
return res.json();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
export type ClientCredentials = {
|
||||
grant_type: 'client_credentials';
|
||||
client_id: string;
|
||||
client_secret: string;
|
||||
};
|
||||
|
||||
export type TokenRequest = ClientCredentials;
|
||||
|
||||
export type TokenResponse = {
|
||||
access_token: string;
|
||||
token_type: string;
|
||||
expires_in: number;
|
||||
refresh_token?: string;
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
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 });
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { login } from '#lib/data/auth.ts';
|
||||
import type { Actions } from './$types';
|
||||
|
||||
export const actions = {
|
||||
default: async ({ request, fetch, cookies }) => {
|
||||
const data = await request.formData();
|
||||
const errors = [];
|
||||
|
||||
const clientId = data.get('clientId')?.toString().trim();
|
||||
if (!clientId) {
|
||||
errors.push('Client ID is required');
|
||||
}
|
||||
|
||||
const clientSecret = data.get('clientSecret')?.toString().trim();
|
||||
if (!clientSecret) {
|
||||
errors.push('Client Secret is required');
|
||||
}
|
||||
|
||||
if (errors.length > 0) {
|
||||
return {
|
||||
success: false,
|
||||
errors
|
||||
};
|
||||
}
|
||||
|
||||
if (errors.length > 0) {
|
||||
return {
|
||||
success: false,
|
||||
errors
|
||||
};
|
||||
}
|
||||
|
||||
const token = await login(
|
||||
{
|
||||
grant_type: 'client_credentials',
|
||||
client_id: clientId!.toString(),
|
||||
client_secret: clientSecret!.toString()
|
||||
},
|
||||
fetch
|
||||
);
|
||||
|
||||
if (errors.length > 0) {
|
||||
return {
|
||||
success: false,
|
||||
errors: ['Failed to login']
|
||||
};
|
||||
}
|
||||
|
||||
cookies.set('token', JSON.stringify(token), {
|
||||
httpOnly: true,
|
||||
path: '/'
|
||||
});
|
||||
|
||||
return {
|
||||
success: true
|
||||
};
|
||||
}
|
||||
} satisfies Actions;
|
||||
@@ -0,0 +1,12 @@
|
||||
<script lang="ts">
|
||||
</script>
|
||||
|
||||
<form method="post">
|
||||
<label for="client_id">Client ID</label>
|
||||
<input type="text" id="client_id" name="clientId" required />
|
||||
|
||||
<label for="client_secret">Client Secret</label>
|
||||
<input type="password" id="client_secret" name="clientSecret" required />
|
||||
|
||||
<button type="submit">Login</button>
|
||||
</form>
|
||||
@@ -1,8 +1,18 @@
|
||||
import type { PageServerLoad } from './$types';
|
||||
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 {
|
||||
scheduledTransactions: listScheduledTransactions(fetch)
|
||||
scheduledTransactions: listScheduledTransactions(
|
||||
authenticatedFetch(parsedToken.access_token, fetch)
|
||||
)
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user