Basic login page.
This commit is contained in:
@@ -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