2024-08-15 01:07:50 +00:00
|
|
|
import { fail, type Actions } from '@sveltejs/kit';
|
2024-07-15 03:59:51 +00:00
|
|
|
import { eq, or } from 'drizzle-orm';
|
2024-04-08 05:11:52 +00:00
|
|
|
import { Argon2id } from 'oslo/password';
|
2024-02-26 06:59:29 +00:00
|
|
|
import { zod } from 'sveltekit-superforms/adapters';
|
2023-05-21 05:18:04 +00:00
|
|
|
import { setError, superValidate } from 'sveltekit-superforms/server';
|
2023-07-30 23:31:39 +00:00
|
|
|
import { redirect } from 'sveltekit-flash-message/server';
|
2024-04-08 05:11:52 +00:00
|
|
|
import { RateLimiter } from 'sveltekit-rate-limiter/server';
|
2024-05-08 00:19:13 +00:00
|
|
|
import db from '../../../db';
|
2024-03-02 01:17:13 +00:00
|
|
|
import { lucia } from '$lib/server/auth';
|
2024-08-01 23:46:29 +00:00
|
|
|
import { twoFactor, usersTable, type Users } from '$db/schema';
|
2024-02-09 02:56:09 +00:00
|
|
|
import type { PageServerLoad } from './$types';
|
2024-08-15 01:07:50 +00:00
|
|
|
import {signinUsernameDto} from "$lib/dtos/signin-username.dto";
|
2023-05-21 05:18:04 +00:00
|
|
|
|
2023-11-05 06:20:34 +00:00
|
|
|
export const load: PageServerLoad = async (event) => {
|
2024-08-15 01:07:50 +00:00
|
|
|
const { locals } = event;
|
2024-06-17 20:06:45 +00:00
|
|
|
|
2024-08-15 01:07:50 +00:00
|
|
|
const authedUser = await locals.getAuthedUser();
|
|
|
|
|
|
|
|
|
|
if (authedUser) {
|
2024-03-02 01:17:13 +00:00
|
|
|
const message = { type: 'success', message: 'You are already signed in' } as const;
|
2023-12-05 06:25:43 +00:00
|
|
|
throw redirect('/', message, event);
|
2023-11-05 00:03:28 +00:00
|
|
|
}
|
2024-08-15 01:07:50 +00:00
|
|
|
|
|
|
|
|
// if (userFullyAuthenticated(user, session)) {
|
|
|
|
|
// const message = { type: 'success', message: 'You are already signed in' } as const;
|
|
|
|
|
// throw redirect('/', message, event);
|
|
|
|
|
// } else if (userNotFullyAuthenticated(user, session)) {
|
|
|
|
|
// await lucia.invalidateSession(locals.session!.id!);
|
|
|
|
|
// const sessionCookie = lucia.createBlankSessionCookie();
|
|
|
|
|
// cookies.set(sessionCookie.name, sessionCookie.value, {
|
|
|
|
|
// path: '.',
|
|
|
|
|
// ...sessionCookie.attributes,
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
const form = await superValidate(event, zod(signinUsernameDto));
|
2024-03-02 02:00:27 +00:00
|
|
|
|
2023-12-05 06:25:43 +00:00
|
|
|
return {
|
2024-04-08 05:11:52 +00:00
|
|
|
form,
|
2023-12-05 06:25:43 +00:00
|
|
|
};
|
2023-05-21 05:18:04 +00:00
|
|
|
};
|
|
|
|
|
|
2023-11-05 06:20:34 +00:00
|
|
|
export const actions: Actions = {
|
2023-05-21 05:18:04 +00:00
|
|
|
default: async (event) => {
|
2024-08-15 01:07:50 +00:00
|
|
|
// if (await limiter.isLimited(event)) {
|
|
|
|
|
// throw error(429);
|
|
|
|
|
// }
|
2024-03-12 18:34:39 +00:00
|
|
|
|
2023-12-20 01:54:39 +00:00
|
|
|
const { locals } = event;
|
2024-08-15 01:07:50 +00:00
|
|
|
|
|
|
|
|
const authedUser = await locals.getAuthedUser();
|
|
|
|
|
|
|
|
|
|
if (authedUser) {
|
|
|
|
|
const message = { type: 'success', message: 'You are already signed in' } as const;
|
|
|
|
|
throw redirect('/', message, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const form = await superValidate(event, zod(signinUsernameDto));
|
|
|
|
|
|
|
|
|
|
const { error } = await locals.api.login.$post({ json: form.data }).then(locals.parseApiResponse);
|
|
|
|
|
if (error) return setError(form, 'username', error);
|
2023-05-21 05:18:04 +00:00
|
|
|
|
|
|
|
|
if (!form.valid) {
|
2023-05-29 06:34:39 +00:00
|
|
|
form.data.password = '';
|
2023-05-21 05:18:04 +00:00
|
|
|
return fail(400, {
|
2024-04-08 05:11:52 +00:00
|
|
|
form,
|
2023-05-21 05:18:04 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 01:53:15 +00:00
|
|
|
let session;
|
|
|
|
|
let sessionCookie;
|
2024-08-01 23:46:29 +00:00
|
|
|
const user: Users | undefined = await db.query.usersTable.findFirst({
|
|
|
|
|
where: or(eq(usersTable.username, form.data.username), eq(usersTable.email, form.data.username)),
|
2024-06-09 00:25:01 +00:00
|
|
|
});
|
2023-06-16 06:28:49 +00:00
|
|
|
|
2024-06-12 02:12:12 +00:00
|
|
|
if (!user) {
|
|
|
|
|
form.data.password = '';
|
|
|
|
|
return setError(form, 'username', 'Your username or password is incorrect.');
|
|
|
|
|
}
|
2023-12-15 01:53:15 +00:00
|
|
|
|
2024-07-11 22:53:56 +00:00
|
|
|
let twoFactorDetails;
|
|
|
|
|
|
2024-06-09 00:25:01 +00:00
|
|
|
try {
|
|
|
|
|
const password = form.data.password;
|
2024-01-19 00:57:15 +00:00
|
|
|
console.log('user', JSON.stringify(user, null, 2));
|
|
|
|
|
|
2024-04-08 05:11:52 +00:00
|
|
|
if (!user?.hashed_password) {
|
2024-06-08 22:09:21 +00:00
|
|
|
console.log('invalid username/password');
|
2023-12-15 01:53:15 +00:00
|
|
|
form.data.password = '';
|
2024-06-08 22:09:21 +00:00
|
|
|
return setError(form, 'password', 'Your username or password is incorrect.');
|
2023-12-15 01:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const validPassword = await new Argon2id().verify(user.hashed_password, password);
|
|
|
|
|
if (!validPassword) {
|
2024-01-19 00:57:15 +00:00
|
|
|
console.log('invalid password');
|
2023-12-15 01:53:15 +00:00
|
|
|
form.data.password = '';
|
2024-06-08 22:09:21 +00:00
|
|
|
return setError(form, 'password', 'Your username or password is incorrect.');
|
2023-07-18 21:23:45 +00:00
|
|
|
}
|
2023-12-15 01:53:15 +00:00
|
|
|
|
2024-01-19 00:57:15 +00:00
|
|
|
console.log('ip', locals.ip);
|
|
|
|
|
console.log('country', locals.country);
|
2024-07-11 22:53:56 +00:00
|
|
|
|
|
|
|
|
twoFactorDetails = await db.query.twoFactor.findFirst({
|
|
|
|
|
where: eq(twoFactor.userId, user?.id),
|
2023-12-15 01:53:15 +00:00
|
|
|
});
|
2024-07-11 22:53:56 +00:00
|
|
|
|
|
|
|
|
if (twoFactorDetails?.secret && twoFactorDetails?.enabled) {
|
|
|
|
|
await db.update(twoFactor).set({
|
2024-07-13 00:44:45 +00:00
|
|
|
initiatedTime: new Date(),
|
2024-07-11 22:53:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
session = await lucia.createSession(user.id, {
|
|
|
|
|
ip_country: locals.country,
|
|
|
|
|
ip_address: locals.ip,
|
|
|
|
|
twoFactorAuthEnabled:
|
|
|
|
|
twoFactorDetails?.enabled &&
|
|
|
|
|
twoFactorDetails?.secret !== null &&
|
|
|
|
|
twoFactorDetails?.secret !== '',
|
|
|
|
|
isTwoFactorAuthenticated: false,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
session = await lucia.createSession(user.id, {
|
|
|
|
|
ip_country: locals.country,
|
|
|
|
|
ip_address: locals.ip,
|
|
|
|
|
twoFactorAuthEnabled: false,
|
|
|
|
|
isTwoFactorAuthenticated: false,
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-03-11 06:19:55 +00:00
|
|
|
console.log('logging in session', session);
|
2023-12-15 01:53:15 +00:00
|
|
|
sessionCookie = lucia.createSessionCookie(session.id);
|
2024-03-11 06:19:55 +00:00
|
|
|
console.log('logging in session cookie', sessionCookie);
|
2023-05-21 05:18:04 +00:00
|
|
|
} catch (e) {
|
|
|
|
|
// TODO: need to return error message to the client
|
|
|
|
|
console.error(e);
|
2023-05-29 06:34:39 +00:00
|
|
|
form.data.password = '';
|
2023-07-01 23:12:17 +00:00
|
|
|
return setError(form, '', 'Your username or password is incorrect.');
|
2023-05-21 05:18:04 +00:00
|
|
|
}
|
2023-12-20 01:54:39 +00:00
|
|
|
|
2024-03-12 18:34:39 +00:00
|
|
|
console.log('setting session cookie', sessionCookie);
|
2024-01-19 00:57:15 +00:00
|
|
|
event.cookies.set(sessionCookie.name, sessionCookie.value, {
|
2024-03-12 18:34:39 +00:00
|
|
|
path: '.',
|
2024-04-08 05:11:52 +00:00
|
|
|
...sessionCookie.attributes,
|
2024-01-19 00:57:15 +00:00
|
|
|
});
|
|
|
|
|
|
2023-05-29 06:34:39 +00:00
|
|
|
form.data.username = '';
|
|
|
|
|
form.data.password = '';
|
2024-06-09 00:25:01 +00:00
|
|
|
|
2024-06-12 02:12:12 +00:00
|
|
|
if (
|
2024-07-11 22:53:56 +00:00
|
|
|
twoFactorDetails?.enabled &&
|
|
|
|
|
twoFactorDetails?.secret !== null &&
|
|
|
|
|
twoFactorDetails?.secret !== ''
|
2024-06-12 02:12:12 +00:00
|
|
|
) {
|
2024-06-09 00:25:01 +00:00
|
|
|
console.log('redirecting to TOTP page');
|
|
|
|
|
const message = { type: 'success', message: 'Please enter your TOTP code.' } as const;
|
|
|
|
|
redirect(302, '/totp', message, event);
|
|
|
|
|
} else {
|
|
|
|
|
const message = { type: 'success', message: 'Signed In!' } as const;
|
|
|
|
|
redirect(302, '/', message, event);
|
|
|
|
|
}
|
2024-04-08 05:11:52 +00:00
|
|
|
},
|
2023-05-21 05:18:04 +00:00
|
|
|
};
|