admin管理员组

文章数量:1277875

I'm trying to get my firebase uid on a nextjs client rendered page.

This is my function to get the uid from the session cookie.

import "server-only";    

export async function isUserAuthenticated(session: string | undefined = undefined) {
    const _session = session ?? (await getSession());
    if (!_session) return false;

    try {
        const decodedIdToken = await auth.verifySessionCookie(_session, true);
        return decodedIdToken.uid;
    } catch (error) {
        console.log(error);
        return false;
    }
}

The problem is, for security reasons, I can only run this in server components. verifySessionCookie() is part of the firebase-admin lib (here).

I'd love to pass this down from my layout.tsx but react doesn't allow passing props to {children} and context in server components are not supported either. The only way I can think to do this is to make every single page.tsx a server component and then pass down the uid as props from there. But this seems horrible, I'd be replicating the same code for every single URL. Is there any other solution besides this?

本文标签: javascriptHow to get uid on client rendered pages (firebasenextJS)Stack Overflow