Skip to content

Commit

Permalink
feat(adapters): move to firebase-admin in Firebase Adapter (#6225)
Browse files Browse the repository at this point in the history
Co-authored-by: Balázs Orbán <info@balazsorban.com>
fixes #5049
closes #6230
closes #5449
closes #5055
fixes #4927

BREAKING CHANGE:
The adapter now expects `firebase-admin` instead of the `firebase` package and also supports either passing a config object or a firestore instance.
  • Loading branch information
wyattades committed Feb 5, 2023
1 parent e9d8805 commit 6445eb6
Show file tree
Hide file tree
Showing 22 changed files with 1,825 additions and 1,788 deletions.
8 changes: 5 additions & 3 deletions .gitignore
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 Expand Up @@ -78,9 +79,6 @@ 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
Expand All @@ -96,3 +94,7 @@ 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
2 changes: 1 addition & 1 deletion docs/docs/guides/03-basics/refresh-token-rotation.md
Expand Up @@ -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/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.
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.

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.

Expand Down
75 changes: 0 additions & 75 deletions docs/docs/reference/06-adapters/firebase.md

This file was deleted.

15 changes: 15 additions & 0 deletions docs/docusaurus.config.js
Expand Up @@ -226,6 +226,21 @@ 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",
},
},
],
],
}

Expand Down
8 changes: 2 additions & 6 deletions docs/sidebars.js
Expand Up @@ -50,12 +50,8 @@ module.exports = {
label: "Database Adapters",
link: { type: "doc", id: "reference/adapters/overview" },
items: [
{
type: "autogenerated",
dirName: "reference/06-adapters",
// See: https://github.com/facebook/docusaurus/issues/5689
// exclude: ["index"],
},
{ type: "doc", id: "reference/adapter/firebase/index" },
{ type: "autogenerated", dirName: "reference/06-adapters" },
],
},
{
Expand Down
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.

70 changes: 5 additions & 65 deletions packages/adapter-firebase/README.md
@@ -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/adapters/main/packages/firebase/logo.svg" />
<h3 align="center"><b>Firebase Adapter</b> - NextAuth.js</h3>
<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>
<p align="center">
Open Source. Full Stack. Own Your Data.
</p>
Expand All @@ -13,72 +13,12 @@
</p>
</p>

## 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 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).

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

## 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).
Check out the [documentation](https://authjs.dev/reference/adapter/firebase) to learn how to use this adapter in your project.

## Contributing

Expand Down
5 changes: 4 additions & 1 deletion packages/adapter-firebase/firebase.json
@@ -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
@@ -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;
}
}
}
1 change: 0 additions & 1 deletion packages/adapter-firebase/jest.config.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/adapter-firebase/logo.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 17 additions & 8 deletions packages/adapter-firebase/package.json
Expand Up @@ -12,35 +12,44 @@
"Nico Domino <yo@ndo.dev>",
"Alex Meuer <github@alexmeuer.com>"
],
"main": "dist/index.js",
"type": "module",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./index.js"
}
},
"files": [
"dist",
"index.d.ts"
"src",
"*.js",
"*.d.ts*"
],
"license": "ISC",
"keywords": [
"next-auth",
"next.js",
"firebase"
"firebase",
"firebase-admin"
],
"private": false,
"publishConfig": {
"access": "public"
},
"scripts": {
"dev": "tsc -w",
"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 -c tests/jest.config.js'"
},
"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",
"jest": "^29.3.1",
"next-auth": "workspace:*"
}
}
58 changes: 0 additions & 58 deletions packages/adapter-firebase/src/converter.ts

This file was deleted.

0 comments on commit 6445eb6

Please sign in to comment.