skip to Main Content

I want to use firebase admin in the new nextjs app directory build however, I keep running into an error:

./node_modules/firebase-admin/lib/app/credential-internal.js:21:0
Module not found: Can't resolve 'fs'

I used the same setup in a next.js app without the app directory and it works.

"firebase": "^9.22.2",
"firebase-admin": "^11.6.0",
"next": "13.4.5",

Login/page.js
‘use client’
// Admin Firebase SDK
import { AdminAuth } from ‘@/AdminFirebase’;

// SSR
import nookies from "nookies";

export const getServerSideProps = async (context) => {
    try {
        const cookies = nookies.get(context);
        console.log(JSON.stringify(cookies, null, 2));

        const token = await AdminAuth.verifyIdToken(cookies.token);
        const { uid, email } = token;

        // If token exists no need to log in again; redirect to '/' 
        if (token) {
            return {
                redirect: {
                    destination: `/`,
                    permanent: false,
                },
                props: {
                    // email, uid, token
                }
            }

        }

        // User not logged in; no need for any SSR props; Becasue Catch statement will be triggered

        return {
            props: {},

        };
    } catch (err) {

        return {
            props: {

            },
        };
    }
};

2

Answers


  1. Chosen as BEST ANSWER

    Old Methods

    Previous Next.js data fetching methods such as getServerSideProps, getStaticProps, and getInitialProps are not supported in the new App Router. However, you can still use them in the Pages Router.

    Found this on the nextjs docs;

    Once "use client" is defined in a file, all other modules imported into it, including child components, are considered part of the client bundle.


  2. The firebase-admin module is designed for server-side usage, you cannot use it directly on your front-end. For login purposes, you can use the web SDK, or if you want to keep using the admin module, you must create an internal API route to handle the authorization process in the back-end.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search