Skip to content

Commit

Permalink
[fix]: mutliple re-rendering of toast on sign-in process
Browse files Browse the repository at this point in the history
  • Loading branch information
SiddharthaMishra-dev committed Feb 29, 2024
1 parent 8d64004 commit 25a111d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
30 changes: 18 additions & 12 deletions app/signin/page.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
"use client";
import { useSession, signIn } from "next-auth/react";
import { FaGithub, FaGoogle } from "react-icons/fa";
import { Button } from "@nextui-org/button";
import { Card, CardHeader, CardBody } from "@nextui-org/card";
import { Card, CardBody, CardHeader } from "@nextui-org/card";
import { signIn, useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import toast from "react-hot-toast";
import { useEffect } from "react";
import { FaGithub, FaGoogle } from "react-icons/fa";
import Loading from "./loading";
import { useEffect, useState } from "react";

const Page = () => {
const { data: session } = useSession();
const { data: session, status } = useSession();
const [isLoggedIn, setIsLoggedIn] = useState(false);

const router = useRouter();

const handleSignIn = async (provider: string) => {
try {
await signIn(provider);
// Redirect to home page after sign-in
router.push("/");
toast.success("Logged in successfully");
} catch (error) {
toast.error("Sign in failed");
console.error("Sign in error:", error);
}
};

useEffect(() => {
if (session) {
router.push(`/`);
toast.success("Logged in successfully");
if (session && !isLoggedIn) {
setIsLoggedIn(true);
toast.success("Logged In");
router.push("/");
}
}, [session, router]);
}, [session, isLoggedIn]);

if (status === "loading") {
return <Loading />;
}

return (
<>
<div className="h-full w-full p-6 ">
Expand Down
5 changes: 3 additions & 2 deletions components/Loader.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { CircularProgress } from "@nextui-org/progress";
import { Loader2 } from "lucide-react";

const Loader = () => {
return (
<div className="h-screen w-full flex justify-center items-center">
<CircularProgress aria-label="Loading ..." />
{/* <CircularProgress aria-label="Loading ..." /> */}
<Loader2 className="animate-spin text-blue-600 h-32 w-32" />
</div>
);
};
Expand Down
22 changes: 22 additions & 0 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getServerSession } from "next-auth";
import { NextRequest, NextResponse } from "next/server";
import { authOptions } from "./config/authoptions";
import { getToken } from "next-auth/jwt";
// export { default } from "next-auth/middleware";

export async function middleware(req: NextRequest) {
// const { url } = req;
// const session = await getServerSession(authOptions);
// const token = await getToken({req});

// console.log(token);

// if (session && ["/signin"].includes(nextUrl.pathname)) {
// return NextResponse.redirect("http://localhost:3000/");
// }

// console.log(req);
return NextResponse.next();
}

// export const config = { matcher: ["/signin"] };

0 comments on commit 25a111d

Please sign in to comment.