Skip to content

Commit

Permalink
navigate to login if unauthenticated
Browse files Browse the repository at this point in the history
  • Loading branch information
Anchel123 committed May 1, 2024
1 parent 398ab27 commit 1afc3c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
20 changes: 20 additions & 0 deletions app/loginVerification.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use client"

import { signOut, useSession } from "next-auth/react"
import { usePathname } from "next/navigation"
import { useEffect } from "react"

export default function LoginVerification({ children }: { children: React.ReactNode }) {

const { status } = useSession()
const url = usePathname()

useEffect(() => {
if (url === "/login") return
if (status === "unauthenticated") {
signOut({ callbackUrl: "/login" })
}
}, [status, url])

return children
}
3 changes: 2 additions & 1 deletion app/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ImperativePanelHandle } from "react-resizable-panels";
import Navbar from "@/components/custom/navbar";
import { ChevronLeft, ChevronRight } from "lucide-react";
import useScreenSize from "./useScreenSize";
import LoginVerification from "./loginVerification";

export default function NextAuthProvider({ children }: { children: React.ReactNode }) {

Expand Down Expand Up @@ -56,7 +57,7 @@ export default function NextAuthProvider({ children }: { children: React.ReactNo
</button>
<Navbar collapsed={isCollapsed} />
</ResizablePanel>
<ResizablePanel defaultSize={100 - panelSize}>{children}</ResizablePanel>
<ResizablePanel defaultSize={100 - panelSize}><LoginVerification>{children}</LoginVerification></ResizablePanel>
</ResizablePanelGroup>
</ThemeProvider>
</SessionProvider>
Expand Down

0 comments on commit 1afc3c0

Please sign in to comment.