2023-12-20 01:54:39 +00:00
|
|
|
import { fail, type Actions } from '@sveltejs/kit';
|
2024-02-18 08:03:08 +00:00
|
|
|
import { eq } from 'drizzle-orm';
|
2024-02-26 06:59:29 +00:00
|
|
|
import { zod } from 'sveltekit-superforms/adapters';
|
2023-08-04 23:40:22 +00:00
|
|
|
import { message, setError, superValidate } from 'sveltekit-superforms/server';
|
2023-12-20 01:54:39 +00:00
|
|
|
import { redirect } from 'sveltekit-flash-message/server';
|
2024-02-26 16:41:24 +00:00
|
|
|
import { changeEmailSchema, profileSchema } from '$lib/validations/account';
|
2023-11-05 06:20:34 +00:00
|
|
|
import type { PageServerLoad } from './$types';
|
2024-02-18 08:03:08 +00:00
|
|
|
import { users } from '../../../../schema';
|
|
|
|
|
import db from '$lib/drizzle';
|
2023-08-04 23:40:22 +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) {
|
2023-12-20 01:54:39 +00:00
|
|
|
const message = { type: 'error', message: 'You are not signed in' } as const;
|
|
|
|
|
throw redirect(302, '/login', message, event);
|
2023-08-04 23:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-15 01:53:15 +00:00
|
|
|
const { user } = event.locals;
|
2023-08-04 23:40:22 +00:00
|
|
|
|
2024-02-28 06:49:21 +00:00
|
|
|
const dbUser = await db.query
|
|
|
|
|
.users
|
|
|
|
|
.findFirst({
|
|
|
|
|
where: eq(users.id, user.id)
|
|
|
|
|
});
|
|
|
|
|
|
2024-02-26 16:41:24 +00:00
|
|
|
const profileForm = await superValidate(zod(profileSchema), {
|
|
|
|
|
defaults: {
|
2024-02-28 06:49:21 +00:00
|
|
|
firstName: dbUser?.first_name || '',
|
|
|
|
|
lastName: dbUser?.last_name || '',
|
|
|
|
|
username: dbUser?.username || '',
|
2024-02-26 16:41:24 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const emailForm = await superValidate(zod(changeEmailSchema), {
|
|
|
|
|
defaults: {
|
2024-02-28 06:49:21 +00:00
|
|
|
email: dbUser?.email || '',
|
2024-02-26 16:41:24 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-04 23:40:22 +00:00
|
|
|
return {
|
2024-02-26 16:41:24 +00:00
|
|
|
profileForm,
|
|
|
|
|
emailForm,
|
2023-08-04 23:40:22 +00:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-05 06:20:34 +00:00
|
|
|
export const actions: Actions = {
|
2024-02-28 06:49:21 +00:00
|
|
|
profileUpdate: async (event) => {
|
2024-02-26 06:59:29 +00:00
|
|
|
const form = await superValidate(event, zod(profileSchema));
|
2023-08-04 23:40:22 +00:00
|
|
|
|
|
|
|
|
if (!form.valid) {
|
|
|
|
|
return fail(400, {
|
|
|
|
|
form
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-12-15 01:53:15 +00:00
|
|
|
if (!event.locals.user) {
|
|
|
|
|
throw redirect(302, '/login');
|
|
|
|
|
}
|
2023-08-04 23:40:22 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
console.log('updating profile');
|
|
|
|
|
|
2023-12-15 01:53:15 +00:00
|
|
|
const user = event.locals.user;
|
2023-08-04 23:40:22 +00:00
|
|
|
|
2024-02-18 08:03:08 +00:00
|
|
|
const newUsername = form.data.username;
|
2024-02-27 17:27:26 +00:00
|
|
|
const existingUser = await db.query
|
|
|
|
|
.users
|
|
|
|
|
.findFirst({
|
|
|
|
|
where: eq(users.username, newUsername)
|
|
|
|
|
}
|
|
|
|
|
);
|
2024-02-18 08:03:08 +00:00
|
|
|
|
|
|
|
|
if (existingUser && existingUser.id !== user.id) {
|
|
|
|
|
return setError(form, 'username', 'That username is already taken');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await db
|
|
|
|
|
.update(users)
|
|
|
|
|
.set({
|
|
|
|
|
first_name: form.data.firstName,
|
|
|
|
|
last_name: form.data.lastName,
|
2023-12-15 01:53:15 +00:00
|
|
|
username: form.data.username
|
2024-02-18 08:03:08 +00:00
|
|
|
})
|
|
|
|
|
.where(eq(users.id, user.id));
|
2023-08-04 23:40:22 +00:00
|
|
|
} catch (e) {
|
2023-12-15 01:53:15 +00:00
|
|
|
if (e.message === `AUTH_INVALID_USER_ID`) {
|
2023-08-04 23:40:22 +00:00
|
|
|
// invalid user id
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
|
|
|
|
return setError(form, 'There was a problem updating your profile.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('profile updated successfully');
|
2024-02-28 06:49:21 +00:00
|
|
|
return message(form, { type: 'success', message: 'Profile updated successfully!' });
|
2024-02-27 17:27:26 +00:00
|
|
|
},
|
|
|
|
|
changeEmail: async (event) => {
|
|
|
|
|
const form = await superValidate(event, zod(changeEmailSchema));
|
|
|
|
|
|
|
|
|
|
const newEmail = form.data?.email;
|
|
|
|
|
if (!form.valid || !newEmail || newEmail === '') {
|
|
|
|
|
return fail(400, {
|
|
|
|
|
form
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!event.locals.user) {
|
|
|
|
|
throw redirect(302, '/login');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const user = event.locals.user;
|
|
|
|
|
const existingUser = await db.query.users.findFirst({
|
|
|
|
|
where: eq(users.email, newEmail)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (existingUser && existingUser.id !== user.id) {
|
2024-02-28 06:49:21 +00:00
|
|
|
return setError(form, 'email', { type: 'error', message: 'That email is already taken' });
|
2024-02-27 17:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await db
|
|
|
|
|
.update(users)
|
|
|
|
|
.set({ email: form.data.email })
|
|
|
|
|
.where(eq(users.id, user.id));
|
|
|
|
|
|
|
|
|
|
if (user.email !== form.data.email) {
|
|
|
|
|
// Send email to confirm new email?
|
|
|
|
|
// auth.update
|
|
|
|
|
// await locals.prisma.key.update({
|
|
|
|
|
// where: {
|
|
|
|
|
// id: 'emailpassword:' + user.email
|
|
|
|
|
// },
|
|
|
|
|
// data: {
|
|
|
|
|
// id: 'emailpassword:' + form.data.email
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// auth.updateUserAttributes(user.user_id, {
|
|
|
|
|
// receiveEmail: false
|
|
|
|
|
// });
|
|
|
|
|
}
|
2024-02-28 06:49:21 +00:00
|
|
|
|
|
|
|
|
return message(form, { type: 'success', message: 'Email updated successfully!' });
|
2023-08-04 23:40:22 +00:00
|
|
|
}
|
|
|
|
|
};
|