Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(adapters): move to firebase-admin in Firebase Adapter #6225

Merged
merged 27 commits into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
70f0343
add firebase-admin-adapter
wyattades Dec 30, 2022
6a7bd5d
fix type typo
wyattades Dec 30, 2022
b32d4f2
assert collections are empty before test
wyattades Dec 30, 2022
4d11596
Merge branch 'main' into firebase-admin-adapter
balazsorban44 Jan 17, 2023
c3400b4
Merge branch 'main' into firebase-admin-adapter
balazsorban44 Jan 17, 2023
e4d5591
cleanup
balazsorban44 Jan 17, 2023
900c51c
autogenerate docs from source
balazsorban44 Jan 17, 2023
498acdb
Merge branch 'main' into firebase-admin-adapter
balazsorban44 Jan 19, 2023
dfc8545
docs fixes
balazsorban44 Jan 19, 2023
b9d9888
use single-page structure for modules
balazsorban44 Jan 19, 2023
22cdfba
fix links
balazsorban44 Jan 19, 2023
0f370ff
remove `remark-github`
balazsorban44 Jan 19, 2023
f96be2b
pass config instead of instance, publish as ESM
balazsorban44 Jan 19, 2023
e695212
do not override `projectId` by default
balazsorban44 Jan 19, 2023
5a77198
update readme/docs
balazsorban44 Jan 19, 2023
ff0d74c
update jest
balazsorban44 Jan 20, 2023
e289670
fix tests
balazsorban44 Jan 20, 2023
9272802
update README
balazsorban44 Jan 20, 2023
f551453
support passing an instance
balazsorban44 Jan 20, 2023
b57ae5f
add dev command
balazsorban44 Jan 20, 2023
c4f2f34
Merge branch 'main' into firebase-admin-adapter
balazsorban44 Jan 20, 2023
bd336aa
upgrade typedoc plugins
balazsorban44 Jan 30, 2023
c3c951b
upgrade typedoc plugin
balazsorban44 Jan 30, 2023
b3caf1c
change from main to index
balazsorban44 Jan 30, 2023
6309949
update typedoc plugin
balazsorban44 Jan 31, 2023
1cd5cbb
Merge branch 'main' into firebase-admin-adapter
balazsorban44 Feb 4, 2023
7bb0689
fix `files` for publishing
balazsorban44 Feb 5, 2023
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
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/3_bug_adapter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ body:
- "@next-auth/dynamodb-adapter"
- "@next-auth/fauna-adapter"
- "@next-auth/firebase-adapter"
- "@next-auth/firebase-admin-adapter"
- "@next-auth/mikro-orm-adapter"
- "@next-auth/mongodb-adapter"
- "@next-auth/neo4j-adapter"
Expand Down
3 changes: 3 additions & 0 deletions .github/issue-labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ fauna:
firebase:
- "@next-auth/firebase-adapter"

firebase-admin:
- "@next-auth/firebase-admin-adapter"

mikro-orm:
- "@next-auth/mikro-orm-adapter"

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
firebase-debug.log
ui-debug.log
.pnpm-debug.log


Expand Down
57 changes: 57 additions & 0 deletions docs/docs/reference/06-adapters/firebase-admin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
id: firebase-admin
title: Firebase Admin
---

This is the Firebase Admin 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-admin-adapter firebase-admin
```

2. Create a Firebase project and generate a service account key. See [instructions](https://firebase.google.com/docs/admin/setup).

3. 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 { FirestoreAdminAdapter } from "@next-auth/firebase-admin-adapter"

import admin from "firebase-admin"

// Initialize the firebase admin app. By default, the firebase admin sdk will
// look for the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// that to authenticate with the firebase project. See other authentication
// methods here: https://firebase.google.com/docs/admin/setup
const app = admin.initializeApp()

const firestore = app.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: FirestoreAdminAdapter(firestore),
...
})
```

## Naming Conventions

If mixed snake_case and camelCase field names in the database is an issue for you, you can pass the option `preferSnakeCase: true` to the adapter. This will convert all
fields names and collection names to snake_case e.g. the collection `verificationTokens` will instead be `verification_tokens`, and fields like `emailVerified` will instead be `email_verified`.

