Basic login page.

This commit is contained in:
2026-10-25 16:29:50 +01:00
parent bfacd2e302
commit 58621b0500
6 changed files with 118 additions and 2 deletions
+58
View File
@@ -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;
+12
View File
@@ -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)
)
};
};