Skip to content

Commit

Permalink
fix amr not existing on user object
Browse files Browse the repository at this point in the history
  • Loading branch information
kizivat committed May 10, 2024
1 parent 48d1f3e commit 873ec1b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Session, SupabaseClient } from "@supabase/supabase-js"
import { Session, SupabaseClient, type AMREntry } from "@supabase/supabase-js"
import { Database } from "./DatabaseDefinitions"

// See https://kit.svelte.dev/docs/types#app
Expand All @@ -11,6 +11,7 @@ declare global {
safeGetSession: () => Promise<{
session: Session | null
user: User | null
amr: AMREntry[] | null
}>
}
interface PageData {
Expand Down
16 changes: 11 additions & 5 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,25 @@ export const handle: Handle = async ({ event, resolve }) => {
data: { session },
} = await event.locals.supabase.auth.getSession()
if (!session) {
return { session: null, user: null }
return { session: null, user: null, amr: null }
}

const {
data: { user },
error,
error: userError,
} = await event.locals.supabase.auth.getUser()
if (error) {
if (userError) {
// JWT validation has failed
return { session: null, user: null }
return { session: null, user: null, amr: null }
}

return { session, user }
const { data: aal, error: amrError } =
await event.locals.supabase.auth.mfa.getAuthenticatorAssuranceLevel()
if (amrError) {
return { session, user, amr: null }
}

return { session, user, amr: aal.currentAuthenticationMethods }
}

return resolve(event, {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/(admin)/account/(menu)/billing/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const load: PageServerLoad = async ({
locals: { safeGetSession, supabaseServiceRole },
}) => {
const { session, user } = await safeGetSession()
if (!session) {
if (!session || !user?.id) {
throw redirect(303, "/login")
}

Expand Down
4 changes: 2 additions & 2 deletions src/routes/(admin)/account/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export const load: LayoutServerLoad = async ({
}) => {
const { session, user } = await safeGetSession()

if (!session) {
if (!session || !user?.id) {
throw redirect(303, "/login")
}

const { data: profile } = await supabase
.from("profiles")
.select(`*`)
.eq("id", user.id)
.eq("id", user?.id)
.single()

return { session, profile }
Expand Down
10 changes: 9 additions & 1 deletion src/routes/(admin)/account/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export const load = async ({ fetch, data, depends, url }) => {
data: { user },
} = await supabase.auth.getUser()

const { data: aal } = await supabase.auth.mfa.getAuthenticatorAssuranceLevel()

const profile: Database["public"]["Tables"]["profiles"]["Row"] | null =
data.profile

Expand All @@ -57,7 +59,13 @@ export const load = async ({ fetch, data, depends, url }) => {
throw redirect(303, createProfilePath)
}

return { supabase, session, profile, user }
return {
supabase,
session,
profile,
user,
amr: aal?.currentAuthenticationMethods,
}
}

export const _hasFullProfile = (
Expand Down
11 changes: 5 additions & 6 deletions src/routes/(admin)/account/api/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const actions = {
}
},
updatePassword: async ({ request, locals: { supabase, safeGetSession } }) => {
const { session, user } = await safeGetSession()
const { session, user, amr } = await safeGetSession()
if (!session) {
throw redirect(303, "/login")
}
Expand All @@ -56,8 +56,7 @@ export const actions = {

// Can check if we're a "password recovery" session by checking session amr
// let currentPassword take priority if provided (user can use either form)
// @ts-expect-error: we ignore because Supabase does not maintain an AMR typedef
const recoveryAmr = user?.amr?.find((x) => x.method === "recovery")
const recoveryAmr = amr?.find((x) => x.method === "recovery")
const isRecoverySession = recoveryAmr && !currentPassword

// if this is password recovery session, check timestamp of recovery session
Expand Down Expand Up @@ -151,7 +150,7 @@ export const actions = {
locals: { supabase, supabaseServiceRole, safeGetSession },
}) => {
const { session, user } = await safeGetSession()
if (!session) {
if (!session || !user?.id) {
throw redirect(303, "/login")
}

Expand Down Expand Up @@ -193,7 +192,7 @@ export const actions = {
},
updateProfile: async ({ request, locals: { supabase, safeGetSession } }) => {
const { session, user } = await safeGetSession()
if (!session) {
if (!session || !user?.id) {
throw redirect(303, "/login")
}

Expand Down Expand Up @@ -239,7 +238,7 @@ export const actions = {
}

const { error } = await supabase.from("profiles").upsert({
id: user?.id,
id: user.id,
full_name: fullName,
company_name: companyName,
website: website,
Expand Down

0 comments on commit 873ec1b

Please sign in to comment.