Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 27 additions & 29 deletions actions/login.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
"use server";

import * as z from "zod";
import bcrypt from "bcryptjs";
import { AuthError } from "next-auth";

import { db } from "@/lib/db";
import { signIn } from "@/auth";
import { LoginSchema } from "@/schemas";
import { getUserByEmail } from "@/data/user";
import { getTwoFactorTokenByEmail } from "@/data/two-factor-token";
import {
sendVerificationEmail,
sendTwoFactorTokenEmail,
} from "@/lib/mail";
import { sendVerificationEmail, sendTwoFactorTokenEmail } from "@/lib/mail";
import { DEFAULT_LOGIN_REDIRECT } from "@/routes";
import {
import {
generateVerificationToken,
generateTwoFactorToken
generateTwoFactorToken,
} from "@/lib/tokens";
import {
getTwoFactorConfirmationByUserId
} from "@/data/two-factor-confirmation";
import { getTwoFactorConfirmationByUserId } from "@/data/two-factor-confirmation";

export const login = async (
values: z.infer<typeof LoginSchema>,
callbackUrl?: string | null,
callbackUrl?: string | null
) => {
const validatedFields = LoginSchema.safeParse(values);

Expand All @@ -36,27 +32,34 @@ export const login = async (
const existingUser = await getUserByEmail(email);

if (!existingUser || !existingUser.email || !existingUser.password) {
return { error: "Email does not exist!" }
return { error: "Email does not exist!" };
}

if (!existingUser.emailVerified) {
const verificationToken = await generateVerificationToken(
existingUser.email,
existingUser.email
);

await sendVerificationEmail(
verificationToken.email,
verificationToken.token,
verificationToken.token
);

return { success: "Confirmation email sent!" };
}

if (existingUser.isTwoFactorEnabled && existingUser.email) {
const isPasswordValid = await bcrypt.compare(
password,
existingUser.password
);

if (!isPasswordValid) {
return { error: "Invalid credentials!" };
}

if (code) {
const twoFactorToken = await getTwoFactorTokenByEmail(
existingUser.email
);
const twoFactorToken = await getTwoFactorTokenByEmail(existingUser.email);

if (!twoFactorToken) {
return { error: "Invalid code!" };
Expand All @@ -73,7 +76,7 @@ export const login = async (
}

await db.twoFactorToken.delete({
where: { id: twoFactorToken.id }
where: { id: twoFactorToken.id },
});

const existingConfirmation = await getTwoFactorConfirmationByUserId(
Expand All @@ -82,21 +85,16 @@ export const login = async (

if (existingConfirmation) {
await db.twoFactorConfirmation.delete({
where: { id: existingConfirmation.id }
where: { id: existingConfirmation.id },
});
}

await db.twoFactorConfirmation.create({
data: {
userId: existingUser.id,
}
data: { userId: existingUser.id },
});
} else {
const twoFactorToken = await generateTwoFactorToken(existingUser.email)
await sendTwoFactorTokenEmail(
twoFactorToken.email,
twoFactorToken.token,
);
const twoFactorToken = await generateTwoFactorToken(existingUser.email);
await sendTwoFactorTokenEmail(twoFactorToken.email, twoFactorToken.token);

return { twoFactor: true };
}
Expand All @@ -107,14 +105,14 @@ export const login = async (
email,
password,
redirectTo: callbackUrl || DEFAULT_LOGIN_REDIRECT,
})
});
} catch (error) {
if (error instanceof AuthError) {
switch (error.type) {
case "CredentialsSignin":
return { error: "Invalid credentials!" }
return { error: "Invalid credentials!" };
default:
return { error: "Something went wrong!" }
return { error: "Something went wrong!" };
}
}

Expand Down