Skip to content

Commit

Permalink
Bug: case sensitive emails (#485)
Browse files Browse the repository at this point in the history
Fixes #484

Co-authored-by: Rajat Saxena <[email protected]>
  • Loading branch information
rajat1saxena and Rajat Saxena authored Aug 30, 2024
1 parent 07f9d38 commit 599e367
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
13 changes: 7 additions & 6 deletions apps/web/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,36 @@ export const { auth, signIn, signOut, handlers } = NextAuth({
return null;
}

const { email, code }: any = parsedCredentials.data;
const { email, code } = parsedCredentials.data;
const sanitizedEmail = email.toLowerCase();

const verificationToken =
await VerificationToken.findOneAndDelete({
email,
email: sanitizedEmail,
domain: domain.name,
code: hashCode(+code),
timestamp: { $gt: Date.now() },
});
if (!verificationToken) {
error(`Invalid code`, {
email: email,
email: sanitizedEmail,
});
return null;
}

let user = await User.findOne({
domain: domain._id,
email,
email: sanitizedEmail,
});
if (!user) {
user = await createUser({
domain,
email,
email: sanitizedEmail,
});
}
return {
id: user.userId,
email,
email: sanitizedEmail,
name: user.name,
};
},
Expand Down
6 changes: 3 additions & 3 deletions apps/web/graphql/mails/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ export async function createSubscription(
ctx: GQLContext,
): Promise<boolean> {
try {
const sanitizedEmail = email.toLowerCase();
let dbUser: User | null = await UserModel.findOne({
name,
email,
email: sanitizedEmail,
domain: ctx.subdomain._id,
});

if (!dbUser) {
dbUser = await createUser({
domain: ctx.subdomain!,
name: name,
email: email,
email: sanitizedEmail,
lead: constants.leadNewsletter,
});
}
Expand Down
6 changes: 4 additions & 2 deletions apps/web/pages/api/auth/code/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ export default async function handler(
}
const code = generateUniquePasscode();

const sanitizedEmail = (email as string).toLowerCase();

await VerificationToken.create({
domain: domain.name,
email,
email: sanitizedEmail,
code: hashCode(code),
timestamp: Date.now() + 1000 * 60 * 5,
});

try {
const emailBody = pug.render(MagicCodeEmailTemplate, { code });
await send({
to: [<string>email],
to: [sanitizedEmail],
subject: `${responses.sign_in_mail_prefix} ${req.headers["host"]}`,
body: emailBody,
});
Expand Down

0 comments on commit 599e367

Please sign in to comment.