2023-11-05 06:20:34 +00:00
|
|
|
import { fail, type Actions } from '@sveltejs/kit';
|
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';
|
2023-11-05 00:03:28 +00:00
|
|
|
import prisma from '$lib/prisma';
|
2023-12-15 01:53:15 +00:00
|
|
|
import { lucia } from '$lib/server/auth';
|
|
|
|
|
import { Argon2id } from 'oslo/password';
|
2023-05-21 05:18:04 +00:00
|
|
|
import { userSchema } from '$lib/config/zod-schemas';
|
2023-11-05 06:20:34 +00:00
|
|
|
import type { PageServerLoad } from './$types';
|
2023-05-21 05:18:04 +00:00
|
|
|
|
|
|
|
|
const signInSchema = userSchema.pick({
|
|
|
|
|
username: true,
|
|
|
|
|
password: true
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-05 06:20:34 +00:00
|
|
|
export const load: PageServerLoad = async (event) => {
|
2023-06-16 06:28:49 +00:00
|
|
|
const form = await superValidate(event, signInSchema);
|
2023-12-05 06:25:43 +00:00
|
|
|
|
|
|
|
|
console.log('login load event', event);
|
2023-12-15 01:53:15 +00:00
|
|
|
if (event.locals.user) {
|
2023-12-05 06:25:43 +00:00
|
|
|
const message = { type: 'info', message: 'You are already signed in' } as const;
|
|
|
|
|
throw redirect('/', message, event);
|
2023-11-05 00:03:28 +00:00
|
|
|
}
|
2023-12-15 01:53:15 +00:00
|
|
|
|
2023-12-05 06:25:43 +00:00
|
|
|
return {
|
|
|
|
|
form
|
|
|
|
|
};
|
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) => {
|
2023-12-20 01:54:39 +00:00
|
|
|
const { locals } = event;
|
2023-05-21 05:18:04 +00:00
|
|
|
const form = await superValidate(event, signInSchema);
|
|
|
|
|
|
|
|
|
|
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, {
|
|
|
|
|
form
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2023-11-05 00:03:28 +00:00
|
|
|
const user = await prisma.user.findUnique({
|
2023-06-16 06:28:49 +00:00
|
|
|
where: {
|
2023-12-15 01:53:15 +00:00
|
|
|
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));
|
|
|
|
|
|
2023-12-15 01:53:15 +00:00
|
|
|
if (!user || !user.hashed_password) {
|
|
|
|
|
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-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,
|
|
|
|
|
ip_address: locals.ip
|
2023-12-15 01:53:15 +00:00
|
|
|
});
|
|
|
|
|
sessionCookie = lucia.createSessionCookie(session.id);
|
|
|
|
|
|
|
|
|
|
await prisma.collection.upsert({
|
|
|
|
|
where: {
|
|
|
|
|
user_id: user.id
|
|
|
|
|
},
|
|
|
|
|
create: {
|
|
|
|
|
user_id: user.id
|
|
|
|
|
},
|
|
|
|
|
update: {
|
|
|
|
|
user_id: user.id
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
await prisma.wishlist.upsert({
|
|
|
|
|
where: {
|
|
|
|
|
user_id: user.id
|
|
|
|
|
},
|
|
|
|
|
create: {
|
|
|
|
|
user_id: user.id
|
|
|
|
|
},
|
|
|
|
|
update: {
|
|
|
|
|
user_id: user.id
|
|
|
|
|
}
|
|
|
|
|
});
|
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-01-19 00:57:15 +00:00
|
|
|
event.cookies.set(sessionCookie.name, sessionCookie.value, {
|
|
|
|
|
path: ".",
|
|
|
|
|
...sessionCookie.attributes
|
|
|
|
|
});
|
|
|
|
|
|
2023-05-29 06:34:39 +00:00
|
|
|
form.data.username = '';
|
|
|
|
|
form.data.password = '';
|
2023-07-30 23:31:39 +00:00
|
|
|
const message = { type: 'success', message: 'Signed In!' };
|
|
|
|
|
// return { form, message };
|
|
|
|
|
throw redirect('/', message, event);
|
2023-05-21 05:18:04 +00:00
|
|
|
}
|
|
|
|
|
};
|