Skip to content

Commit

Permalink
chore: Check if user is already available and if an email is available
Browse files Browse the repository at this point in the history
  • Loading branch information
fayazara committed Nov 14, 2024
1 parent d249754 commit 62b4523
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions src/runtime/server/lib/webauthn/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ import { generateRegistrationOptions, verifyRegistrationResponse } from '@simple
import defu from 'defu'
import { bufferToBase64URLString } from '@simplewebauthn/browser'
import { getRandomValues } from 'uncrypto'
import { useRuntimeConfig } from '#imports'
import { useUserSession, useRuntimeConfig } from '#imports'

import type { WebAuthnUser, WebAuthnRegisterEventHandlerOptions } from '#auth-utils'
import type { RegistrationBody } from '~/src/runtime/types/webauthn'

export interface WebAuthnRegisterOptions {
userName?: string
displayName?: string
}

export function defineWebAuthnRegisterEventHandler<T extends WebAuthnUser>({
storeChallenge,
getChallenge,
Expand All @@ -22,31 +18,27 @@ export function defineWebAuthnRegisterEventHandler<T extends WebAuthnUser>({
excludeCredentials,
onSuccess,
onError,
userName,
displayName,
}: WebAuthnRegisterEventHandlerOptions<T> & WebAuthnRegisterOptions) {
}: WebAuthnRegisterEventHandlerOptions<T>) {
return eventHandler(async (event) => {
const { user: sessionUser } = useUserSession()
const url = getRequestURL(event)
const body = await readBody<RegistrationBody<T>>(event)

// Check if userName is provided in options or request body
const finalUserName = userName || body.user?.userName
if (!finalUserName) {
// Check for existing session user's email or body user's userName
if (!sessionUser?.email && (body.verify === undefined || !body.user?.userName)) {

Check failure on line 28 in src/runtime/server/lib/webauthn/register.ts

View workflow job for this annotation

GitHub Actions / test

Property 'email' does not exist on type 'ComputedRef<User | null>'.
throw createError({
message: 'userName is required either in options or request body',
message: 'No authenticated user found and missing userName in request',
statusCode: 400,
})
}

let user: T
if (body.user && validateUser) {
user = await validateUserData(body.user, validateUser)
}
else {
user = {
userName: finalUserName,
displayName: displayName || finalUserName,
} as T
// Use session user's email as userName if available, otherwise use body user
let user = sessionUser?.email

Check failure on line 36 in src/runtime/server/lib/webauthn/register.ts

View workflow job for this annotation

GitHub Actions / test

Property 'email' does not exist on type 'ComputedRef<User | null>'.
? { ...body.user, userName: sessionUser.email }

Check failure on line 37 in src/runtime/server/lib/webauthn/register.ts

View workflow job for this annotation

GitHub Actions / test

Property 'email' does not exist on type 'ComputedRef<User | null>'.
: body.user

if (validateUser) {
user = await validateUserData(user, validateUser)
}

const _config = defu(await getOptions?.(event, body) ?? {}, useRuntimeConfig(event).webauthn.register, {
Expand Down Expand Up @@ -159,4 +151,4 @@ function createUserValidationError(validateError?: any) {
message: 'User Validation Error',
data: validateError,
})
}
}

0 comments on commit 62b4523

Please sign in to comment.