Skip to content

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

♻️ Replace auth server actions with tRPC mutations (RAL-1025) (#2247)

Luke Velladd7b3280f578
diff --git a/apps/web/src/app/[locale]/(auth)/login/actions.ts b/apps/web/src/app/[locale]/(auth)/login/actions.ts
index 73fb06437..733b2ee10 100644
--- a/apps/web/src/app/[locale]/(auth)/login/actions.ts
+++ b/apps/web/src/app/[locale]/(auth)/login/actions.ts
@@ -1,11 +1,6 @@
"use server";
-import { prisma } from "@rallly/database";
-import { cookies, headers } from "next/headers";
-import * as z from "zod";
-import { AppError } from "@/lib/errors";
-import { createRatelimit } from "@/lib/rate-limit";
-import { actionClient } from "@/lib/safe-action/server";
+import { cookies } from "next/headers";
export async function setVerificationEmail(email: string) {
const cookieStore = await cookies();
@@ -17,47 +12,3 @@ export async function setVerificationEmail(email: string) {
maxAge: 15 * 60,
});
}
-
-const ratelimit = createRatelimit(10, "1 m");
-
-export const getLoginMethodAction = actionClient
- .metadata({ actionName: "get_login_method" })
- .inputSchema(z.object({ email: z.email() }))
- .action(async ({ parsedInput: { email } }) => {
- if (ratelimit) {
- const headersList = await headers();
- const ipAddress = headersList.get("x-forwarded-for");
-
- const { success } = await ratelimit.limit(
- `get_login_method:${ipAddress}`,
- );
-
- if (!success) {
- throw new AppError({
- code: "TOO_MANY_REQUESTS",
- message: "Too many requests",
- });
- }
- }
-
- const user = await prisma.user.findUnique({
- where: { email },
- select: {
- emailVerified: true,
- accounts: {
- select: {
- provider: true,
- },
- },
- },
- });
-
- if (
- user?.emailVerified &&
- user?.accounts.some((account) => account.provider === "credential")
- ) {
- return { method: "credential" as const };
- }
-
- return { method: "email" as const };
- });
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 d44241b25..b4747deb3 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
@@ -16,13 +16,10 @@ import React from "react";
import { useForm } from "react-hook-form";
import * as z from "zod";
-import {
- getLoginMethodAction,
- setVerificationEmail,
-} from "@/app/[locale]/(auth)/login/actions";
+import { setVerificationEmail } from "@/app/[locale]/(auth)/login/actions";
import { Trans, useTranslation } from "@/i18n/client";
import { authClient } from "@/lib/auth-client";
-import { useSafeAction } from "@/lib/safe-action/client";
+import { trpc } from "@/trpc/client";
import { validateRedirectUrl } from "@/utils/redirect";
function useLoginWithEmailSchema() {
@@ -50,8 +47,7 @@ export function LoginWithEmailForm() {
const { handleSubmit, formState } = form;
const { t } = useTranslation();
- const { executeAsync: getLoginMethod } = useSafeAction(getLoginMethodAction);
-
+ const getLoginMethod = trpc.auth.getLoginMethod.useMutation();
const isPasswordLogin = showPasswordField && !!form.watch("password");
return (
@@ -92,11 +88,11 @@ export function LoginWithEmailForm() {
return;
} else {
if (!showPasswordField) {
- const res = await getLoginMethod({
+ const res = await getLoginMethod.mutateAsync({
email: identifier,
});
- if (res.data?.method === "credential") {
+ if (res.method === "credential") {
// show password field
setShowPasswordField(true);
return;
diff --git a/apps/web/src/app/api/trpc/[trpc]/route.ts b/apps/web/src/app/api/trpc/[trpc]/route.ts
index 6da3096bc..e594e24b9 100644
--- a/apps/web/src/app/api/trpc/[trpc]/route.ts
+++ b/apps/web/src/app/api/trpc/[trpc]/route.ts
@@ -12,7 +12,7 @@ import { appRouter } from "@/trpc/routers";
const handler = async (req: NextRequest) => {
const { user } = await getUserSession();
- const ip = ipAddress(req);
+ const ip = ipAddress(req) ?? "127.0.0.1";
const ja4Digest = req.headers.get("x-vercel-ja4-digest") ?? undefined;
const reqLocale = getLocaleFromRequest(req);
const startTime = Date.now();
diff --git a/apps/web/src/trpc/routers/auth.ts b/apps/web/src/trpc/routers/auth.ts
index 34a39369f..608051f02 100644
--- a/apps/web/src/trpc/routers/auth.ts
+++ b/apps/web/src/trpc/routers/auth.ts
@@ -1,7 +1,8 @@
+import { prisma } from "@rallly/database";
import * as z from "zod";
import { decryptToken } from "@/utils/session";
-import { publicProcedure, router } from "../trpc";
+import { createRateLimitMiddleware, publicProcedure, router } from "../trpc";
export const auth = router({
getUserPermission: publicProcedure
@@ -15,4 +16,34 @@ export const auth = router({
return res;
}),
+
+ getLoginMethod: publicProcedure
+ .input(z.object({ email: z.email() }))
+ .use(createRateLimitMiddleware("get_login_method", 10, "1 m"))
+ .mutation(async ({ input }) => {
+ /**
+ * This should probably be a query instead of a mutation, but we need to
+ * keep it as a mutation for now to avoid breaking changes.
+ */
+ const user = await prisma.user.findUnique({
+ where: { email: input.email },
+ select: {
+ emailVerified: true,
+ accounts: {
+ select: {
+ provider: true,
+ },
+ },
+ },
+ });
+
+ if (
+ user?.emailVerified &&
+ user?.accounts.some((account) => account.provider === "credential")
+ ) {
+ return { method: "credential" as const };
+ }
+
+ return { method: "email" as const };
+ }),
});