fix account save
This commit is contained in:
parent
e6e4b068c9
commit
bce9a19776
@ -64,8 +64,10 @@ export async function login(email: string, password: string): Promise<User> {
|
||||
SELECT * FROM users
|
||||
WHERE email = ${email};
|
||||
`;
|
||||
console.log('1.25: ' + Date.now());
|
||||
if (user) {
|
||||
if (await bcrypt.compare(password, user.passwordHash!)) {
|
||||
console.log('1.5: ' + Date.now());
|
||||
delete user.passwordHash;
|
||||
|
||||
await sql`
|
||||
@ -73,6 +75,7 @@ export async function login(email: string, password: string): Promise<User> {
|
||||
SET last_sign_in = NOW()
|
||||
WHERE id = ${user.id};
|
||||
`;
|
||||
console.log('1.75: ' + Date.now());
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@ -4,21 +4,22 @@ import type { User, UserSettings } from '$lib/types/user';
|
||||
import { type Actions, error, fail } from '@sveltejs/kit';
|
||||
import { getFormString, getRequiredFormString } from '$lib/shared';
|
||||
import bcrypt from 'bcrypt';
|
||||
import { setJWTCookie } from '$lib/auth/index.server';
|
||||
|
||||
export const load: PageServerLoad = async ({ locals }) => {
|
||||
if (!locals || !locals.user) {
|
||||
throw Error('Need to be logged in!');
|
||||
}
|
||||
|
||||
const [userData]: User[] = await sql`
|
||||
const [userData] = await sql`
|
||||
SELECT * FROM users WHERE id = ${locals.user.id}
|
||||
`;
|
||||
|
||||
userData.settings = JSON.parse(userData.settings);
|
||||
return { userData };
|
||||
};
|
||||
|
||||
export const actions: Actions = {
|
||||
default: async ({ request, url, locals, params }) => {
|
||||
default: async ({ request, url, locals, params, cookies }) => {
|
||||
if (!locals || !locals.user) {
|
||||
throw error(403, 'Need to be logged in!');
|
||||
}
|
||||
@ -53,18 +54,17 @@ export const actions: Actions = {
|
||||
notifyAllTurnedInInquiries
|
||||
};
|
||||
|
||||
return await sql`
|
||||
const [res] = await sql`
|
||||
UPDATE users SET name = ${name},
|
||||
email = ${email},
|
||||
${passwordHash ? sql`password_hash = ${passwordHash},` : sql``}
|
||||
settings = ${settings.toString()}
|
||||
settings = ${JSON.stringify(settings)}
|
||||
WHERE id = ${locals.user.id}
|
||||
RETURNING *;
|
||||
`;
|
||||
res.settings = JSON.parse(res.settings);
|
||||
setJWTCookie(cookies, res as User);
|
||||
} catch (e) {
|
||||
console.log('fails');
|
||||
console.log(e);
|
||||
|
||||
return fail(400, {
|
||||
message: e instanceof Error ? e.message : 'Unknown error occurred',
|
||||
success: false
|
||||
|
||||
@ -10,8 +10,11 @@ export const actions = {
|
||||
const email = getRequiredFormString(data, 'email');
|
||||
const password = getRequiredFormString(data, 'password');
|
||||
|
||||
console.log('1: ' + Date.now());
|
||||
const user = await login(email, password);
|
||||
console.log('2: ' + Date.now());
|
||||
setJWTCookie(cookies, user);
|
||||
console.log('3: ' + Date.now());
|
||||
} catch (e) {
|
||||
return fail(400, { message: e instanceof Error ? e.message : 'Unknown error occurred' });
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user