Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions docs/docs/adapters/firebase.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ npm install next-auth @next-auth/firebase-adapter

2. Add this adapter to your `pages/api/auth/[...nextauth].js` next-auth configuration object.

```javascript title="pages/api/auth/[...nextauth].js"
```typescript title="pages/api/auth/[...nextauth].ts"
import NextAuth from "next-auth"
import GoogleProvider from "next-auth/providers/google"
import { FirestoreAdapter } from "@next-auth/firebase-adapter"
Expand Down Expand Up @@ -47,13 +47,38 @@ export default NextAuth({
});
```

Additionally, if you've initialized your app on the client you can pass in the project options:

```typescript title="firebase.ts"
import { initializeApp } from "firebase/app"

export const firebaseApp = initializeApp({
// ...firebase options
});
```

```typescript title="pages/api/auth/[...nextauth].ts"
import NextAuth from "next-auth"
import { FirestoreAdapter } from "@next-auth/firebase-adapter"

import { firebaseApp } from "../../../firebase"

export default NextAuth({
providers: [
//...
],
adapter: FirestoreAdapter({ ...firebaseApp.options }),
})

```

## Options

When initializing the firestore adapter, you must pass in the firebase config object with the details from your project. More details on how to obtain that config object can be found [here](https://support.google.com/firebase/answer/7015592).

An example firebase config looks like this:

```js
```typescript
const firebaseConfig = {
apiKey: "AIzaSyDOCAbC123dEf456GhI789jKl01-MnO",
authDomain: "myapp-project-123.firebaseapp.com",
Expand All @@ -70,7 +95,7 @@ See [firebase.google.com/docs/web/setup](https://firebase.google.com/docs/web/se

You can optionally pass in emulator options to automatically connect to your local Firebase emulator.

```js
```typescript
FirestoreAdapter({
// ...
// Passing in an enable object will enable the emulator
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-firebase/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
"jest": "^27.4.3",
"next-auth": "workspace:*"
}
}
}
10 changes: 10 additions & 0 deletions packages/adapter-firebase/src/firebase-initializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getApp, getApps, initializeApp } from "firebase/app"
import type { FirebaseOptions } from "firebase/app"

export const getFirebaseApp = (options: FirebaseOptions) => {
if (getApps().length === 0) {
return initializeApp(options)
}

return getApp()
}
7 changes: 4 additions & 3 deletions packages/adapter-firebase/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import type {
} from "next-auth/adapters"

import { getConverter } from "./converter"
import { getFirebaseApp } from "./firebase-initializer"

type IndexableObject = Record<string, unknown>

export interface FirestoreAdapterOptions {
export interface FirestoreAdapterOptions extends FirebaseOptions {
emulator?: {
host?: string
port?: number
Expand All @@ -37,8 +38,8 @@ export interface FirestoreAdapterOptions {
export function FirestoreAdapter({
emulator,
...firebaseOptions
}: FirebaseOptions & FirestoreAdapterOptions): Adapter {
const firebaseApp = initializeApp(firebaseOptions)
}: FirestoreAdapterOptions): Adapter {
const firebaseApp = getFirebaseApp(firebaseOptions)
const db = getFirestore(firebaseApp)

if (emulator) {
Expand Down
120 changes: 81 additions & 39 deletions packages/adapter-firebase/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,118 @@
import { runBasicTests } from "@next-auth/adapter-test"
import { FirestoreAdapter } from "../src"

import { getFirestore, connectFirestoreEmulator, terminate, collection, query, where, limit, getDocs, getDoc, doc } from "firebase/firestore"
import { initializeApp } from "firebase/app";
import { getConverter } from "../src/converter";
import type { AdapterSession, AdapterUser, VerificationToken } from "next-auth/adapters";
import type { Account } from "next-auth";

const app = initializeApp({ projectId: "next-auth-test" });
const firestore = getFirestore(app);

connectFirestoreEmulator(firestore, 'localhost', 8080);

type IndexableObject = Record<string, unknown>;

const Users = collection(firestore, 'users').withConverter(getConverter<AdapterUser>());
const Sessions = collection(firestore, 'sessions').withConverter(getConverter<AdapterSession & IndexableObject>());
const Accounts = collection(firestore, 'accounts').withConverter(getConverter<Account>());
const VerificationTokens = collection(firestore, 'verificationTokens').withConverter(getConverter<VerificationToken & IndexableObject>({ excludeId: true }));
import {
getFirestore,
connectFirestoreEmulator,
terminate,
collection,
query,
where,
limit,
getDocs,
getDoc,
doc,
} from "firebase/firestore"
import { initializeApp } from "firebase/app"
import { getConverter } from "../src/converter"
import type {
AdapterSession,
AdapterUser,
VerificationToken,
} from "next-auth/adapters"
import type { Account } from "next-auth"

const app = initializeApp({ projectId: "next-auth-test" })
const firestore = getFirestore(app)

connectFirestoreEmulator(firestore, "localhost", 8080)

type IndexableObject = Record<string, unknown>

const Users = collection(firestore, "users").withConverter(
getConverter<AdapterUser>()
)
const Sessions = collection(firestore, "sessions").withConverter(
getConverter<AdapterSession & IndexableObject>()
)
const Accounts = collection(firestore, "accounts").withConverter(
getConverter<Account>()
)
const VerificationTokens = collection(
firestore,
"verificationTokens"
).withConverter(
getConverter<VerificationToken & IndexableObject>({ excludeId: true })
)

runBasicTests({
adapter: FirestoreAdapter({ projectId: "next-auth-test" }),
db: {
async disconnect() {
await terminate(firestore);
await terminate(firestore)
},
async session(sessionToken) {
const snapshotQuery = query(Sessions, where("sessionToken", "==", sessionToken), limit(1));
const snapshots = await getDocs(snapshotQuery);
const snapshot = snapshots.docs[0];
const snapshotQuery = query(
Sessions,
where("sessionToken", "==", sessionToken),
limit(1)
)
const snapshots = await getDocs(snapshotQuery)
const snapshot = snapshots.docs[0]

if (snapshot?.exists() && Sessions.converter) {
const session = Sessions.converter.fromFirestore(snapshot);
const session = Sessions.converter.fromFirestore(snapshot)

return session;
return session
}

return null;
return null
},
async user(id) {
const snapshot = await getDoc(doc(Users, id));
const snapshot = await getDoc(doc(Users, id))

if (snapshot?.exists() && Users.converter) {
const user = Users.converter.fromFirestore(snapshot);
const user = Users.converter.fromFirestore(snapshot)

return user;
return user
}

return null;
return null
},
async account({ provider, providerAccountId }) {
const snapshotQuery = query(Accounts, where("provider", "==", provider), where("providerAccountId", "==", providerAccountId), limit(1));
const snapshots = await getDocs(snapshotQuery);
const snapshot = snapshots.docs[0];
const snapshotQuery = query(
Accounts,
where("provider", "==", provider),
where("providerAccountId", "==", providerAccountId),
limit(1)
)
const snapshots = await getDocs(snapshotQuery)
const snapshot = snapshots.docs[0]

if (snapshot?.exists() && Accounts.converter) {
const account = Accounts.converter.fromFirestore(snapshot);
const account = Accounts.converter.fromFirestore(snapshot)

return account;
return account
}

return null;
return null
},
async verificationToken({ identifier, token }) {
const snapshotQuery = query(VerificationTokens, where("identifier", "==", identifier), where("token", "==", token), limit(1));
const snapshots = await getDocs(snapshotQuery);
const snapshot = snapshots.docs[0];
const snapshotQuery = query(
VerificationTokens,
where("identifier", "==", identifier),
where("token", "==", token),
limit(1)
)
const snapshots = await getDocs(snapshotQuery)
const snapshot = snapshots.docs[0]

if (snapshot?.exists() && VerificationTokens.converter) {
const verificationToken = VerificationTokens.converter.fromFirestore(snapshot);
const verificationToken = VerificationTokens.converter.fromFirestore(
snapshot
)

return verificationToken;
return verificationToken
}
},
},
Expand Down