Skip to content

Commit

Permalink
Force lowercase on email signup.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoBernardino committed Aug 26, 2023
1 parent 66f453a commit 8f9e9f2
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions pages/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,23 @@ import { SupportedCurrencySymbol, validateEmail } from '/public/ts/utils.ts';
async function createUserAction(request: Request) {
const { email, encrypted_key_pair }: { email: string; encrypted_key_pair: EncryptedData } = await request.json();

if (!email || !encrypted_key_pair) {
const lowercaseEmail = (email || '').toLocaleLowerCase().trim();

if (!lowercaseEmail || !encrypted_key_pair) {
return new Response('Bad Request', { status: 400 });
}

if (!validateEmail(email)) {
if (!validateEmail(lowercaseEmail)) {
return new Response('Bad Request', { status: 400 });
}

const existingUserByEmail = await getUserByEmail(email);
const existingUserByEmail = await getUserByEmail(lowercaseEmail);

if (existingUserByEmail) {
return new Response('Bad Request', { status: 400 });
}

const user = await createUser(email, encrypted_key_pair);
const user = await createUser(lowercaseEmail, encrypted_key_pair);

if (!user) {
return new Response('Bad Request', { status: 400 });
Expand Down Expand Up @@ -68,7 +70,9 @@ async function updateUserAction(request: Request) {
return new Response('Bad Request', { status: 400 });
}

if (!email && !encrypted_key_pair && !currency) {
const lowercaseEmail = (email || '').toLocaleLowerCase().trim();

if (!lowercaseEmail && !encrypted_key_pair && !currency) {
return new Response('Bad Request', { status: 400 });
}

Expand All @@ -79,8 +83,8 @@ async function updateUserAction(request: Request) {
user.extra.currency = currency;

await updateUser(user);
} else if (email) {
const existingUserByEmail = await getUserByEmail(email);
} else if (lowercaseEmail) {
const existingUserByEmail = await getUserByEmail(lowercaseEmail);

if (existingUserByEmail) {
return new Response('Bad Request', { status: 400 });
Expand All @@ -89,7 +93,7 @@ async function updateUserAction(request: Request) {

const verificationCode = await createVerificationCode(user, session, 'user-update');

if (email) {
if (lowercaseEmail) {
await sendVerifyUpdateEmailEmail(user.email, verificationCode);
}
if (encrypted_key_pair) {
Expand All @@ -100,8 +104,8 @@ async function updateUserAction(request: Request) {

const oldEmail = user.email;

if (email) {
user.email = email;
if (lowercaseEmail) {
user.email = lowercaseEmail;
}

if (encrypted_key_pair) {
Expand All @@ -110,8 +114,11 @@ async function updateUserAction(request: Request) {

await updateUser(user);

if (email && (user.subscription.external.stripe || user.subscription.external.paypal) && email !== oldEmail) {
await sendUpdateEmailInProviderEmail(oldEmail, email);
if (
lowercaseEmail && (user.subscription.external.stripe || user.subscription.external.paypal) &&
lowercaseEmail !== oldEmail
) {
await sendUpdateEmailInProviderEmail(oldEmail, lowercaseEmail);
}
}

Expand Down

0 comments on commit 8f9e9f2

Please sign in to comment.