Skip to content
Migrating from NextAuth.js v4? Read our migration guide.
API reference
@auth/firebase-adapter

@auth/firebase-adapter

Official Firebase adapter for Auth.js / NextAuth.js, using the Firebase Admin SDK and Firestore. Firestore logo

Installation

npm install @auth/firebase-adapter firebase-admin

FirebaseAdapterConfig

Configure the Firebase Adapter.

Extends

  • AppOptions

Properties

collections?

optional collections: {
  accounts: string;
  sessions: string;
  users: string;
  verificationTokens: string;
};

Use this option if you already have one of the default collections in your Firestore database.

Example
 // This will use the collection name "authjs_users" instead of the default "users"
 adapter: FirestoreAdapter({ collections: { users: "authjs_users" } })
 // ...
accounts?
optional accounts: string;
sessions?
optional sessions: string;
users?
optional users: string;
verificationTokens?
optional verificationTokens: string;

credential?

optional credential: Credential;

A firebase-admin.app#Credential object used to authenticate the Admin SDK.

See Initialize the SDK for detailed documentation and code samples.

Inherited from

AppOptions.credential

databaseAuthVariableOverride?

optional databaseAuthVariableOverride: null | object;

The object to use as the auth variable in your Realtime Database Rules when the Admin SDK reads from or writes to the Realtime Database. This allows you to downscope the Admin SDK from its default full read and write privileges.

You can pass null to act as an unauthenticated client.

See Authenticate with limited privileges for detailed documentation and code samples.

Inherited from

AppOptions.databaseAuthVariableOverride

databaseURL?

optional databaseURL: string;

The URL of the Realtime Database from which to read and write data.

Inherited from

AppOptions.databaseURL

firestore?

optional firestore: Firestore;

httpAgent?

optional httpAgent: Agent;

An HTTP Agent to be used when making outgoing HTTP calls. This Agent instance is used by all services that make REST calls (e.g. auth, messaging, projectManagement).

Realtime Database and Firestore use other means of communicating with the backend servers, so they do not use this HTTP Agent. Credential instances also do not use this HTTP Agent, but instead support specifying an HTTP Agent in the corresponding factory methods.

Inherited from

AppOptions.httpAgent

name?

optional name: string;

The name of the app passed to initializeApp().

namingStrategy?

optional namingStrategy: "snake_case" | "default";

Use this option if mixed snake_case and camelCase field names in the database is an issue for you. Passing snake_case will convert all field and collection names to snake_case. E.g. the collection verificationTokens will be verification_tokens, and fields like emailVerified will be email_verified instead.

Example
 // This will convert all field and collection names to snake_case
 adapter: FirestoreAdapter({ namingStrategy: "snake_case" })
 // ...
})

projectId?

optional projectId: string;

The ID of the Google Cloud project associated with the App.

Inherited from

AppOptions.projectId

serviceAccountId?

optional serviceAccountId: string;

The ID of the service account to be used for signing custom tokens. This can be found in the client_email field of a service account JSON file.

Inherited from

AppOptions.serviceAccountId

storageBucket?

optional storageBucket: string;

The name of the Google Cloud Storage bucket used for storing application data. Use only the bucket name without any prefixes or additions (do not prefix the name with “gs://”).

Inherited from

AppOptions.storageBucket


FirestoreAdapter()

FirestoreAdapter(config?): Adapter

Parameters

ParameterType
config?FirebaseAdapterConfig | Firestore

Returns

Adapter


initFirestore()

initFirestore(options): Firestore

Utility function that helps making sure that there is no duplicate app initialization issues in serverless environments. If no parameter is passed, it will use the GOOGLE_APPLICATION_CREDENTIALS environment variable to initialize a Firestore instance.

Parameters

ParameterType
optionsAppOptions & { name: string; }

Returns

Firestore

Example

lib/firestore.ts
import { initFirestore } from "@auth/firebase-adapter"
import { cert } from "firebase-admin/app"
 
export const firestore = initFirestore({
 credential: cert({
   projectId: process.env.FIREBASE_PROJECT_ID,
   clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
   privateKey: process.env.FIREBASE_PRIVATE_KEY,
 })
})
Auth.js © Balázs Orbán and Team - 2024