Registry / security / next-fire-auth

next-fire-auth

JSON →
library0.0.5jsnpmunverified

A lightweight Firebase authentication library for Next.js supporting both SSR and CSR components. Version 0.0.5 provides React context-based auth with automatic Firebase app initialization from environment variables or custom configuration. Key differentiators: minimal setup with automatic cookie-based session management (BaseCookieManager), path-based auth redirects via onPathChange callback, and separate client provider pattern to preserve SSR. Pitfalls: cookie caching issues with push navigation (recommends replace+refresh), requires both firebase and firebase-admin peer deps, and early-stage package with limited documentation. The library ships TypeScript definitions but has low adoption and infrequent updates.

npm install next-fire-auth
INSTALL
IMPORT
SIG · NEXT-FIRE-AUTH
N
next-fire-auth
securityjavascriptv0.0.5
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

NextFireAuthContextProvider
import { NextFireAuthContextProvider } from 'next-fire-auth'
import NextFireAuthContextProvider from 'next-fire-auth'
Named export; default export does not exist. Requires 'use client' directive in the importing component.
BaseCookieManager
import { BaseCookieManager } from 'next-fire-auth'
const BaseCookieManager = require('next-fire-auth').BaseCookieManager
ESM module; require() works but type inference may fail. TypeScript recommended.
useUser
import { useUser } from 'next-fire-auth'
import { useUser } from 'next-fire-auth/useUser'
Incorrect subpath import; the hook is re-exported from the main entry point.

Sets up NextFireAuthContextProvider in a separate client component to preserve SSR, with path-based auth redirects.

// ClientProviders.tsx "use client"; import { NextFireAuthContextProvider } from 'next-fire-auth'; export default function ClientProviders({ children }: { children: React.ReactNode }) { return ( <NextFireAuthContextProvider config={{ loadingComponent: <span>Loading...</span>, cookieManager: undefined, // Use default BaseCookieManager onPathChange: (pathname, user, router) => { if (user && pathname === '/login') { router.replace('/app/dashboard'); router.refresh(); } if (!user && pathname.startsWith('/app')) { router.replace('/login'); router.refresh(); } }, }} > {children} </NextFireAuthContextProvider> ); } // layout.tsx import ClientProviders from './ClientProviders'; export default function RootLayout({ children }: { children: React.ReactNode }) { return ( <html lang="en"> <body> <ClientProviders>{children}</ClientProviders> </body> </html> ); }
Debug
Known issues
gotchaUsing router.push() instead of replace() for auth redirects causes stale cookie caching; always use replace() + refresh().
fix
Use router.replace(pathname); router.refresh(); instead of router.push(pathname).
affects: >=0.0.1
gotchaThe 'use client' directive must be added to any component importing NextFireAuthContextProvider; otherwise Next.js will throw an error.
fix
Add "use client"; at the top of the file using the provider.
affects: >=0.0.1
gotchaEnvironment variables for Firebase Admin (FIREBASE_CLIENT_EMAIL, FIREBASE_PRIVATE_KEY) are required server-side; missing them causes initialization failure.
fix
Set FIREBASE_CLIENT_EMAIL and FIREBASE_PRIVATE_KEY in your .env.local or server environment.
affects: >=0.0.1
gotchaIf you provide a custom firebaseApp via config, you must ensure it is a valid FirebaseApp instance; otherwise the provider may crash silently.
fix
Pass a FirebaseApp instance obtained from initializeApp().
affects: >=0.0.1
deprecatedThe BaseCookieManager export may be removed in future versions; check changelog before upgrading.
fix
Avoid relying on BaseCookieManager directly; pass undefined to use default or implement your own cookie manager.
affects: >=0.0.5
Errors
Common errors & fixes
Error: Must use 'use client' directive in component using NextFireAuthContextProvider
Missing 'use client' directive in the component that imports NextFireAuthContextProvider.
fix
Add "use client"; at the top of the file before any imports.
FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call firebase.initializeApp()
Missing Firebase client configuration; env variables not set or invalid.
fix
Ensure all required NEXT_PUBLIC_FIREBASE_* env variables are set correctly, or pass a custom firebaseApp in config.
TypeError: Cannot read properties of undefined (reading 'signInWithEmailAndPassword')
Firebase Auth module is not initialized or imported incorrectly.
fix
Import 'firebase/auth' in your project and ensure Firebase is properly initialized before using auth methods.
Error: ESM modules cannot be required. Use import instead.
Using require() instead of import with this ESM-only package.
fix
Use import { NextFireAuthContextProvider } from 'next-fire-auth' instead of require().
TypeError: Failed to fetch (or net::ERR_NAME_NOT_RESOLVED)
Firebase Admin SDK cannot reach Firebase servers due to network issues or missing FIREBASE_PRIVATE_KEY.
fix
Verify FIREBASE_PRIVATE_KEY is set correctly (including newlines) and that the Firebase project ID is correct.
Upgrade
Version history
0.0.5latest on npm
Audit
Dependencies
firebaserequiredPeer dependency for client-side Firebase authentication (>=9.0.0 <11)
firebase-adminrequiredPeer dependency for server-side Firebase Admin SDK (>=10.0.0 <12)
reactrequiredPeer dependency for React context and hooks (>=16.8.0)
react-domrequiredPeer dependency for React DOM rendering (>=16.8.0)
Agent activity
8 hits · last 30 days
node
8
Resources
next-fire-auth — npm install next-fire-auth · libregistry