Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

♻️ Clean up login code (#1727)

Luke Vella1b3b3aac5024
diff --git a/apps/web/src/app/[locale]/(auth)/login/actions.ts b/apps/web/src/app/[locale]/(auth)/login/actions.ts
index c603faa25..733b2ee10 100644
--- a/apps/web/src/app/[locale]/(auth)/login/actions.ts
+++ b/apps/web/src/app/[locale]/(auth)/login/actions.ts
@@ -1,27 +1,14 @@
"use server";
-import { prisma } from "@rallly/database";
import { cookies } from "next/headers";
export async function setVerificationEmail(email: string) {
- const user = await prisma.user.findUnique({
- where: {
- email,
- },
- select: {
- email: true,
- },
- });
-
- if (user) {
- (await cookies()).set("verification-email", user.email, {
- httpOnly: true,
- secure: process.env.NEXT_PUBLIC_BASE_URL?.startsWith("https://"),
- sameSite: "lax",
- maxAge: 15 * 60,
- });
- return true;
- }
+ const cookieStore = await cookies();
- return false;
+ cookieStore.set("verification-email", email, {
+ httpOnly: true,
+ secure: process.env.NEXT_PUBLIC_BASE_URL?.startsWith("https://"),
+ sameSite: "lax",
+ maxAge: 15 * 60,
+ });
}
diff --git a/apps/web/src/app/[locale]/(auth)/login/components/login-email-form.tsx b/apps/web/src/app/[locale]/(auth)/login/components/login-email-form.tsx
index 3a41369fe..a42df5819 100644
--- a/apps/web/src/app/[locale]/(auth)/login/components/login-email-form.tsx
+++ b/apps/web/src/app/[locale]/(auth)/login/components/login-email-form.tsx
@@ -49,20 +49,28 @@ export function LoginWithEmailForm() {
<form
className="space-y-4"
onSubmit={handleSubmit(async ({ identifier }) => {
- const doesExist = await setVerificationEmail(identifier);
- if (doesExist) {
- await signIn("email", {
+ try {
+ const res = await signIn("email", {
email: identifier,
redirectTo: searchParams?.get("redirectTo") ?? undefined,
redirect: false,
});
+
+ if (res?.error) {
+ throw new Error(res.error);
+ }
+
+ await setVerificationEmail(identifier);
// redirect to verify page with redirectTo
+ const redirectTo = searchParams?.get("redirectTo");
router.push(
- `/login/verify?redirectTo=${encodeURIComponent(
- searchParams?.get("redirectTo") ?? "",
- )}`,
+ `/login/verify${
+ redirectTo
+ ? `?redirectTo=${encodeURIComponent(redirectTo)}`
+ : ""
+ }`,
);
- } else {
+ } catch (e) {
form.setError("identifier", {
message: t("userNotFound", {
defaultValue: "A user with that email doesn't exist",