Closed jmif closed 3 years ago
Yeah, it looks like the Firebase admin module is being included in client-side code somewhere. Can you try removing firebase-admin
from everywhere except API routes? Or, where you're importing firebase-admin
, try checking the code with the Next.js code elimination app to ensure it's not bundled for the client.
Yep that was the problem, we had a call to firebase.config()
to pull the cookie secret. We're deployed to firebase functions right now so we can't set environment variables to pass the secret. Ideally we'd only run this code server side, is there a way to achieve this? Only thing I can think of right now is two different configs, one for client and one for server:
server.ts (imported in server code)
export default function initAuth(): void {
init({
...commonConfig,
cookies: {
name: 'auth', // required
// Keys are required unless you set `signed` to `false`.
// The keys cannot be accessible on the client side.
keys: [COOKIE_SECRET],
httpOnly: true,
maxAge: 12 * 60 * 60 * 24 * 1000, // twelve days
overwrite: true,
path: '/',
sameSite: 'strict',
secure: process.env.NODE_ENV === 'production',
signed: true,
},
});
}
client.ts (imported in _app.tsx)
export default function initAuth(): void {
init({
...commonConfig,
cookies: {
name: 'auth', // required
signed: false,
},
});
}
Yes, initializing separately for client and server should work as long as you take care to eliminate the server code from the client bundle. You should also be able to init once and conditionally set the config values based on the client/server context, even without using environment variables.
It looks like Firebase functions do support environment variables. I haven't used this before so not sure if it's helpful for your use case.
Describe the bug I've followed the config examples in the repo but the client JS fails to compile with the following error:
My init auth file (note that I do not supply server side admin config, I initialize admin SDK outside of this file before importing this):
My _app.tsx file:
If I remove the initAuth import and method call then compilation fails but I'm not able to render pages that require auth, I get an error message saying client side auth needs to be configured.
Looks like this is also referenced here. From what I can tell something in the dependency tree of firebase auth is pulling in a server side auth module which imports
child_process
. I've also tried supplying thefirebaseAdminInitConfig
field but the same error occurs.Version
All dependency versions from package json: