♻️ Require auth for self hosters with app router (#938)

This commit is contained in:
Luke Vella 2023-11-17 17:22:43 +07:00 committed by GitHub
parent acb55d9c45
commit 1c849eac77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,34 @@
"use client";
import { signIn, useSession } from "next-auth/react";
import React from "react";
import { StandardLayout } from "@/components/layouts/standard-layout";
import { isSelfHosted } from "@/utils/constants";
const Auth = ({ children }: { children: React.ReactNode }) => {
const session = useSession();
const isAuthenticated = !!session.data?.user.email;
React.useEffect(() => {
if (!isAuthenticated) {
signIn();
}
}, [isAuthenticated]);
if (isAuthenticated) {
return <>{children}</>;
}
return null;
};
export default function Layout({ children }: { children: React.ReactNode }) {
if (isSelfHosted) {
return (
<Auth>
<StandardLayout>{children}</StandardLayout>
</Auth>
);
}
return <StandardLayout>{children}</StandardLayout>;
}