Compare commits

..

11 Commits

Author SHA1 Message Date
Balázs Orbán
354b03471c Delete index.md 2023-02-04 15:33:16 +01:00
Balázs Orbán
0a7286e857 remove CSS hacks 2023-02-03 15:56:50 +01:00
Balázs Orbán
cf544d6ec7 remove gitignored files 2023-02-03 15:55:54 +01:00
Balázs Orbán
84e14d76b3 fix paths 2023-02-03 15:24:57 +01:00
Balázs Orbán
7794b6dfbb pre-build packages before docs dev script with turbo 2023-02-03 15:16:44 +01:00
Balázs Orbán
d195381224 update gitignore 2023-02-03 15:16:31 +01:00
Balázs Orbán
b3d5ec596b update typedoc/docusaurus config 2023-02-03 15:16:27 +01:00
Balázs Orbán
34f8f36038 rename main entry points to index 2023-02-03 15:16:02 +01:00
Balázs Orbán
a79a5d6cbe update lock file 2023-02-03 15:15:35 +01:00
Balázs Orbán
cac71774a6 move nuxt postinstall to dev and build scripts 2023-02-03 15:15:22 +01:00
Balázs Orbán
7376f10cac chore: upgradde typedoc plugins 2023-02-03 15:15:08 +01:00
22 changed files with 1824 additions and 1861 deletions

8
.gitignore vendored
View File

@@ -12,7 +12,6 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
firebase-debug.log
ui-debug.log
.pnpm-debug.log
@@ -79,6 +78,9 @@ docs/.docusaurus
docs/providers.json
# Core
packages/core/*.js
packages/core/*.d.ts
packages/core/*.d.ts.map
packages/core/src/providers/oauth-types.ts
packages/core/lib
packages/core/providers
@@ -94,7 +96,3 @@ packages/frameworks-sveltekit/.svelte-kit
packages/frameworks-sveltekit/package
packages/frameworks-sveltekit/vite.config.js.timestamp-*
packages/frameworks-sveltekit/vite.config.ts.timestamp-*
# Adapters
docs/docs/reference/adapter

View File

@@ -22,7 +22,7 @@ Using a JWT to store the `refresh_token` is less secure than saving it in a data
#### JWT strategy
Using the [jwt](../../reference/core/types#jwt) and [session](../../reference/core/types#session) callbacks, we can persist OAuth tokens and refresh them when they expire.
Using the [jwt](../../reference/core/interfaces/types.CallbacksOptions.md#jwt) and [session](../../reference/core/interfaces/types.CallbacksOptions.md#session) callbacks, we can persist OAuth tokens and refresh them when they expire.
Below is a sample implementation using Google's Identity Provider. Please note that the OAuth 2.0 request in the `refreshAccessToken()` function will vary between different providers, but the core logic should remain similar.

View File

@@ -0,0 +1,75 @@
---
id: firebase
title: Firebase
---
:::warning
This adapter is still experimental and does not work with Auth.js 4 or newer. If you would like to help out upgrading it, please visit [this PR](https://github.com/nextauthjs/next-auth/pull/3873)
:::
This is the Firebase Adapter for [`next-auth`](https://authjs.dev). This package can only be used in conjunction with the primary `next-auth` package. It is not a standalone package.
## Getting Started
1. Install the necessary packages
```bash npm2yarn
npm install next-auth @next-auth/firebase-adapter@experimental
```
2. Add this adapter to your `pages/api/auth/[...nextauth].js` next-auth configuration object.
```javascript title="pages/api/auth/[...nextauth].js"
import NextAuth from "next-auth"
import GoogleProvider from "next-auth/providers/google"
import { FirebaseAdapter } from "@next-auth/firebase-adapter"
import firebase from "firebase/app"
import "firebase/firestore"
const firestore = (
firebase.apps[0] ?? firebase.initializeApp(/* your config */)
).firestore()
// For more information on each option (and a full list of options) go to
// https://authjs.dev/reference/configuration/auth-options
export default NextAuth({
// https://authjs.dev/reference/providers/
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
],
adapter: FirebaseAdapter(firestore),
...
})
```
## 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
const firebaseConfig = {
apiKey: "AIzaSyDOCAbC123dEf456GhI789jKl01-MnO",
authDomain: "myapp-project-123.firebaseapp.com",
databaseURL: "https://myapp-project-123.firebaseio.com",
projectId: "myapp-project-123",
storageBucket: "myapp-project-123.appspot.com",
messagingSenderId: "65211879809",
appId: "1:65211879909:web:3ae38ef1cdcb2e01fe5f0c",
measurementId: "G-8GSGZQ44ST",
}
```
See [firebase.google.com/docs/web/setup](https://firebase.google.com/docs/web/setup) for more details.
:::tip **From Firebase**
**Caution**: We do not recommend manually modifying an app's Firebase config file or object. If you initialize an app with invalid or missing values for any of these required "Firebase options", then your end users may experience serious issues.
For open source projects, we generally do not recommend including the app's Firebase config file or object in source control because, in most cases, your users should create their own Firebase projects and point their apps to their own Firebase resources (via their own Firebase config file or object).
:::

View File

@@ -226,21 +226,6 @@ const docusaurusConfig = {
},
},
],
[
"docusaurus-plugin-typedoc",
{
...typedocConfig,
id: "firebase-adapter",
plugin: [require.resolve("./typedoc-mdn-links")],
watch: process.env.TYPEDOC_WATCH,
entryPoints: ["../packages/adapter-firebase/src/index.ts"],
tsconfig: "../packages/adapter-firebase/tsconfig.json",
out: "reference/adapter/firebase",
sidebar: {
indexLabel: "Firebase",
},
},
],
],
}

