@next-auth/firebase-adapter
Installationβ
- npm
- yarn
- pnpm
npm install next-auth @next-auth/firebase-adapter firebase-admin
yarn add next-auth @next-auth/firebase-adapter firebase-admin
pnpm add next-auth @next-auth/firebase-adapter firebase-admin
FirestoreAdapter()β
Setupβ
First, create a Firebase project and generate a service account key. Visit: https://console.firebase.google.com/u/0/project/{project-id}/settings/serviceaccounts/adminsdk
(replace {project-id}
with your project's id)
Now you have a few options to authenticate with the Firebase Admin SDK in your app:
Environment variablesβ
- Download the service account key and save it in your project. (Make sure to add the file to your
.gitignore
!) - Add
GOOGLE_APPLICATION_CREDENTIALS
to your environment variables and point it to the service account key file. - The adapter will automatically pick up the environment variable and use it to authenticate with the Firebase Admin SDK.
Exampleβ
import NextAuth from "next-auth";
import { FirestoreAdapter } from "@next-auth/firebase-adapter";
export default NextAuth({
adapter: FirestoreAdapter(),
// ...
});
Service account valuesβ
- Download the service account key to a temporary location. (Make sure to not commit this file to your repository!)
- Add the following environment variables to your project:
FIREBASE_PROJECT_ID
,FIREBASE_CLIENT_EMAIL
,FIREBASE_PRIVATE_KEY
. - Pass the config to the adapter, using the environment variables as shown in the example below.
Exampleβ
import NextAuth from "next-auth";
import { FirestoreAdapter } from "@next-auth/firebase-adapter";
import { cert } from "firebase-admin/app";
export default NextAuth({
adapter: FirestoreAdapter({
credential: cert({
projectId: process.env.FIREBASE_PROJECT_ID,
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
privateKey: process.env.FIREBASE_PRIVATE_KEY,
}),
}),
// ...
});
Using an existing Firestore instanceβ
If you already have a Firestore instance, you can pass that to the adapter directly instead.
When passing an instance and in a serverless environment, remember to handle duplicate app initialization.
You can use the initFirestore utility to initialize the app and get an instance safely.
Exampleβ
import NextAuth from "next-auth";
import { FirestoreAdapter } from "@next-auth/firebase-adapter";
import { firestore } from "lib/firestore";
export default NextAuth({
adapter: FirestoreAdapter(firestore),
// ...
});
FirestoreAdapter(
config
?:FirebaseAdapterConfig
|Firestore
):Adapter
Parametersβ
Parameter | Type |
---|---|
config? | FirebaseAdapterConfig | Firestore |
Returnsβ
Adapter
initFirestore()β
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.
Exampleβ
import { initFirestore } from "@next-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,
}),
});
initFirestore(
options
:AppOptions
& {name
?:string
;} ={}
):Firestore
Parametersβ
Parameter | Type |
---|---|
options | AppOptions & {name ?: string ;} |
Returnsβ
Firestore
FirebaseAdapterConfigβ
Configure the Firebase Adapter.
Propertiesβ
name?β
name:
string
The name of the app passed to initializeApp()
.
namingStrategy?β
namingStrategy: "snake_case"
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β
import NextAuth from "next-auth";
import { FirestoreAdapter } from "@next-auth/firebase-adapter";
export default NextAuth({
adapter: FirestoreAdapter({ namingStrategy: "snake_case" }),
// ...
});