({ request, match, user, session, isRunningLocally }: RequestHandlerParams)
| 72 | } |
| 73 | |
| 74 | async function post({ request, match, user, session, isRunningLocally }: RequestHandlerParams) { |
| 75 | if (user) { |
| 76 | return new Response('Redirect', { status: 303, headers: { 'Location': `/` } }); |
| 77 | } |
| 78 | |
| 79 | const isEmailVerificationEnabled = await AppConfig.isEmailVerificationEnabled(); |
| 80 | const isMultiFactorAuthEnabled = await AppConfig.isMultiFactorAuthEnabled() && await UserModel.isThereAnAdmin(); |
| 81 | const isSingleSignOnEnabled = await AppConfig.isSingleSignOnEnabled(); |
| 82 | const config = await AppConfig.getConfig(); |
| 83 | const helpEmail = config.visuals.helpEmail; |
| 84 | |
| 85 | const singleSignOnUrl = isSingleSignOnEnabled |
| 86 | ? (await OidcModel.getSignInUrl({ requestPermissions: config.auth.singleSignOnScopes })) |
| 87 | : undefined; |
| 88 | |
| 89 | const searchParams = new URL(request.url).searchParams; |
| 90 | |
| 91 | const formData = await request.clone().formData(); |
| 92 | const email = getFormDataField(formData, 'email'); |
| 93 | |
| 94 | const redirectUrl = searchParams.get('redirect') || '/'; |
| 95 | |
| 96 | let errorMessage: string | undefined = undefined; |
| 97 | |
| 98 | try { |
| 99 | if (!validateEmail(email)) { |
| 100 | throw new Error(`Invalid email.`); |
| 101 | } |
| 102 | |
| 103 | const password = getFormDataField(formData, 'password'); |
| 104 | |
| 105 | if (password.length < 6) { |
| 106 | throw new Error(`Password is too short.`); |
| 107 | } |
| 108 | |
| 109 | const hashedPassword = await generateHash(`${password}:${PASSWORD_SALT}`, 'SHA-256'); |
| 110 | |
| 111 | const user = await UserModel.getByEmail(email); |
| 112 | |
| 113 | if (!user || user.hashed_password !== hashedPassword) { |
| 114 | throw new Error('Email not found or invalid password.'); |
| 115 | } |
| 116 | |
| 117 | const isEmailVerificationEnabled = await AppConfig.isEmailVerificationEnabled(); |
| 118 | |
| 119 | if (!isEmailVerificationEnabled && !user.extra.is_email_verified) { |
| 120 | user.extra.is_email_verified = true; |
| 121 | |
| 122 | await UserModel.update(user); |
| 123 | } |
| 124 | |
| 125 | if (!user.extra.is_email_verified) { |
| 126 | const code = getFormDataField(formData, 'verification-code'); |
| 127 | |
| 128 | if (!code) { |
| 129 | const verificationCode = await VerificationCodeModel.create(user, user.email, 'email'); |
| 130 | |
| 131 | await EmailModel.sendVerificationEmail(user.email, verificationCode); |
nothing calls this directly
no test coverage detected