import { checkUserCreds, updateLastSignin } from '$lib/db/index.server'; import { fail, redirect, type Actions } from '@sveltejs/kit'; import * as dotenv from 'dotenv'; import { setJWT } from '$lib/shared.server'; import type { User } from '$lib/types'; dotenv.config({ path: '.env' }); export const actions: Actions = { signin: async ({ request, cookies }) => { const data = await request.formData(); const username = data.get('username')?.toString().trim(); const password = data.get('password')?.toString().trim(); if (username && password && username !== '' && password !== '') { const user: User | null = await checkUserCreds(username, password); if (!user) { return fail(400, { errorMessage: 'Invalid username or password' }); } if (!user.active) { return fail(400, { errorMessage: 'Account is disabled. Please contact your admin if you think this is a mistake.' }); } setJWT(cookies, user); await updateLastSignin(username); // redirect to home page throw redirect(303, '/account'); } else { return fail(400, { errorMessage: 'Missing username or password' }); } } };