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-05-21 05:18:04 +00:00
|
|
|
import { auth } from '$lib/server/lucia';
|
|
|
|
|
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-11-05 00:03:28 +00:00
|
|
|
try {
|
|
|
|
|
console.log('login load event', event);
|
|
|
|
|
const session = await event.locals.auth.validate();
|
|
|
|
|
if (session) {
|
|
|
|
|
const message = { type: 'info', message: 'You are already signed in' };
|
|
|
|
|
throw redirect('/', message, event);
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
form
|
|
|
|
|
};
|
|
|
|
|
} catch (e) {
|
|
|
|
|
fail(500, {
|
|
|
|
|
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-10-17 09:28:53 +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
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const key = await auth.useKey('username', form.data.username, form.data.password);
|
2023-07-30 05:00:51 +00:00
|
|
|
const session = await auth.createSession({
|
|
|
|
|
userId: key.userId,
|
|
|
|
|
attributes: {}
|
|
|
|
|
});
|
2023-05-21 05:18:04 +00:00
|
|
|
event.locals.auth.setSession(session);
|
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-07-30 23:31:39 +00:00
|
|
|
id: session.user.userId
|
2023-06-16 06:28:49 +00:00
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
roles: {
|
|
|
|
|
select: {
|
|
|
|
|
role: true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-18 21:23:45 +00:00
|
|
|
if (user) {
|
2023-11-05 00:03:28 +00:00
|
|
|
await prisma.collection.upsert({
|
2023-07-18 21:23:45 +00:00
|
|
|
where: {
|
|
|
|
|
user_id: user.id
|
|
|
|
|
},
|
|
|
|
|
create: {
|
|
|
|
|
user_id: user.id
|
|
|
|
|
},
|
|
|
|
|
update: {
|
|
|
|
|
user_id: user.id
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-11-05 00:03:28 +00:00
|
|
|
await prisma.wishlist.upsert({
|
2023-07-18 21:23:45 +00:00
|
|
|
where: {
|
|
|
|
|
user_id: user.id
|
|
|
|
|
},
|
|
|
|
|
create: {
|
2023-08-14 05:12:02 +00:00
|
|
|
user_id: user.id
|
2023-07-18 21:23:45 +00:00
|
|
|
},
|
|
|
|
|
update: {
|
2023-08-14 05:12:02 +00:00
|
|
|
user_id: user.id
|
2023-07-18 21:23:45 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
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-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
|
|
|
}
|
|
|
|
|
};
|