View File

@@ -50,8 +50,12 @@ module.exports = {
label: "Database Adapters",
link: { type: "doc", id: "reference/adapters/overview" },
items: [
{ type: "doc", id: "reference/adapter/firebase/index" },
{ type: "autogenerated", dirName: "reference/06-adapters" },
{
type: "autogenerated",
dirName: "reference/06-adapters",
// See: https://github.com/facebook/docusaurus/issues/5689
// exclude: ["index"],
},
],
},
{

View File

@@ -0,0 +1 @@
{}

View File

@@ -0,0 +1,16 @@
# Change Log
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [0.1.3](https://github.com/nextauthjs/adapters/compare/@next-auth/firebase-adapter@0.1.2...@next-auth/firebase-adapter@0.1.3) (2021-08-17)
**Note:** Version bump only for package @next-auth/firebase-adapter
## [0.1.2](https://github.com/nextauthjs/adapters/compare/@next-auth/firebase-adapter@0.1.1...@next-auth/firebase-adapter@0.1.2) (2021-07-02)
**Note:** Version bump only for package @next-auth/firebase-adapter
## [0.1.1](https://github.com/nextauthjs/adapters/compare/@next-auth/firebase-adapter@0.1.0...@next-auth/firebase-adapter@0.1.1) (2021-06-30)
**Note:** Version bump only for package @next-auth/firebase-adapter

View File

@@ -1,8 +1,8 @@
<p align="center">
<br/>
<a href="https://authjs.dev" target="_blank">
<img height="64px" src="https://authjs.dev/img/logo/logo-sm.png" /></a><img height="64px" src="https://raw.githubusercontent.com/nextauthjs/next-auth/main/packages/adapter-firebase/logo.svg" />
<h3 align="center"><b>Firebase Adapter</b> - Auth.js</h3>
<img height="64px" src="https://authjs.dev/img/logo/logo-sm.png" /></a><img height="64px" src="https://raw.githubusercontent.com/nextauthjs/adapters/main/packages/firebase/logo.svg" />
<h3 align="center"><b>Firebase Adapter</b> - NextAuth.js</h3>
<p align="center">
Open Source. Full Stack. Own Your Data.
</p>
@@ -13,12 +13,72 @@
</p>
</p>
## Overview
This is the official Firebase Adapter for [Auth.js](https://authjs.dev) / [NextAuth.js](https://next-auth.js.org/), using the [Firebase Admin SDK](https://firebase.google.com/docs/admin/setup) and [Firestore](https://firebase.google.com/docs/firestore).
This is the Firebase Adapter for [`auth.js`](https://authjs.dev). This package can only be used in conjunction with the primary `next-auth` package. It is not a standalone package.
## Documentation
You can find more Firebase information in the docs at [authjs.dev/reference/adapters/firebase](https://authjs.dev/reference/adapters/firebase).
Check out the [documentation](https://authjs.dev/reference/adapter/firebase) to learn how to use this adapter in your project.
## Getting Started
1. Install `next-auth` and `@next-auth/firebase-adapter`.
```js
npm install next-auth @next-auth/firebase-adapter
```
2. Add this adapter to your `pages/api/[...nextauth].js` next-auth configuration object.
```js
import NextAuth from "next-auth"
import Providers from "next-auth/providers"
import { FirestoreAdapter } from "@next-auth/firebase-adapter"
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore"
const app = initializeApp({ projectId: "next-auth-test" });
const firestore = getFirestore(app);
// For more information on each option (and a full list of options) go to
// https://authjs.dev/reference/configuration/auth-options
export default NextAuth({
// https://authjs.dev/reference/providers/oauth-builtin
providers: [
Providers.Google({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
],
adapter: FirestoreAdapter(firestore),
...
})
```
## 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
const firebaseConfig = {
apiKey: "AIzaSyDOCAbC123dEf456GhI789jKl01-MnO",
authDomain: "myapp-project-123.firebaseapp.com",
databaseURL: "https://myapp-project-123.firebaseio.com",
projectId: "myapp-project-123",
storageBucket: "myapp-project-123.appspot.com",
messagingSenderId: "65211879809",
appId: "1:65211879909:web:3ae38ef1cdcb2e01fe5f0c",
measurementId: "G-8GSGZQ44ST",
}
```
See [firebase.google.com/docs/web/setup](https://firebase.google.com/docs/web/setup) for more details.
> **From Firebase - Caution**: We do not recommend manually modifying an app's Firebase config file or object. If you initialize an app with invalid or missing values for any of these required "Firebase options", then your end users may experience serious issues.
>
> For open source projects, we generally do not recommend including the app's Firebase config file or object in source control because, in most cases, your users should create their own Firebase projects and point their apps to their own Firebase resources (via their own Firebase config file or object).
## Contributing

View File

@@ -1,8 +1,5 @@
{
"firestore": {
"rules": "firestore.rules"
},
"emulator": {
"emulators": {
"firestore": {
"port": 8080
}

View File

@@ -1,10 +0,0 @@
rules_version = '2';
// Deny read/write access to all users under any conditions
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
}
}

View File

@@ -0,0 +1 @@
module.exports = require("@next-auth/adapter-test/jest/jest-preset")

View File

@@ -33,4 +33,4 @@
<circle cx="144" cy="144" r="40" fill="#757575"/>
<path d="M144 146l-18 8v-8l18-8 18 8v7-1.5 2.5zm0-22l18 8v8l-18-8-18 8v-8zm6.75 29l9 4-15.75 7v-8z" fill="#fff" fill-rule="evenodd"/>
</g>
</svg>
</svg>

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB

View File

@@ -12,44 +12,35 @@
"Nico Domino <yo@ndo.dev>",
"Alex Meuer <github@alexmeuer.com>"
],
"type": "module",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./index.js"
}
},
"main": "dist/index.js",
"files": [
"src",
"*.js",
"*.d.ts*"
"dist",
"index.d.ts"
],
"license": "ISC",
"keywords": [
"next-auth",
"next.js",
"firebase",
"firebase-admin"
"firebase"
],
"private": false,
"publishConfig": {
"access": "public"
},
"scripts": {
"dev": "tsc -w",
"build": "tsc",
"test": "firebase emulators:exec --only firestore --project next-auth-test 'jest -c tests/jest.config.js'"
"test": "FIRESTORE_EMULATOR_HOST=localhost:8080 firebase --token '$FIREBASE_TOKEN' emulators:exec --only firestore --project next-auth-test jest"
},
"peerDependencies": {
"firebase-admin": "^11.4.1",
"firebase": "^9.7.0",
"next-auth": "^4"
},
"devDependencies": {
"@next-auth/adapter-test": "workspace:*",
"@next-auth/tsconfig": "workspace:*",
"firebase-admin": "^11.4.1",
"firebase": "^9.14.0",
"firebase-tools": "^11.16.1",
"jest": "^29.3.1",
"jest": "^27.4.3",
"next-auth": "workspace:*"
}
}

View File

@@ -0,0 +1,58 @@
import { Timestamp } from "firebase/firestore"
import type {
FirestoreDataConverter,
QueryDocumentSnapshot,
WithFieldValue,
} from "firebase/firestore"
const isTimestamp = (value: unknown): value is Timestamp =>
typeof value === "object" && value !== null && value instanceof Timestamp
interface GetConverterOptions {
excludeId?: boolean
}
export const getConverter = <Document extends Record<string, unknown>>(
options?: GetConverterOptions
): FirestoreDataConverter<Document> => ({
// `PartialWithFieldValue` implicitly types `object` as `any`, so we want to explicitly type it
toFirestore(object: WithFieldValue<Document>) {
const document: Record<string, unknown> = {}
Object.keys(object).forEach((key) => {
if (object[key] !== undefined) {
document[key] = object[key]
}
})
return document
},
// We need to explicitly type `snapshot` since it uses `DocumentData` for generic type
fromFirestore(snapshot: QueryDocumentSnapshot<Document>) {
if (!snapshot.exists()) {
return snapshot
}
let document: Document = snapshot.data()
if (!options?.excludeId) {
document = {
...document,
id: snapshot.id,
}
}
for (const key in document) {
const value = document[key]
if (isTimestamp(value)) {
document = {
...document,
[key]: value.toDate(),
}
}
}
return document
},
})

View File

@@ -0,0 +1,11 @@
import { initializeApp, getApps, FirebaseOptions } from "firebase/app"
export default function getFirebase(firebaseOptions: FirebaseOptions) {
const apps = getApps()
const app = apps.find((app) => app.name === firebaseOptions.projectId)
if (app) {
return app
} else {
return initializeApp(firebaseOptions)
}
}

View File

@@ -1,302 +1,283 @@
/**
* <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding: 16}}>
* <span>
* Official <b>Firebase</b> adapter for Auth.js / NextAuth.js,
* using the <a href="https://firebase.google.com/docs/admin/setup">Firebase Admin SDK</a>
* &nbsp;and <a href="https://firebase.google.com/docs/firestore">Firestore</a>.</span>
* <a href="https://firebase.google.com/">
* <img style={{display: "block"}} src="https://raw.githubusercontent.com/nextauthjs/next-auth/main/packages/adapter-firebase/logo.svg" height="48" width="48"/>
* </a>
* </div>
*
* ## Installation
*
* ```bash npm2yarn2pnpm
* npm install next-auth @next-auth/firebase-admin-adapter firebase-admin
* ```
*
* ## References
* - [`GOOGLE_APPLICATION_CREDENTIALS` environment variable](https://cloud.google.com/docs/authentication/application-default-credentials#GAC)
* - [Firebase Admin SDK setup](https://firebase.google.com/docs/admin/setup#initialize-sdk)
*
* @module @next-auth/firebase-adapter
*/
import { type AppOptions } from "firebase-admin"
import { Firestore } from "firebase-admin/firestore"
import type { Adapter, AdapterUser } from "next-auth/adapters"
import { initializeApp } from "firebase/app"
import type { FirebaseOptions } from "firebase/app"
import {
collestionsFactory,
deleteDocs,
initFirestore,
addDoc,
collection,
deleteDoc,
doc,
getDoc,
getOneDoc,
mapFieldsFactory,
} from "./utils"
getDocs,
getFirestore,
limit,
query,
runTransaction,
setDoc,
where,
connectFirestoreEmulator,
} from "firebase/firestore"
export { initFirestore } from "./utils"
import type {
Adapter,
AdapterUser,
AdapterAccount,
AdapterSession,
VerificationToken,
} from "next-auth/adapters"
/** Configure the Firebase Adapter. */
export interface FirebaseAdapterConfig extends AppOptions {
/**
* The name of the app passed to {@link https://firebase.google.com/docs/reference/admin/node/firebase-admin.md#initializeapp `initializeApp()`}.
*/
name?: string
firestore?: Firestore
/**
* 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
* ```ts title="pages/api/auth/[...nextauth].ts"
* import NextAuth from "next-auth"
* import { FirestoreAdapter } from "@next-auth/firebase-adapter"
*
* export default NextAuth({
* adapter: FirestoreAdapter({ namingStrategy: "snake_case" })
* // ...
* })
* ```
*/
namingStrategy?: "snake_case"
import { getConverter } from "./converter"
import getFirebase from "./getFirebase"
export type IndexableObject = Record<string, unknown>
export interface FirestoreAdapterOptions {
emulator?: {
host?: string
port?: number
}
}
/**
* #### Usage
*
* 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:
*
* ##### 1. `GOOGLE_APPLICATION_CREDENTIALS` environment variable:
* - Download the service account key and save it in your project. (Make sure to add the file to your `.gitignore`!)
* - Add [`GOOGLE_APPLICATION_CREDENTIALS`](https://cloud.google.com/docs/authentication/application-default-credentials#GAC) 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
* ```ts title="pages/api/auth/[...nextauth].ts"
* import NextAuth from "next-auth"
* import { FirestoreAdapter } from "@next-auth/firebase-adapter"
*
* export default NextAuth({
* adapter: FirestoreAdapter(),
* // ...
* })
* ```
*
* ##### 2. Service account values as environment variables
*
* - 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
* ```ts title="pages/api/auth/[...nextauth].ts"
* 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,
* })
* })
* // ...
* })
* ```
*
* ##### 3. Use an existing Firestore instance
*
* If you already have a Firestore instance, you can pass that to the adapter directly instead.
*
* :::note
* When passing an instance and in a serverless environment, remember to handle duplicate app initialization.
* :::
*
* :::tip
* You can use the {@link initFirestore} utility to initialize the app and get an instance safely.
* :::
*
* @example
* ```ts title="pages/api/auth/[...nextauth].ts"
* import NextAuth from "next-auth"
* import { FirestoreAdapter } from "@next-auth/firebase-adapter"
* import { firestore } from "lib/firestore"
*
* export default NextAuth({
* adapter: FirestoreAdapter(firestore),
* // ...
* })
* ```
*/
export function FirestoreAdapter(
config?: FirebaseAdapterConfig | Firestore
): Adapter {
const { db, namingStrategy = "default" } =
config instanceof Firestore
? { db: config }
: { ...config, db: config?.firestore ?? initFirestore(config) }
export function FirestoreAdapter({
emulator,
...firebaseOptions
}: FirebaseOptions & FirestoreAdapterOptions): Adapter {
const firebaseApp = getFirebase(firebaseOptions)
const db = getFirestore(firebaseApp)
const preferSnakeCase = namingStrategy === "snake_case"
const C = collestionsFactory(db, preferSnakeCase)
const mapper = mapFieldsFactory(preferSnakeCase)
if (emulator) {
connectFirestoreEmulator(
db,
emulator?.host ?? "localhost",
emulator?.port ?? 3001
)
}
const Users = collection(db, "users").withConverter(
getConverter<AdapterUser & IndexableObject>()
)
const Sessions = collection(db, "sessions").withConverter(
getConverter<AdapterSession & IndexableObject>()
)
const Accounts = collection(db, "accounts").withConverter(
getConverter<AdapterAccount>()
)
const VerificationTokens = collection(db, "verificationTokens").withConverter(
getConverter<VerificationToken & IndexableObject>({ excludeId: true })
)
return {
async createUser(userInit) {
const { id: userId } = await C.users.add(userInit as AdapterUser)
async createUser(newUser) {
const userRef = await addDoc(Users, newUser)
const userSnapshot = await getDoc(userRef)
const user = await getDoc(C.users.doc(userId))
if (!user) throw new Error("[createUser] Failed to fetch created user")
if (userSnapshot.exists() && Users.converter) {
return Users.converter.fromFirestore(userSnapshot)
}
return user
throw new Error("[createUser] Failed to create user")
},
async getUser(id) {
return await getDoc(C.users.doc(id))
},
const userSnapshot = await getDoc(doc(Users, id))
if (userSnapshot.exists() && Users.converter) {
return Users.converter.fromFirestore(userSnapshot)
}
return null
},
async getUserByEmail(email) {
return await getOneDoc(C.users.where("email", "==", email))
const userQuery = query(Users, where("email", "==", email), limit(1))
const userSnapshots = await getDocs(userQuery)
const userSnapshot = userSnapshots.docs[0]
if (userSnapshot?.exists() && Users.converter) {
return Users.converter.fromFirestore(userSnapshot)
}
return null
},
async getUserByAccount({ provider, providerAccountId }) {
const account = await getOneDoc(
C.accounts
.where("provider", "==", provider)
.where(mapper.toDb("providerAccountId"), "==", providerAccountId)
const accountQuery = query(
Accounts,
where("provider", "==", provider),
where("providerAccountId", "==", providerAccountId),
limit(1)
)
if (!account) return null
const accountSnapshots = await getDocs(accountQuery)
const accountSnapshot = accountSnapshots.docs[0]
return await getDoc(C.users.doc(account.userId))
if (accountSnapshot?.exists()) {
const { userId } = accountSnapshot.data()
const userDoc = await getDoc(doc(Users, userId))
if (userDoc.exists() && Users.converter) {
return Users.converter.fromFirestore(userDoc)
}
}
return null
},
async updateUser(partialUser) {
if (!partialUser.id) throw new Error("[updateUser] Missing id")
const userRef = doc(Users, partialUser.id)
const userRef = C.users.doc(partialUser.id)
await setDoc(userRef, partialUser, { merge: true })
await userRef.set(partialUser, { merge: true })
const userSnapshot = await getDoc(userRef)
const user = await getDoc(userRef)
if (!user) throw new Error("[updateUser] Failed to fetch updated user")
if (userSnapshot.exists() && Users.converter) {
return Users.converter.fromFirestore(userSnapshot)
}
return user
throw new Error("[updateUser] Failed to update user")
},
async deleteUser(userId) {
await db.runTransaction(async (transaction) => {
const accounts = await C.accounts
.where(mapper.toDb("userId"), "==", userId)
.get()
const sessions = await C.sessions
.where(mapper.toDb("userId"), "==", userId)
.get()
const userRef = doc(Users, userId)
const accountsQuery = query(Accounts, where("userId", "==", userId))
const sessionsQuery = query(Sessions, where("userId", "==", userId))
transaction.delete(C.users.doc(userId))
// TODO: May be better to use events instead of transactions?
await runTransaction(db, async (transaction) => {
const accounts = await getDocs(accountsQuery)
const sessions = await getDocs(sessionsQuery)
transaction.delete(userRef)
accounts.forEach((account) => transaction.delete(account.ref))
sessions.forEach((session) => transaction.delete(session.ref))
})
},
async linkAccount(accountInit) {
const ref = await C.accounts.add(accountInit)
const account = await ref.get().then((doc) => doc.data())
return account ?? null
async linkAccount(account) {
const accountRef = await addDoc(Accounts, account)
const accountSnapshot = await getDoc(accountRef)
if (accountSnapshot.exists() && Accounts.converter) {
return Accounts.converter.fromFirestore(accountSnapshot)
}
},
async unlinkAccount({ provider, providerAccountId }) {
await deleteDocs(
C.accounts
.where("provider", "==", provider)
.where(mapper.toDb("providerAccountId"), "==", providerAccountId)
.limit(1)
const accountQuery = query(
Accounts,
where("provider", "==", provider),
where("providerAccountId", "==", providerAccountId),
limit(1)
)
const accountSnapshots = await getDocs(accountQuery)
const accountSnapshot = accountSnapshots.docs[0]
if (accountSnapshot?.exists()) {
await deleteDoc(accountSnapshot.ref)
}
},
async createSession(sessionInit) {
const ref = await C.sessions.add(sessionInit)
const session = await ref.get().then((doc) => doc.data())
async createSession(session) {
const sessionRef = await addDoc(Sessions, session)
const sessionSnapshot = await getDoc(sessionRef)
if (session) return session ?? null
if (sessionSnapshot.exists() && Sessions.converter) {
return Sessions.converter.fromFirestore(sessionSnapshot)
}
throw new Error("[createSession] Failed to fetch created session")
throw new Error("[createSession] Failed to create session")
},
async getSessionAndUser(sessionToken) {
const session = await getOneDoc(
C.sessions.where(mapper.toDb("sessionToken"), "==", sessionToken)
const sessionQuery = query(
Sessions,
where("sessionToken", "==", sessionToken),
limit(1)
)
if (!session) return null
const sessionSnapshots = await getDocs(sessionQuery)
const sessionSnapshot = sessionSnapshots.docs[0]
const user = await getDoc(C.users.doc(session.userId))
if (!user) return null
if (sessionSnapshot?.exists() && Sessions.converter) {
const session = Sessions.converter.fromFirestore(sessionSnapshot)
const userDoc = await getDoc(doc(Users, session.userId))
return { session, user }
if (userDoc.exists() && Users.converter) {
const user = Users.converter.fromFirestore(userDoc)
return { session, user }
}
}
return null
},
async updateSession(partialSession) {
const sessionId = await db.runTransaction(async (transaction) => {
const sessionSnapshot = (
await transaction.get(
C.sessions
.where(
mapper.toDb("sessionToken"),
"==",
partialSession.sessionToken
)
.limit(1)
)
).docs[0]
if (!sessionSnapshot?.exists) return null
const sessionQuery = query(
Sessions,
where("sessionToken", "==", partialSession.sessionToken),
limit(1)
)
const sessionSnapshots = await getDocs(sessionQuery)
const sessionSnapshot = sessionSnapshots.docs[0]
transaction.set(sessionSnapshot.ref, partialSession, { merge: true })
if (sessionSnapshot?.exists()) {
await setDoc(sessionSnapshot.ref, partialSession, { merge: true })
return sessionSnapshot.id
})
const sessionDoc = await getDoc(sessionSnapshot.ref)
if (!sessionId) return null
if (sessionDoc?.exists() && Sessions.converter) {
const session = Sessions.converter.fromFirestore(sessionDoc)
const session = await getDoc(C.sessions.doc(sessionId))
if (session) return session
throw new Error("[updateSession] Failed to fetch updated session")
return session
}
}
return null
},
async deleteSession(sessionToken) {
await deleteDocs(
C.sessions
.where(mapper.toDb("sessionToken"), "==", sessionToken)
.limit(1)
const sessionQuery = query(
Sessions,
where("sessionToken", "==", sessionToken),
limit(1)
)
const sessionSnapshots = await getDocs(sessionQuery)
const sessionSnapshot = sessionSnapshots.docs[0]
if (sessionSnapshot?.exists()) {
await deleteDoc(sessionSnapshot.ref)
}
},
async createVerificationToken(verificationToken) {
await C.verification_tokens.add(verificationToken)
return verificationToken
const verificationTokenRef = await addDoc(
VerificationTokens,
verificationToken
)
const verificationTokenSnapshot = await getDoc(verificationTokenRef)
if (verificationTokenSnapshot.exists() && VerificationTokens.converter) {
const { id, ...verificationToken } =
VerificationTokens.converter.fromFirestore(verificationTokenSnapshot)
return verificationToken
}
},
async useVerificationToken({ identifier, token }) {
const verificationTokenSnapshot = (
await C.verification_tokens
.where("identifier", "==", identifier)
.where("token", "==", token)
.limit(1)
.get()
).docs[0]
const verificationTokensQuery = query(
VerificationTokens,
where("identifier", "==", identifier),
where("token", "==", token),
limit(1)
)
const verificationTokenSnapshots = await getDocs(verificationTokensQuery)
const verificationTokenSnapshot = verificationTokenSnapshots.docs[0]
if (!verificationTokenSnapshot) return null
if (verificationTokenSnapshot?.exists() && VerificationTokens.converter) {
await deleteDoc(verificationTokenSnapshot.ref)
const data = verificationTokenSnapshot.data()
await verificationTokenSnapshot.ref.delete()
return data
const { id, ...verificationToken } =
VerificationTokens.converter.fromFirestore(verificationTokenSnapshot)
return verificationToken
}
return null
},
}
}

View File

@@ -1,168 +0,0 @@
import { AppOptions, getApps, initializeApp } from "firebase-admin/app"
import {
getFirestore,
initializeFirestore,
Timestamp,
} from "firebase-admin/firestore"
import type {
AdapterUser,
AdapterAccount,
AdapterSession,
VerificationToken,
} from "next-auth/adapters"
import { FirebaseAdapterConfig } from "."
// for consistency, store all fields as snake_case in the database
const MAP_TO_FIRESTORE: Record<string, string | undefined> = {
userId: "user_id",
sessionToken: "session_token",
providerAccountId: "provider_account_id",
emailVerified: "email_verified",
}
const MAP_FROM_FIRESTORE: Record<string, string | undefined> = {}
for (const key in MAP_TO_FIRESTORE) {
MAP_FROM_FIRESTORE[MAP_TO_FIRESTORE[key]!] = key
}
const identity = <T>(x: T) => x
/** @internal */
export function mapFieldsFactory(preferSnakeCase?: boolean) {
if (preferSnakeCase) {
return {
toDb: (field: string) => MAP_TO_FIRESTORE[field] ?? field,
fromDb: (field: string) => MAP_FROM_FIRESTORE[field] ?? field,
}
}
return { toDb: identity, fromDb: identity }
}
/** @internal */
export function getConverter<Document extends Record<string, any>>(options: {
excludeId?: boolean
preferSnakeCase?: boolean
}): FirebaseFirestore.FirestoreDataConverter<Document> {
const mapper = mapFieldsFactory(options?.preferSnakeCase ?? false)
return {
toFirestore(object) {
const document: Record<string, unknown> = {}
for (const key in object) {
if (key === "id") continue
const value = object[key]
if (value !== undefined) {
document[mapper.toDb(key)] = value
} else {
console.warn(`FirebaseAdapter: value for key "${key}" is undefined`)
}
}
return document
},
fromFirestore(
snapshot: FirebaseFirestore.QueryDocumentSnapshot<Document>
): Document {
const document = snapshot.data()! // we can guarentee it exists
const object: Record<string, unknown> = {}
if (!options?.excludeId) {
object.id = snapshot.id
}
for (const key in document) {
let value: any = document[key]
if (value instanceof Timestamp) value = value.toDate()
object[mapper.fromDb(key)] = value
}
return object as Document
},
}
}
/** @internal */
export async function getOneDoc<T>(
querySnapshot: FirebaseFirestore.Query<T>
): Promise<T | null> {
const querySnap = await querySnapshot.limit(1).get()
return querySnap.docs[0]?.data() ?? null
}
/** @internal */
export async function deleteDocs<T>(
querySnapshot: FirebaseFirestore.Query<T>
): Promise<void> {
const querySnap = await querySnapshot.get()
for (const doc of querySnap.docs) {
await doc.ref.delete()
}
}
/** @internal */
export async function getDoc<T>(
docRef: FirebaseFirestore.DocumentReference<T>
): Promise<T | null> {
const docSnap = await docRef.get()
return docSnap.data() ?? null
}
/** @internal */
export function collestionsFactory(
db: FirebaseFirestore.Firestore,
preferSnakeCase = false
) {
return {
users: db
.collection("users")
.withConverter(getConverter<AdapterUser>({ preferSnakeCase })),
sessions: db
.collection("sessions")
.withConverter(getConverter<AdapterSession>({ preferSnakeCase })),
accounts: db
.collection("accounts")
.withConverter(getConverter<AdapterAccount>({ preferSnakeCase })),
verification_tokens: db
.collection(
preferSnakeCase ? "verification_tokens" : "verificationTokens"
)
.withConverter(
getConverter<VerificationToken>({ preferSnakeCase, excludeId: true })
),
}
}
/**
* 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
* ```ts title="lib/firestore.ts"
* 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,
* })
* })
* ```
*/
export function initFirestore(
options: AppOptions & { name?: FirebaseAdapterConfig["name"] } = {}
) {
const apps = getApps()
const app = options.name ? apps.find((a) => a.name === options.name) : apps[0]
if (app) return getFirestore(app)
return initializeFirestore(initializeApp(options, options.name))
}

View File

@@ -1,57 +1,118 @@
import { runBasicTests } from "@next-auth/adapter-test"
import { FirestoreAdapter } from "../src"
import { FirestoreAdapter, type FirebaseAdapterConfig } from "../src"
import {
collestionsFactory,
initFirestore,
getFirestore,
connectFirestoreEmulator,
terminate,
collection,
query,
where,
limit,
getDocs,
getDoc,
getOneDoc,
mapFieldsFactory,
} from "../src/utils"
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"
describe.each([
{ namingStrategy: "snake_case" },
{ namingStrategy: "default" },
] as Partial<FirebaseAdapterConfig>[])(
"FirebaseAdapter with config: %s",
(config) => {
config.name = `next-auth-test-${config.namingStrategy}`
config.projectId = "next-auth-test"
config.databaseURL = "http://localhost:8080"
const app = initializeApp({ projectId: "next-auth-test" })
const firestore = getFirestore(app)
const db = initFirestore(config)
const preferSnakeCase = config.namingStrategy === "snake_case"
const mapper = mapFieldsFactory(preferSnakeCase)
const C = collestionsFactory(db, preferSnakeCase)
connectFirestoreEmulator(firestore, "localhost", 8080)
for (const [name, collection] of Object.entries(C)) {
test(`collection "${name}" should be empty`, async () => {
expect((await collection.count().get()).data().count).toBe(0)
})
}
type IndexableObject = Record<string, unknown>
runBasicTests({
adapter: FirestoreAdapter(config),
db: {
disconnect: async () => await db.terminate(),
session: (sessionToken) =>
getOneDoc(
C.sessions.where(mapper.toDb("sessionToken"), "==", sessionToken)
),
user: (userId) => getDoc(C.users.doc(userId)),
account: ({ provider, providerAccountId }) =>
getOneDoc(
C.accounts
.where("provider", "==", provider)
.where(mapper.toDb("providerAccountId"), "==", providerAccountId)
),
verificationToken: ({ identifier, token }) =>
getOneDoc(
C.verification_tokens
.where("identifier", "==", identifier)
.where("token", "==", token)
),
},
})
}
const Users = collection(firestore, "users").withConverter(
getConverter<AdapterUser & IndexableObject>()
)
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)
},
async session(sessionToken) {
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)
return session
}
return null
},
async user(id) {
const snapshot = await getDoc(doc(Users, id))
if (snapshot?.exists() && Users.converter) {
const user = Users.converter.fromFirestore(snapshot)
return user
}
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]
if (snapshot?.exists() && Accounts.converter) {
const account = Accounts.converter.fromFirestore(snapshot)
return account
}
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]
if (snapshot?.exists() && VerificationTokens.converter) {
const verificationToken =
VerificationTokens.converter.fromFirestore(snapshot)
return verificationToken
}
},
},
})

View File

@@ -1,11 +0,0 @@
import config from "@next-auth/adapter-test/jest/jest-preset.js"
//TODO: update rest of the packages to Jest 29+
const {testURL, ...rest} = config
export default {
...rest,
testEnvironmentOptions: {
url: testURL
},
rootDir: ".."
}

View File

@@ -1,23 +1,11 @@
{
"extends": "@next-auth/tsconfig/tsconfig.adapters.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"strict": true,
"noUncheckedIndexedAccess": true,
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "node",
"outDir": ".",
"rootDir": "src",
"skipDefaultLibCheck": true,
"strictNullChecks": true,
"stripInternal": true,
"declarationMap": true,
"declaration": true
"moduleResolution": "node"
},
"include": [
"src/**/*"
],
"exclude": [
"tests"
]
}
"exclude": ["tests", "dist", "jest.config.js"]
}

View File

@@ -1,8 +1,4 @@
/**
*
* :::warning Experimental
* `@auth/core` is under active development.
* :::
*
* This is the main entry point to the Auth.js library.
*

2575
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff