Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

add signin

Kiran Kf390568ac561
diff --git a/apps/web/app/api/auth/reset-password/route.ts b/apps/web/app/api/auth/reset-password/route.ts
index dc090a167a..b7ff07cd9d 100644
--- a/apps/web/app/api/auth/reset-password/route.ts
+++ b/apps/web/app/api/auth/reset-password/route.ts
@@ -2,7 +2,7 @@ import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import { parseRequestBody } from "@/lib/api/utils";
import { hashPassword } from "@/lib/auth/password";
import { prisma } from "@/lib/prisma";
-import { resetPasswordSchema } from "@/lib/zod/schemas/password";
+import { resetPasswordSchema } from "@/lib/zod/schemas/auth";
import { sendEmail } from "emails";
import PasswordUpdated from "emails/password-updated";
import { NextRequest, NextResponse } from "next/server";
diff --git a/apps/web/app/api/user/password/route.ts b/apps/web/app/api/user/password/route.ts
index 586c4dfa51..01b810a507 100644
--- a/apps/web/app/api/user/password/route.ts
+++ b/apps/web/app/api/user/password/route.ts
@@ -3,7 +3,7 @@ import { parseRequestBody } from "@/lib/api/utils";
import { withSession } from "@/lib/auth";
import { hashPassword, validatePassword } from "@/lib/auth/password";
import { prisma } from "@/lib/prisma";
-import { updatePasswordSchema } from "@/lib/zod/schemas/password";
+import { updatePasswordSchema } from "@/lib/zod/schemas/auth";
import { NextResponse } from "next/server";
// PATCH /api/user/password - updates the user's password
diff --git a/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/form.tsx b/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/form.tsx
index 7200f68075..0a316df32c 100644
--- a/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/form.tsx
+++ b/apps/web/app/app.dub.co/(auth)/auth/reset-password/[token]/form.tsx
@@ -1,7 +1,7 @@
"use client";
import z from "@/lib/zod";
-import { resetPasswordSchema } from "@/lib/zod/schemas/password";
+import { resetPasswordSchema } from "@/lib/zod/schemas/auth";
import { Button } from "@dub/ui";
import { cn } from "@dub/utils";
import { zodResolver } from "@hookform/resolvers/zod";
diff --git a/apps/web/app/app.dub.co/(auth)/login/page.tsx b/apps/web/app/app.dub.co/(auth)/login/page.tsx
index d51597bcc1..72707ec1c2 100644
--- a/apps/web/app/app.dub.co/(auth)/login/page.tsx
+++ b/apps/web/app/app.dub.co/(auth)/login/page.tsx
@@ -2,6 +2,7 @@ import { Button, Logo } from "@dub/ui";
import { HOME_DOMAIN, constructMetadata } from "@dub/utils";
import { Suspense } from "react";
import LoginForm from "./form";
+import { SignInWithEmailPassword } from "./signin-email-password";
export const metadata = constructMetadata({
title: `Sign in to ${process.env.NEXT_PUBLIC_APP_NAME}`,
@@ -32,6 +33,8 @@ export default function LoginPage() {
</>
}
>
+ <SignInWithEmailPassword csrfToken="" />
+ <div className="border-t border-gray-300" />
<LoginForm />
</Suspense>
</div>
diff --git a/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx b/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx
new file mode 100644
index 0000000000..4e193cef14
--- /dev/null
+++ b/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx
@@ -0,0 +1,89 @@
+"use client";
+
+import z from "@/lib/zod";
+import { signInSchema } from "@/lib/zod/schemas/auth";
+import { Button } from "@dub/ui";
+import { signIn } from "next-auth/react";
+import { useRouter, useSearchParams } from "next/navigation";
+import { useForm } from "react-hook-form";
+import { toast } from "sonner";
+
+interface SignInWithEmailPasswordProps {
+ csrfToken: string;
+}
+
+const errorCodes = {
+ "no-credentials": "Please provide an email and password.",
+ "invalid-credentials": "Email or password is incorrect.",
+};
+
+export const SignInWithEmailPassword = ({
+ csrfToken,
+}: SignInWithEmailPasswordProps) => {
+ const router = useRouter();
+ const searchParams = useSearchParams();
+
+ const {
+ register,
+ handleSubmit,
+ formState: { isLoading, isSubmitting },
+ } = useForm<z.infer<typeof signInSchema>>({
+ defaultValues: { email: "", password: "" },
+ });
+
+ const onSubmit = handleSubmit(async (data) => {
+ try {
+ const response = await signIn("credentials", {
+ ...data,
+ csrfToken,
+ redirect: false,
+ callbackUrl: searchParams.get("next") || "/",
+ });
+
+ if (!response) {
+ return;
+ }
+
+ if (!response.ok && response.error) {
+ throw new Error(errorCodes[response.error]);
+ }
+
+ if (response.url) {
+ toast.success("Redirecting...");
+ router.replace(response.url);
+ return;
+ }
+ } catch (error) {
+ toast.error(error.message);
+ }
+ });
+
+ return (
+ <form onSubmit={onSubmit}>
+ <div className="flex flex-col space-y-4">
+ <input
+ type="email"
+ placeholder="[email protected]"
+ autoComplete="email"
+ required
+ {...register("email")}
+ className="mt-1 block w-full appearance-none rounded-md border border-gray-300 px-3 py-2 placeholder-gray-400 shadow-sm focus:border-black focus:outline-none focus:ring-black sm:text-sm"
+ />
+ <input
+ type="password"
+ placeholder="********"
+ autoComplete="password"
+ required
+ {...register("password")}
+ className="mt-1 block w-full appearance-none rounded-md border border-gray-300 px-3 py-2 placeholder-gray-400 shadow-sm focus:border-black focus:outline-none focus:ring-black sm:text-sm"
+ />
+ <Button
+ text="Sign in"
+ type="submit"
+ loading={isLoading}
+ disabled={isSubmitting}
+ />
+ </div>
+ </form>
+ );
+};
diff --git a/apps/web/app/app.dub.co/(dashboard)/settings/security/update-password.tsx b/apps/web/app/app.dub.co/(dashboard)/settings/security/update-password.tsx
index 452686777a..acd3dfec02 100644
--- a/apps/web/app/app.dub.co/(dashboard)/settings/security/update-password.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/settings/security/update-password.tsx
@@ -1,7 +1,7 @@
"use client";
import z from "@/lib/zod";
-import { updatePasswordSchema } from "@/lib/zod/schemas/password";
+import { updatePasswordSchema } from "@/lib/zod/schemas/auth";
import { Button } from "@dub/ui";
import { cn } from "@dub/utils";
import { zodResolver } from "@hookform/resolvers/zod";
diff --git a/apps/web/lib/auth/options.ts b/apps/web/lib/auth/options.ts
index fc239bdf37..4b9d323040 100644
--- a/apps/web/lib/auth/options.ts
+++ b/apps/web/lib/auth/options.ts
@@ -17,6 +17,7 @@ import { dub } from "../dub";
import { subscribe } from "../flodesk";
import { isStored, storage } from "../storage";
import { UserProps } from "../types";
+import { validatePassword } from "./password";
const VERCEL_DEPLOYMENT = !!process.env.VERCEL_URL;
@@ -165,7 +166,61 @@ export const authOptions: NextAuthOptions = {
};
},
}),
+
+ // Sign in with email and password
+ CredentialsProvider({
+ id: "credentials",
+ name: "Dub.co",
+ type: "credentials",
+ credentials: {
+ email: { type: "email" },
+ password: { type: "password" },
+ },
+ async authorize(credentials) {
+ if (!credentials) {
+ throw new Error("no-credentials");
+ }
+
+ const { email, password } = credentials;
+
+ if (!email || !password) {
+ throw new Error("no-credentials");
+ }
+
+ const user = await prisma.user.findUnique({
+ where: { email },
+ select: {
+ id: true,
+ passwordHash: true,
+ name: true,
+ email: true,
+ image: true,
+ },
+ });
+
+ if (!user || !user.passwordHash) {
+ throw new Error("invalid-credentials");
+ }
+
+ const passwordMatch = await validatePassword({
+ password,
+ passwordHash: user.passwordHash,
+ });
+
+ if (!passwordMatch) {
+ throw new Error("invalid-credentials");
+ }
+
+ return {
+ id: user.id,
+ name: user.name,
+ email: user.email,
+ image: user.image,
+ };
+ },
+ }),
],
+ // @ts-ignore
adapter: PrismaAdapter(prisma),
session: { strategy: "jwt" },
cookies: {
diff --git a/apps/web/lib/zod/schemas/password.ts b/apps/web/lib/zod/schemas/auth.ts
similarity index 77%
rename from apps/web/lib/zod/schemas/password.ts
rename to apps/web/lib/zod/schemas/auth.ts
index dac0958a61..339955e34f 100644
--- a/apps/web/lib/zod/schemas/password.ts
+++ b/apps/web/lib/zod/schemas/auth.ts
@@ -10,3 +10,8 @@ export const updatePasswordSchema = z.object({
currentPassword: z.string().min(1),
newPassword: z.string().min(8, "Password must be at least 8 characters"),
});
+
+export const signInSchema = z.object({
+ email: z.string().email().min(1),
+ password: z.string().min(1),
+});