({ request }: RequestHandlerParams)
| 20 | } |
| 21 | |
| 22 | async function post({ request }: RequestHandlerParams) { |
| 23 | const isMultiFactorAuthEnabled = await AppConfig.isMultiFactorAuthEnabled(); |
| 24 | |
| 25 | if (!isMultiFactorAuthEnabled) { |
| 26 | const responseBody: ResponseBody = { |
| 27 | success: false, |
| 28 | error: 'Passwordless passkey login requires multi-factor authentication to be enabled.', |
| 29 | }; |
| 30 | |
| 31 | return new Response(JSON.stringify(responseBody), { status: 403 }); |
| 32 | } |
| 33 | |
| 34 | const body = await request.clone().json() as RequestBody; |
| 35 | const { email } = body; |
| 36 | |
| 37 | const config = await AppConfig.getConfig(); |
| 38 | |
| 39 | // Discoverable credential flow: no email provided, let the authenticator present all credentials |
| 40 | if (!email) { |
| 41 | const options = await PasskeyModel.generateAuthenticationOptions(config.auth.baseUrl); |
| 42 | |
| 43 | const responseBody: ResponseBody = { |
| 44 | success: true, |
| 45 | options, |
| 46 | sessionData: { |
| 47 | challenge: options.challenge, |
| 48 | methodId: options.challenge, |
| 49 | }, |
| 50 | }; |
| 51 | |
| 52 | return new Response(JSON.stringify(responseBody)); |
| 53 | } |
| 54 | |
| 55 | const user = await UserModel.getByEmail(email); |
| 56 | |
| 57 | if (!user) { |
| 58 | const responseBody: ResponseBody = { |
| 59 | success: false, |
| 60 | error: 'User not found', |
| 61 | }; |
| 62 | |
| 63 | return new Response(JSON.stringify(responseBody), { status: 404 }); |
| 64 | } |
| 65 | |
| 66 | const allowedCredentials = PasskeyModel.getCredentialsFromUser(user); |
| 67 | |
| 68 | if (allowedCredentials.length === 0) { |
| 69 | const responseBody: ResponseBody = { |
| 70 | success: false, |
| 71 | error: 'No passkeys registered for this user', |
| 72 | }; |
| 73 | |
| 74 | return new Response(JSON.stringify(responseBody), { status: 400 }); |
| 75 | } |
| 76 | |
| 77 | const options = await PasskeyModel.generateAuthenticationOptions( |
| 78 | config.auth.baseUrl, |
| 79 | allowedCredentials, |
nothing calls this directly
no test coverage detected