2024-03-12 18:34:39 +00:00
|
|
|
import { fail, error, type Actions } from '@sveltejs/kit';
|
2024-02-26 06:59:29 +00:00
|
|
|
import { eq } from 'drizzle-orm';
|
2024-04-08 05:11:52 +00:00
|
|
|
import { Argon2id } from 'oslo/password';
|
|
|
|
|
import { decodeHex } from 'oslo/encoding';
|
|
|
|
|
import { TOTPController } from 'oslo/otp';
|
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-02-08 03:37:54 +00:00
|
|
|
import db from '$lib/drizzle';
|
2024-03-02 01:17:13 +00:00
|
|
|
import { lucia } from '$lib/server/auth';
|
2024-03-12 18:34:39 +00:00
|
|
|
import { signInSchema } from '$lib/validations/auth';
|
2024-02-08 03:37:54 +00:00
|
|
|
import { collections, users, wishlists } from '../../../schema';
|
2024-02-09 02:56:09 +00:00
|
|
|
import type { PageServerLoad } from './$types';
|
2023-05-21 05:18:04 +00:00
|
|
|
|
2023-11-05 06:20:34 +00:00
|
|
|
export const load: PageServerLoad = async (event) => {
|
2023-12-15 01:53:15 +00:00
|
|
|
if (event.locals.user) {
|
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
|
|
|
}
|
2023-12-15 01:53:15 +00:00
|
|
|
|
2024-03-02 02:00:27 +00:00
|
|
|
const form = await superValidate(event, zod(signInSchema));
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2024-03-12 18:34:39 +00:00
|
|
|
const limiter = new RateLimiter({
|
|
|
|
|
// A rate is defined by [number, unit]
|
2024-04-08 05:11:52 +00:00
|
|
|
IPUA: [5, 'm'],
|
2024-03-12 18:34:39 +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-03-12 18:34:39 +00:00
|
|
|
if (await limiter.isLimited(event)) {
|
|
|
|
|
throw error(429);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 01:54:39 +00:00
|
|
|
const { locals } = event;
|
2024-02-26 06:59:29 +00:00
|
|
|
const form = await superValidate(event, zod(signInSchema));
|
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;
|
2023-05-21 05:18:04 +00:00
|
|
|
try {
|
2023-12-15 01:53:15 +00:00
|
|
|
const password = form.data.password;
|
2023-06-16 06:28:49 +00:00
|
|
|
|
2024-02-08 03:37:54 +00:00
|
|
|
const user = await db.query.users.findFirst({
|
2024-04-08 05:11:52 +00:00
|
|
|
where: eq(users.username, form.data.username),
|
2023-06-16 06:28:49 +00:00
|
|
|
});
|
2023-12-15 01:53:15 +00:00
|
|
|
|
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) {
|
2023-12-15 01:53:15 +00:00
|
|
|
form.data.password = '';
|
|
|
|
|
return setError(form, '', 'Your username or password is incorrect.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 = '';
|
|
|
|
|
return setError(form, '', 'Your username or password is incorrect.');
|
2023-07-18 21:23:45 +00:00
|
|
|
}
|
2023-12-15 01:53:15 +00:00
|
|
|
|
2024-02-19 08:22:05 +00:00
|
|
|
await db
|
|
|
|
|
.insert(collections)
|
|
|
|
|
.values({
|
2024-04-08 05:11:52 +00:00
|
|
|
user_id: user.id,
|
2024-02-19 08:22:05 +00:00
|
|
|
})
|
|
|
|
|
.onConflictDoNothing();
|
|
|
|
|
await db
|
|
|
|
|
.insert(wishlists)
|
|
|
|
|
.values({
|
2024-04-08 05:11:52 +00:00
|
|
|
user_id: user.id,
|
2024-02-19 08:22:05 +00:00
|
|
|
})
|
|
|
|
|
.onConflictDoNothing();
|
|
|
|
|
|
2024-04-08 05:11:52 +00:00
|
|
|
if (user?.two_factor_enabled && user?.two_factor_secret && !form?.data?.totpToken) {
|
2024-04-09 00:47:54 +00:00
|
|
|
return fail(400, {
|
2024-04-08 05:11:52 +00:00
|
|
|
form,
|
2024-04-09 00:47:54 +00:00
|
|
|
twoFactorRequired: true,
|
|
|
|
|
});
|
2024-04-08 05:11:52 +00:00
|
|
|
} else if (user?.two_factor_enabled && user?.two_factor_secret && form?.data?.totpToken) {
|
|
|
|
|
console.log('totpToken', form.data.totpToken);
|
|
|
|
|
const validOTP = await new TOTPController().verify(
|
|
|
|
|
form.data.totpToken,
|
|
|
|
|
decodeHex(user.two_factor_secret)
|
|
|
|
|
);
|
|
|
|
|
console.log('validOTP', validOTP);
|
2024-04-09 00:47:54 +00:00
|
|
|
form.errors.totpToken = ['Invalid TOTP code'];
|
2024-04-08 05:11:52 +00:00
|
|
|
if (!validOTP) {
|
2024-04-09 00:47:54 +00:00
|
|
|
return fail(400, {
|
|
|
|
|
form,
|
|
|
|
|
twoFactorRequired: true,
|
|
|
|
|
});
|
2024-04-08 05:11:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-01-19 00:57:15 +00:00
|
|
|
console.log('ip', locals.ip);
|
|
|
|
|
console.log('country', locals.country);
|
2023-12-15 01:53:15 +00:00
|
|
|
session = await lucia.createSession(user.id, {
|
2024-01-19 00:57:15 +00:00
|
|
|
ip_country: locals.country,
|
2024-04-08 05:11:52 +00:00
|
|
|
ip_address: locals.ip,
|
2023-12-15 01:53:15 +00:00
|
|
|
});
|
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-03-02 01:17:13 +00:00
|
|
|
const message = { type: 'success', message: 'Signed In!' } as const;
|
2024-03-11 06:19:55 +00:00
|
|
|
redirect(302, '/', message, event);
|
2024-04-08 05:11:52 +00:00
|
|
|
},
|
2023-05-21 05:18:04 +00:00
|
|
|
};
|