```javascript
FirestoreAdminAdapter(firestore, { preferSnakeCase: true })
```
1 change: 0 additions & 1 deletion packages/adapter-firebase/.firebaserc

This file was deleted.

16 changes: 0 additions & 16 deletions packages/adapter-firebase/CHANGELOG.md

This file was deleted.

64 changes: 27 additions & 37 deletions packages/adapter-firebase/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,71 +15,61 @@

## Overview

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.
This is the Firebase Admin 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.

You can find more Firebase information in the docs at [authjs.dev/reference/adapters/firebase](https://authjs.dev/reference/adapters/firebase).
You can find more Firebase information in the docs at [authjs.dev/reference/adapters/firebase-admin](https://authjs.dev/reference/adapters/firebase-admin).

## Getting Started

1. Install `next-auth` and `@next-auth/firebase-adapter`.
1. Install the necessary packages

```js
npm install next-auth @next-auth/firebase-adapter
```bash npm2yarn
npm install next-auth @next-auth/firebase-admin-adapter firebase-admin
```

2. Add this adapter to your `pages/api/[...nextauth].js` next-auth configuration object.
2. Create a Firebase project and generate a service account key. See [instructions](https://firebase.google.com/docs/admin/setup).

```js
3. 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 Providers from "next-auth/providers"
import { FirestoreAdapter } from "@next-auth/firebase-adapter"
import GoogleProvider from "next-auth/providers/google"
import { FirestoreAdminAdapter } from "@next-auth/firebase-admin-adapter"

import admin from "firebase-admin"

import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore"
// Initialize the firebase admin app. By default, the firebase admin sdk will
// look for the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// that to authenticate with the firebase project. See other authentication
// methods here: https://firebase.google.com/docs/admin/setup
const app = admin.initializeApp()

const app = initializeApp({ projectId: "next-auth-test" });
const firestore = getFirestore(app);
const firestore = app.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/oauth-builtin
// https://authjs.dev/reference/providers/
providers: [
Providers.Google({
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
],
adapter: FirestoreAdapter(firestore),
adapter: FirestoreAdminAdapter(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).
## Naming Conventions

An example firebase config looks like this:
If mixed snake_case and camelCase field names in the database is an issue for you, you can pass the option `preferSnakeCase: true` to the adapter. This will convert all
fields names and collection names to snake_case e.g. the collection `verificationTokens` will instead be `verification_tokens`, and fields like `emailVerified` will instead be `email_verified`.

```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",
}
```javascript
FirestoreAdminAdapter(firestore, { preferSnakeCase: true })
```

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

We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/.github/blob/main/CONTRIBUTING.md).
Expand Down
5 changes: 4 additions & 1 deletion packages/adapter-firebase/firebase.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"emulators": {
"firestore": {
"rules": "firestore.rules"
},
"emulator": {
"firestore": {
"port": 8080
}
Expand Down
10 changes: 10 additions & 0 deletions packages/adapter-firebase/firestore.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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;
}
}
}
2 changes: 1 addition & 1 deletion packages/adapter-firebase/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 5 additions & 4 deletions packages/adapter-firebase/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@
"keywords": [
"next-auth",
"next.js",
"firebase"
"firebase",
"firebase-admin"
],
"private": false,
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "tsc",
"test": "FIRESTORE_EMULATOR_HOST=localhost:8080 firebase --token '$FIREBASE_TOKEN' emulators:exec --only firestore --project next-auth-test jest"
"test": "firebase emulators:exec --only firestore --project next-auth-test jest"
},
"peerDependencies": {
"firebase": "^9.7.0",
"firebase-admin": "^11.4.1",
"next-auth": "^4"
},
"devDependencies": {
"@next-auth/adapter-test": "workspace:*",
"@next-auth/tsconfig": "workspace:*",
"firebase": "^9.14.0",
"firebase-admin": "^11.4.1",
"firebase-tools": "^11.16.1",
"jest": "^27.4.3",
"next-auth": "workspace:*"
Expand Down
58 changes: 0 additions & 58 deletions packages/adapter-firebase/src/converter.ts

This file was deleted.

Loading