Skip to content

Commit

Permalink
Refactoring login url to function
Browse files Browse the repository at this point in the history
  • Loading branch information
leog committed Mar 4, 2023
1 parent ad24062 commit 1569afe
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions apps/web/pages/api/auth/[...nextauth].tsx
Expand Up @@ -51,6 +51,9 @@ const signJwt = async (payload: { email: string }) => {
.sign(secret);
};

const loginWithTotp = async (user: { email: string }) =>
`/auth/login?totp=${await signJwt({ email: user.email })}`;

const providers: Provider[] = [
CredentialsProvider({
id: "credentials",
Expand Down Expand Up @@ -465,7 +468,7 @@ export default NextAuth({
}
}
if (existingUser.twoFactorEnabled) {
return `/auth/login?totp=${await signJwt({ email: existingUser.email })}`;
return loginWithTotp(existingUser);
} else {
return true;
}
Expand All @@ -481,7 +484,7 @@ export default NextAuth({
if (!userWithNewEmail) {
await prisma.user.update({ where: { id: existingUser.id }, data: { email: user.email } });
if (existingUser.twoFactorEnabled) {
return `/auth/login?totp=${await signJwt({ email: user.email })}`;
return loginWithTotp(existingUser);
} else {
return true;
}
Expand All @@ -501,7 +504,7 @@ export default NextAuth({
// if self-hosted then we can allow auto-merge of identity providers if email is verified
if (!hostedCal && existingUserWithEmail.emailVerified) {
if (existingUserWithEmail.twoFactorEnabled) {
return `/auth/login?totp=${await signJwt({ email: existingUserWithEmail.email })}`;
return loginWithTotp(existingUserWithEmail);
} else {
return true;
}
Expand All @@ -527,8 +530,7 @@ export default NextAuth({
});

if (existingUserWithEmail.twoFactorEnabled) {
const jwt = await signJwt({ email: existingUserWithEmail.email });
return `/auth/login?totp=${jwt}`;
return loginWithTotp(existingUserWithEmail);
} else {
return true;
}
Expand All @@ -544,7 +546,7 @@ export default NextAuth({
data: { password: null },
});
if (existingUserWithEmail.twoFactorEnabled) {
return `/auth/login?totp=${await signJwt({ email: existingUserWithEmail.email })}`;
return loginWithTotp(existingUserWithEmail);
} else {
return true;
}
Expand All @@ -571,7 +573,7 @@ export default NextAuth({
await calcomAdapter.linkAccount(linkAccountNewUserData);

if (account.twoFactorEnabled) {
return `/auth/login?totp=${await signJwt({ email: newUser.email })}`;
return loginWithTotp(newUser);
} else {
return true;
}
Expand Down

0 comments on commit 1569afe

Please sign in to comment.