I am using firebase and using firebase admin api to check for authentication status via cookies.
I am doing this call at the layout.tsx level
import { redirect } from 'next/navigation';
import LoggedIn from '@/components/layouts/logged-in';
import { getCurrentUser } from '@/lib/firebase/firebase-admin';
import InvoicesNav from './invoices-nav';
export const metadata = {
title: 'Invoices | Acctual',
};
export default async function InvoiceLayout({
children,
}: {
children: React.ReactNode;
}) {
const currentUser = await getCurrentUser();
if (!currentUser) {
redirect('/login');
}
const createInvoiceAction = {
label: 'Create invoice',
href: '/invoices/new',
};
return (
<LoggedIn title="Invoices" action={createInvoiceAction}>
<div className="w-full">
<InvoicesNav />
<div className="pt-8 pb-6">{children}</div>
</div>
</LoggedIn>
);
}
export async function getCurrentUser() {
const session = await getSession();
if (!(await isUserAuthenticated(session))) {
return null;
}
const decodedIdToken = await auth.verifySessionCookie(session!);
const currentUser = await auth.getUser(decodedIdToken.uid);
return currentUser;
}
Is there a more performant way to do this? I have layouts for separate parts of the app that need to be authenticated, but wondering if there's a better way to do this so that it doesn't take long between navigation switches