diff --git a/layouts/AuthRedirect.tsx b/layouts/AuthRedirect.tsx index 916dc03..4fb32e5 100644 --- a/layouts/AuthRedirect.tsx +++ b/layouts/AuthRedirect.tsx @@ -20,14 +20,22 @@ export default function AuthRedirect({ children }: Props) { if (!router.pathname.startsWith("/public")) { if ( status === "authenticated" && - (router.pathname === "/login" || router.pathname === "/register") + (router.pathname === "/login" || + router.pathname === "/register" || + router.pathname === "/confirmation" || + router.pathname === "/forgot") ) { router.push("/").then(() => { setRedirect(false); }); } else if ( status === "unauthenticated" && - !(router.pathname === "/login" || router.pathname === "/register") + !( + router.pathname === "/login" || + router.pathname === "/register" || + router.pathname === "/confirmation" || + router.pathname === "/forgot" + ) ) { router.push("/login").then(() => { setRedirect(false); diff --git a/lib/api/sendVerificationRequest.ts b/lib/api/sendVerificationRequest.ts index 4cb85a2..b51db86 100644 --- a/lib/api/sendVerificationRequest.ts +++ b/lib/api/sendVerificationRequest.ts @@ -5,8 +5,6 @@ import { createTransport } from "nodemailer"; export default async function sendVerificationRequest( params: SendVerificationRequestParams ) { - console.log(params); - const { identifier, url, provider, theme } = params; const { host } = new URL(url); const transport = createTransport(provider.server); diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts index 3f2d8ef..fc66184 100644 --- a/pages/api/auth/[...nextauth].ts +++ b/pages/api/auth/[...nextauth].ts @@ -10,6 +10,8 @@ import { Adapter } from "next-auth/adapters"; import sendVerificationRequest from "@/lib/api/sendVerificationRequest"; import { Provider } from "next-auth/providers"; +let email; + const providers: Provider[] = [ CredentialsProvider({ type: "credentials", @@ -63,6 +65,7 @@ if (process.env.EMAIL_SERVER && process.env.EMAIL_FROM) from: process.env.EMAIL_FROM, maxAge: 600, sendVerificationRequest(params) { + email = params.identifier; sendVerificationRequest(params); }, }) @@ -76,6 +79,7 @@ export const authOptions: AuthOptions = { providers, pages: { signIn: "/login", + verifyRequest: "/confirmation", }, callbacks: { session: async ({ session, token }: { session: Session; token: JWT }) => { diff --git a/components/Modal/EmailConfirmaion.tsx b/pages/confirmation.tsx similarity index 69% rename from components/Modal/EmailConfirmaion.tsx rename to pages/confirmation.tsx index 966287b..1c9a657 100644 --- a/components/Modal/EmailConfirmaion.tsx +++ b/pages/confirmation.tsx @@ -1,23 +1,24 @@ import { signIn } from "next-auth/react"; import React from "react"; -export default function EmailConfirmaion({ email }: { email: string }) { +export default function EmailConfirmaion() { return (
-
+

Please check your email

A sign in link has been sent to your email address.

-
You can safely close this page.

+ {/*
signIn("email", { - email, + email: email, redirect: false, }) } className="mx-auto font-semibold mt-2 cursor-pointer w-fit" > Resend? -
+
*/}
); diff --git a/pages/forgot.tsx b/pages/forgot.tsx index 2ae2029..ee7cd46 100644 --- a/pages/forgot.tsx +++ b/pages/forgot.tsx @@ -1,4 +1,3 @@ -import EmailConfirmaion from "@/components/Modal/EmailConfirmaion"; import SubmitButton from "@/components/SubmitButton"; import { signIn } from "next-auth/react"; import Link from "next/link"; @@ -11,7 +10,6 @@ interface FormData { export default function Forgot() { const [submitLoader, setSubmitLoader] = useState(false); - const [showConfirmation, setShowConfirmation] = useState(false); const [form, setForm] = useState({ email: "", @@ -23,20 +21,16 @@ export default function Forgot() { const load = toast.loading("Sending login link..."); - const res = await signIn("email", { + await signIn("email", { email: form.email, - callbackUrl: window.location.origin, + callbackUrl: "/", }); - setShowConfirmation(true); - toast.dismiss(load); setSubmitLoader(false); - if (!res?.ok) { - toast.error("Invalid login."); - } + toast.success("Login link sent."); } else { toast.error("Please fill out all the fields."); } @@ -44,9 +38,6 @@ export default function Forgot() { return ( <> - {showConfirmation && form.email ? ( - - ) : undefined}

Linkwarden

diff --git a/pages/register.tsx b/pages/register.tsx index 8e22b76..c17db4a 100644 --- a/pages/register.tsx +++ b/pages/register.tsx @@ -3,7 +3,6 @@ import { useState } from "react"; import { toast } from "react-hot-toast"; import SubmitButton from "@/components/SubmitButton"; import { signIn } from "next-auth/react"; -import EmailConfirmaion from "@/components/Modal/EmailConfirmaion"; const EmailProvider = process.env.NEXT_PUBLIC_EMAIL_PROVIDER; @@ -17,7 +16,6 @@ type FormData = { export default function Register() { const [submitLoader, setSubmitLoader] = useState(false); - const [showConfirmation, setShowConfirmation] = useState(false); const [form, setForm] = useState({ name: "", @@ -50,7 +48,7 @@ export default function Register() { const sendConfirmation = async () => { await signIn("email", { email: form.email, - redirect: false, + callbackUrl: "/", }); }; @@ -76,10 +74,7 @@ export default function Register() { setSubmitLoader(false); if (response.ok) { - if (form.email) { - await sendConfirmation(); - setShowConfirmation(true); - } + if (form.email) await sendConfirmation(); toast.success( EmailProvider @@ -99,9 +94,6 @@ export default function Register() { return ( <> - {showConfirmation && form.email ? ( - - ) : undefined}

Linkwarden