Currently you have two factor authentication enabled
To disable two factor authentication, please enter your current password.
- Current Password
@@ -51,7 +51,7 @@
{:else}
Please scan the following QR Code
- Enter Code
diff --git a/src/routes/(app)/(protected)/profile/security/two-factor/recovery-codes/+page.server.ts b/src/routes/(app)/(protected)/profile/security/mfa/recovery-codes/+page.server.ts
similarity index 72%
rename from src/routes/(app)/(protected)/profile/security/two-factor/recovery-codes/+page.server.ts
rename to src/routes/(app)/(protected)/profile/security/mfa/recovery-codes/+page.server.ts
index fb8b009..b14c28e 100644
--- a/src/routes/(app)/(protected)/profile/security/two-factor/recovery-codes/+page.server.ts
+++ b/src/routes/(app)/(protected)/profile/security/mfa/recovery-codes/+page.server.ts
@@ -1,31 +1,36 @@
-import db from '../../../../../../../db';
import { eq } from 'drizzle-orm';
import { Argon2id } from 'oslo/password';
import { alphabet, generateRandomString } from 'oslo/crypto';
import { redirect } from 'sveltekit-flash-message/server';
+import { db } from '$lib/server/api/infrastructure/database';
import { notSignedInMessage } from '$lib/flashMessages';
import type { PageServerLoad } from '../../../$types';
-import {recoveryCodes, twoFactor, usersTable} from '$db/schema';
+import { recoveryCodesTable, twoFactorTable, usersTable} from '$lib/server/api/infrastructure/database/tables';
import { userNotAuthenticated } from '$lib/server/auth-utils';
export const load: PageServerLoad = async (event) => {
const { locals } = event;
- const { user, session } = locals;
- if (userNotAuthenticated(user, session)) {
- redirect(302, '/login', notSignedInMessage, event);
+
+ const authedUser = await locals.getAuthedUser();
+ if (!authedUser) {
+ throw redirect(302, '/login', notSignedInMessage, event);
}
const dbUser = await db.query.usersTable.findFirst({
- where: eq(usersTable.id, user!.id),
+ where: eq(usersTable.id, authedUser.id),
});
+ if (!dbUser) {
+ throw redirect(302, '/login', notSignedInMessage, event);
+ }
+
const twoFactorDetails = await db.query.twoFactor.findFirst({
- where: eq(twoFactor.userId, dbUser!.id),
+ where: eq(twoFactor.userId, dbUser.id),
});
if (twoFactorDetails?.enabled) {
const dbRecoveryCodes = await db.query.recoveryCodes.findMany({
- where: eq(recoveryCodes.userId, user!.id),
+ where: eq(recoveryCodes.userId, authedUser.id),
});
if (dbRecoveryCodes.length === 0) {
@@ -37,7 +42,7 @@ export const load: PageServerLoad = async (event) => {
const hashedCode = await new Argon2id().hash(code);
console.log('Inserting recovery code', code, hashedCode);
await db.insert(recoveryCodes).values({
- userId: user!.id,
+ userId: authedUser.id,
code: hashedCode,
});
}
diff --git a/src/routes/(app)/(protected)/profile/security/two-factor/recovery-codes/+page.svelte b/src/routes/(app)/(protected)/profile/security/mfa/recovery-codes/+page.svelte
similarity index 100%
rename from src/routes/(app)/(protected)/profile/security/two-factor/recovery-codes/+page.svelte
rename to src/routes/(app)/(protected)/profile/security/mfa/recovery-codes/+page.svelte
diff --git a/src/routes/(app)/(protected)/profile/security/password/change/+page.server.ts b/src/routes/(app)/(protected)/profile/security/password/change/+page.server.ts
index ae0168f..ed158f6 100644
--- a/src/routes/(app)/(protected)/profile/security/password/change/+page.server.ts
+++ b/src/routes/(app)/(protected)/profile/security/password/change/+page.server.ts
@@ -5,22 +5,22 @@ import { setError, superValidate } from 'sveltekit-superforms/server';
import { redirect } from 'sveltekit-flash-message/server';
import { Argon2id } from 'oslo/password';
import type { PageServerLoad } from '../../../$types';
-import db from '../../../../../../../db';
+import { db } from '$lib/server/api/infrastructure/database';
import { changeUserPasswordSchema } from '$lib/validations/account';
-import { lucia } from '$lib/server/auth.js';
-import { usersTable } from '$db/schema';
+import { usersTable } from '$lib/server/api/infrastructure/database/tables';
import { notSignedInMessage } from '$lib/flashMessages';
import type { Cookie } from 'lucia';
-import { userNotAuthenticated } from '$lib/server/auth-utils';
export const load: PageServerLoad = async (event) => {
- const form = await superValidate(event, zod(changeUserPasswordSchema));
const { locals } = event;
- const { user, session } = locals;
- if (userNotAuthenticated(user, session)) {
- redirect(302, '/login', notSignedInMessage, event);
+
+ const authedUser = await locals.getAuthedUser();
+ if (!authedUser) {
+ throw redirect(302, '/login', notSignedInMessage, event);
}
+ const form = await superValidate(event, zod(changeUserPasswordSchema));
+
form.data = {
current_password: '',
password: '',
@@ -34,9 +34,10 @@ export const load: PageServerLoad = async (event) => {
export const actions: Actions = {
default: async (event) => {
const { locals } = event;
- const { user, session } = locals;
- if (userNotAuthenticated(user, session)) {
- return fail(401);
+
+ const authedUser = await locals.getAuthedUser();
+ if (!authedUser) {
+ throw redirect(302, '/login', notSignedInMessage, event);
}
const form = await superValidate(event, zod(changeUserPasswordSchema));
@@ -57,7 +58,7 @@ export const actions: Actions = {
}
const dbUser = await db.query.usersTable.findFirst({
- where: eq(usersTable.id, user!.id),
+ where: eq(usersTable.id, authedUser.id),
});
// if (!dbUser?.hashed_password) {
@@ -78,14 +79,14 @@ export const actions: Actions = {
if (!currentPasswordVerified) {
return setError(form, 'current_password', 'Your password is incorrect');
}
- if (user?.username) {
+ if (authedUser?.username) {
let sessionCookie: Cookie;
try {
if (form.data.password !== form.data.confirm_password) {
return setError(form, 'Password and confirm password do not match');
}
const hashedPassword = await new Argon2id().hash(form.data.password);
- await lucia.invalidateUserSessions(user.id);
+ await lucia.invalidateUserSessions(authedUser.id);
// await db
// .update(usersTable)
// .set({ hashed_password: hashedPassword })
diff --git a/src/routes/(app)/game/[id]/+page.svelte b/src/routes/(app)/game/[id]/+page.svelte
index 354689f..5edca30 100644
--- a/src/routes/(app)/game/[id]/+page.svelte
+++ b/src/routes/(app)/game/[id]/+page.svelte
@@ -52,7 +52,7 @@
{:else}
- Sign Up or Sign In to add to a list.
+ Sign Up or Sign In to add to a list.
{/if}
diff --git a/src/routes/(auth)/+layout.svelte b/src/routes/(auth)/+layout.svelte
index 5524879..0c042f8 100644
--- a/src/routes/(auth)/+layout.svelte
+++ b/src/routes/(auth)/+layout.svelte
@@ -18,8 +18,8 @@
{#if $page.url.pathname !== '/login'}
Login
{/if}
- {#if $page.url.pathname !== '/sign-up'}
- Sign up
+ {#if $page.url.pathname !== '/signup'}
+ Sign up
{/if}
diff --git a/src/routes/(auth)/sign-up/+page.server.ts b/src/routes/(auth)/signup/+page.server.ts
similarity index 100%
rename from src/routes/(auth)/sign-up/+page.server.ts
rename to src/routes/(auth)/signup/+page.server.ts
diff --git a/src/routes/(auth)/sign-up/+page.svelte b/src/routes/(auth)/signup/+page.svelte
similarity index 98%
rename from src/routes/(auth)/sign-up/+page.svelte
rename to src/routes/(auth)/signup/+page.svelte
index a126a5b..54c563b 100644
--- a/src/routes/(auth)/sign-up/+page.svelte
+++ b/src/routes/(auth)/signup/+page.svelte
@@ -46,7 +46,7 @@
Signup for an account
-