fixes and improvements
This commit is contained in:
parent
0bcb8515a0
commit
7be3b37b21
|
@ -20,14 +20,22 @@ export default function AuthRedirect({ children }: Props) {
|
||||||
if (!router.pathname.startsWith("/public")) {
|
if (!router.pathname.startsWith("/public")) {
|
||||||
if (
|
if (
|
||||||
status === "authenticated" &&
|
status === "authenticated" &&
|
||||||
(router.pathname === "/login" || router.pathname === "/register")
|
(router.pathname === "/login" ||
|
||||||
|
router.pathname === "/register" ||
|
||||||
|
router.pathname === "/confirmation" ||
|
||||||
|
router.pathname === "/forgot")
|
||||||
) {
|
) {
|
||||||
router.push("/").then(() => {
|
router.push("/").then(() => {
|
||||||
setRedirect(false);
|
setRedirect(false);
|
||||||
});
|
});
|
||||||
} else if (
|
} else if (
|
||||||
status === "unauthenticated" &&
|
status === "unauthenticated" &&
|
||||||
!(router.pathname === "/login" || router.pathname === "/register")
|
!(
|
||||||
|
router.pathname === "/login" ||
|
||||||
|
router.pathname === "/register" ||
|
||||||
|
router.pathname === "/confirmation" ||
|
||||||
|
router.pathname === "/forgot"
|
||||||
|
)
|
||||||
) {
|
) {
|
||||||
router.push("/login").then(() => {
|
router.push("/login").then(() => {
|
||||||
setRedirect(false);
|
setRedirect(false);
|
||||||
|
|
|
@ -5,8 +5,6 @@ import { createTransport } from "nodemailer";
|
||||||
export default async function sendVerificationRequest(
|
export default async function sendVerificationRequest(
|
||||||
params: SendVerificationRequestParams
|
params: SendVerificationRequestParams
|
||||||
) {
|
) {
|
||||||
console.log(params);
|
|
||||||
|
|
||||||
const { identifier, url, provider, theme } = params;
|
const { identifier, url, provider, theme } = params;
|
||||||
const { host } = new URL(url);
|
const { host } = new URL(url);
|
||||||
const transport = createTransport(provider.server);
|
const transport = createTransport(provider.server);
|
||||||
|
|
|
@ -10,6 +10,8 @@ import { Adapter } from "next-auth/adapters";
|
||||||
import sendVerificationRequest from "@/lib/api/sendVerificationRequest";
|
import sendVerificationRequest from "@/lib/api/sendVerificationRequest";
|
||||||
import { Provider } from "next-auth/providers";
|
import { Provider } from "next-auth/providers";
|
||||||
|
|
||||||
|
let email;
|
||||||
|
|
||||||
const providers: Provider[] = [
|
const providers: Provider[] = [
|
||||||
CredentialsProvider({
|
CredentialsProvider({
|
||||||
type: "credentials",
|
type: "credentials",
|
||||||
|
@ -63,6 +65,7 @@ if (process.env.EMAIL_SERVER && process.env.EMAIL_FROM)
|
||||||
from: process.env.EMAIL_FROM,
|
from: process.env.EMAIL_FROM,
|
||||||
maxAge: 600,
|
maxAge: 600,
|
||||||
sendVerificationRequest(params) {
|
sendVerificationRequest(params) {
|
||||||
|
email = params.identifier;
|
||||||
sendVerificationRequest(params);
|
sendVerificationRequest(params);
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -76,6 +79,7 @@ export const authOptions: AuthOptions = {
|
||||||
providers,
|
providers,
|
||||||
pages: {
|
pages: {
|
||||||
signIn: "/login",
|
signIn: "/login",
|
||||||
|
verifyRequest: "/confirmation",
|
||||||
},
|
},
|
||||||
callbacks: {
|
callbacks: {
|
||||||
session: async ({ session, token }: { session: Session; token: JWT }) => {
|
session: async ({ session, token }: { session: Session; token: JWT }) => {
|
||||||
|
|
|
@ -1,23 +1,24 @@
|
||||||
import { signIn } from "next-auth/react";
|
import { signIn } from "next-auth/react";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
export default function EmailConfirmaion({ email }: { email: string }) {
|
export default function EmailConfirmaion() {
|
||||||
return (
|
return (
|
||||||
<div className="overflow-y-auto py-2 fixed top-0 bottom-0 right-0 left-0 bg-gray-500 bg-opacity-10 backdrop-blur-sm flex items-center fade-in z-30">
|
<div className="overflow-y-auto py-2 fixed top-0 bottom-0 right-0 left-0 bg-gray-500 bg-opacity-10 backdrop-blur-sm flex items-center fade-in z-30">
|
||||||
<div className="mx-auto p-3 rounded-xl border border-sky-100 shadow-lg bg-gray-100 text-sky-800">
|
<div className="mx-auto p-3 text-center rounded-xl border border-sky-100 shadow-lg bg-gray-100 text-sky-800">
|
||||||
<p className="text-center text-2xl mb-2">Please check your email</p>
|
<p className="text-center text-2xl mb-2">Please check your email</p>
|
||||||
<p>A sign in link has been sent to your email address.</p>
|
<p>A sign in link has been sent to your email address.</p>
|
||||||
<div
|
<p>You can safely close this page.</p>
|
||||||
|
{/* <div
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
signIn("email", {
|
signIn("email", {
|
||||||
email,
|
email: email,
|
||||||
redirect: false,
|
redirect: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
className="mx-auto font-semibold mt-2 cursor-pointer w-fit"
|
className="mx-auto font-semibold mt-2 cursor-pointer w-fit"
|
||||||
>
|
>
|
||||||
Resend?
|
Resend?
|
||||||
</div>
|
</div> */}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
|
@ -1,4 +1,3 @@
|
||||||
import EmailConfirmaion from "@/components/Modal/EmailConfirmaion";
|
|
||||||
import SubmitButton from "@/components/SubmitButton";
|
import SubmitButton from "@/components/SubmitButton";
|
||||||
import { signIn } from "next-auth/react";
|
import { signIn } from "next-auth/react";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
|
@ -11,7 +10,6 @@ interface FormData {
|
||||||
|
|
||||||
export default function Forgot() {
|
export default function Forgot() {
|
||||||
const [submitLoader, setSubmitLoader] = useState(false);
|
const [submitLoader, setSubmitLoader] = useState(false);
|
||||||
const [showConfirmation, setShowConfirmation] = useState(false);
|
|
||||||
|
|
||||||
const [form, setForm] = useState<FormData>({
|
const [form, setForm] = useState<FormData>({
|
||||||
email: "",
|
email: "",
|
||||||
|
@ -23,20 +21,16 @@ export default function Forgot() {
|
||||||
|
|
||||||
const load = toast.loading("Sending login link...");
|
const load = toast.loading("Sending login link...");
|
||||||
|
|
||||||
const res = await signIn("email", {
|
await signIn("email", {
|
||||||
email: form.email,
|
email: form.email,
|
||||||
callbackUrl: window.location.origin,
|
callbackUrl: "/",
|
||||||
});
|
});
|
||||||
|
|
||||||
setShowConfirmation(true);
|
|
||||||
|
|
||||||
toast.dismiss(load);
|
toast.dismiss(load);
|
||||||
|
|
||||||
setSubmitLoader(false);
|
setSubmitLoader(false);
|
||||||
|
|
||||||
if (!res?.ok) {
|
toast.success("Login link sent.");
|
||||||
toast.error("Invalid login.");
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
toast.error("Please fill out all the fields.");
|
toast.error("Please fill out all the fields.");
|
||||||
}
|
}
|
||||||
|
@ -44,9 +38,6 @@ export default function Forgot() {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{showConfirmation && form.email ? (
|
|
||||||
<EmailConfirmaion email={form.email} />
|
|
||||||
) : undefined}
|
|
||||||
<p className="text-xl font-bold text-center text-sky-500 mt-10 mb-3">
|
<p className="text-xl font-bold text-center text-sky-500 mt-10 mb-3">
|
||||||
Linkwarden
|
Linkwarden
|
||||||
</p>
|
</p>
|
||||||
|
|
|
@ -3,7 +3,6 @@ import { useState } from "react";
|
||||||
import { toast } from "react-hot-toast";
|
import { toast } from "react-hot-toast";
|
||||||
import SubmitButton from "@/components/SubmitButton";
|
import SubmitButton from "@/components/SubmitButton";
|
||||||
import { signIn } from "next-auth/react";
|
import { signIn } from "next-auth/react";
|
||||||
import EmailConfirmaion from "@/components/Modal/EmailConfirmaion";
|
|
||||||
|
|
||||||
const EmailProvider = process.env.NEXT_PUBLIC_EMAIL_PROVIDER;
|
const EmailProvider = process.env.NEXT_PUBLIC_EMAIL_PROVIDER;
|
||||||
|
|
||||||
|
@ -17,7 +16,6 @@ type FormData = {
|
||||||
|
|
||||||
export default function Register() {
|
export default function Register() {
|
||||||
const [submitLoader, setSubmitLoader] = useState(false);
|
const [submitLoader, setSubmitLoader] = useState(false);
|
||||||
const [showConfirmation, setShowConfirmation] = useState(false);
|
|
||||||
|
|
||||||
const [form, setForm] = useState<FormData>({
|
const [form, setForm] = useState<FormData>({
|
||||||
name: "",
|
name: "",
|
||||||
|
@ -50,7 +48,7 @@ export default function Register() {
|
||||||
const sendConfirmation = async () => {
|
const sendConfirmation = async () => {
|
||||||
await signIn("email", {
|
await signIn("email", {
|
||||||
email: form.email,
|
email: form.email,
|
||||||
redirect: false,
|
callbackUrl: "/",
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -76,10 +74,7 @@ export default function Register() {
|
||||||
setSubmitLoader(false);
|
setSubmitLoader(false);
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
if (form.email) {
|
if (form.email) await sendConfirmation();
|
||||||
await sendConfirmation();
|
|
||||||
setShowConfirmation(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
toast.success(
|
toast.success(
|
||||||
EmailProvider
|
EmailProvider
|
||||||
|
@ -99,9 +94,6 @@ export default function Register() {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{showConfirmation && form.email ? (
|
|
||||||
<EmailConfirmaion email={form.email} />
|
|
||||||
) : undefined}
|
|
||||||
<p className="text-xl font-bold text-center my-10 mb-3 text-sky-500">
|
<p className="text-xl font-bold text-center my-10 mb-3 text-sky-500">
|
||||||
Linkwarden
|
Linkwarden
|
||||||
</p>
|
</p>
|
||||||
|
|
Ŝarĝante…
Reference in New Issue