MCPcopy Create free account
hub / github.com/bewcloud/bewcloud / post

Function post

pages/login.tsx:74–175  ·  view source on GitHub ↗
({ request, match, user, session, isRunningLocally }: RequestHandlerParams)

Source from the content-addressed store, hash-verified

72}
73
74async 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);

Callers

nothing calls this directly

Calls 15

getFormDataFieldFunction · 0.90
validateEmailFunction · 0.90
generateHashFunction · 0.90
createSessionResponseFunction · 0.90
basicLayoutResponseFunction · 0.90
isThereAnAdminMethod · 0.80
isSingleSignOnEnabledMethod · 0.80
getConfigMethod · 0.80
getSignInUrlMethod · 0.80

Tested by

no test coverage detected