Last active
March 17, 2023 15:32
-
-
Save ephraimduncan/79b86d855ec8010e5e4752026d002aa0 to your computer and use it in GitHub Desktop.
Auth Wrapper for `next-auth`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useSession } from "next-auth/react"; | |
import { useRouter } from "next/router"; | |
export default function WithAuth({ children }: { children: React.ReactNode }): JSX.Element { | |
const router = useRouter(); | |
const { data, status } = useSession({ | |
required: true, | |
onUnauthenticated() { | |
router.push("/api/auth/signin"); | |
}, | |
}); | |
if (status === "loading") { | |
return <div>Page Loading...</div>; | |
} | |
return <>{children}</>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment