diff --git a/apps/X/app/favicon.ico b/apps/X/app/favicon.ico new file mode 100644 index 0000000..2dfdd30 Binary files /dev/null and b/apps/X/app/favicon.ico differ diff --git a/apps/X/app/home/page.tsx b/apps/X/app/home/page.tsx index bdb58f7..c0d1a0f 100644 --- a/apps/X/app/home/page.tsx +++ b/apps/X/app/home/page.tsx @@ -1,6 +1,11 @@ -import { CenterComp, HomeLeft, HomeRight } from "@/components/ui"; +"use client"; +import { HomeRight, Button, CenterComp, HomeLeft } from "@/components/ui"; +import { signOut } from "next-auth/react"; const homepage = () => { + const handleLogout = async () => { + await signOut({ callbackUrl: "/" }); + }; return (
diff --git a/apps/X/app/icon.svg b/apps/X/app/icon.svg deleted file mode 100644 index bff9881..0000000 --- a/apps/X/app/icon.svg +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/apps/X/app/layout.tsx b/apps/X/app/layout.tsx index 28ea232..9ef1e65 100644 --- a/apps/X/app/layout.tsx +++ b/apps/X/app/layout.tsx @@ -1,7 +1,8 @@ import type { Metadata } from "next"; import localFont from "next/font/local"; import "./globals.css"; -import { Provieder } from "./provider"; +import { Provider } from "./provider"; +import { RouteGard } from "@/components/RouteGuard"; const geistSans = localFont({ src: "./fonts/GeistVF.woff", @@ -24,11 +25,11 @@ export default function RootLayout({ }>) { return ( - + - {children} + {children} - + ); } diff --git a/apps/X/app/lib/auth.ts b/apps/X/app/lib/auth.ts index d41c137..1c1acf6 100644 --- a/apps/X/app/lib/auth.ts +++ b/apps/X/app/lib/auth.ts @@ -4,6 +4,7 @@ import { JWT } from "next-auth/jwt"; import CredentialsProvider from "next-auth/providers/credentials"; import GithubProvider from "next-auth/providers/github"; import GoogleProvider from "next-auth/providers/google"; +import { signOut } from "next-auth/react"; import z from "zod"; interface credentialsTypes { @@ -15,8 +16,8 @@ interface credentialsTypes { const userInput = z.object({ username: z.string(), - name: z.string(), - email: z.string().email(), + name: z.string().optional(), + email: z.string().email().optional(), password: z.string(), }); @@ -52,7 +53,11 @@ export const authOptions = { name: creds.name, }); }; - + const validationResult = validatedUserInput(credentials); + if (!validationResult.success) { + console.log("Validation failed", validationResult.error); + return null; + } if (!validatedUserInput) return null; const hashedPassword = await bcrypt.hash(credentials.password, 10); @@ -70,7 +75,6 @@ export const authOptions = { credentials.password, existingUser.password ); - console.log("This is the password", passwordValidation); if (passwordValidation) { console.log("This is userEmail", existingUser.email); console.log("This is username", existingUser.username); @@ -80,8 +84,9 @@ export const authOptions = { email: existingUser.email, name: existingUser.name, }; + } else { + console.log("Invalid password for existing user"); } - console.log("This is name", existingUser.name); } catch (error) { console.log("Error while LogIn", error); @@ -119,6 +124,8 @@ export const authOptions = { async jwt({ token, user }: any) { if (user) { token.id = user.id; + token.username = user.username; + token.email = user.email; } return token; }, @@ -127,14 +134,17 @@ export const authOptions = { // where: { id: token.sub }, // }); - if (token) { - session.user.id = token.sub; + if (token && session.user) { + session.user.id = token.id || null; + session.user.username = token.username || null; + session.user.email = token.email || null; } - return session; }, }, pages: { signIn: "/signin", + error: "/error", + signOut: "/signin", }, }; diff --git a/apps/X/app/page.tsx b/apps/X/app/page.tsx index 6c256a4..b678fa6 100644 --- a/apps/X/app/page.tsx +++ b/apps/X/app/page.tsx @@ -4,15 +4,14 @@ import { redirect } from "next/navigation"; const Page = async () => { const session = await getServerSession(authOptions); + console.log(session, "This is the session log"); - if (!session) { - console.log("No session,redirecting to SIGNIN"); + if (!session?.user?.id) { + console.log("No valid session, redirecting to signin"); redirect("/signin"); } - if (session.user && session.user.id) { - redirect("/home"); - } - return
Loading......
; + console.log("Valid session detected, redirecting to /home"); + redirect("/home"); }; export default Page; diff --git a/apps/X/app/provider.tsx b/apps/X/app/provider.tsx index 9ace421..4d564ee 100644 --- a/apps/X/app/provider.tsx +++ b/apps/X/app/provider.tsx @@ -1,6 +1,6 @@ "use client"; import { SessionProvider } from "next-auth/react"; -export const Provieder = ({ children }: { children: React.ReactNode }) => { +export const Provider = ({ children }: { children: React.ReactNode }) => { return {children}; }; diff --git a/apps/X/src/components/RouteGuard.tsx b/apps/X/src/components/RouteGuard.tsx new file mode 100644 index 0000000..6666248 --- /dev/null +++ b/apps/X/src/components/RouteGuard.tsx @@ -0,0 +1,38 @@ +"use client"; +import { useSession } from "next-auth/react"; +import { usePathname, useRouter } from "next/navigation"; +import { useEffect } from "react"; + +const publicPaths = ["/signin", "/login", "/error"]; + +export function RouteGard({ children }: { children: React.ReactNode }) { + const { data: session, status } = useSession(); + const router = useRouter(); + const pathname = usePathname(); + useEffect(() => { + if (status == "loading") { + return; + } + if (!session && !publicPaths.includes(pathname)) { + router.push("/signin"); + } + }, [session, status, router, pathname]); + + if (status == "loading") { + return ( +
+ +
+ ); + } + + return <>{children}; +} diff --git a/apps/X/src/components/home/HomeLeft.tsx b/apps/X/src/components/home/HomeLeft.tsx index e00b839..19a7732 100644 --- a/apps/X/src/components/home/HomeLeft.tsx +++ b/apps/X/src/components/home/HomeLeft.tsx @@ -1,3 +1,4 @@ +"use client" export const HomeLeft = () => { return
Home Left
; }; diff --git a/apps/X/src/components/home/HomeRight.tsx b/apps/X/src/components/home/HomeRight.tsx index 734edaa..98547d2 100644 --- a/apps/X/src/components/home/HomeRight.tsx +++ b/apps/X/src/components/home/HomeRight.tsx @@ -1,3 +1,4 @@ +"use client" export const HomeRight = () => { return
Home Right Side
; }; diff --git a/apps/X/src/components/ui/X_logo.tsx b/apps/X/src/components/ui/X_logo.tsx index 7fcfeb7..0914049 100644 --- a/apps/X/src/components/ui/X_logo.tsx +++ b/apps/X/src/components/ui/X_logo.tsx @@ -5,7 +5,7 @@ export const X_logo = () => {