Skip to content

Instantly share code, notes, and snippets.

@ephraimduncan
Last active March 17, 2023 15:32
Show Gist options
  • Save ephraimduncan/79b86d855ec8010e5e4752026d002aa0 to your computer and use it in GitHub Desktop.
Save ephraimduncan/79b86d855ec8010e5e4752026d002aa0 to your computer and use it in GitHub Desktop.
Auth Wrapper for `next-auth`
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