Skip to content

Instantly share code, notes, and snippets.

@Rashmi-278
Last active February 19, 2021 09:32
Show Gist options
  • Select an option

  • Save Rashmi-278/633fb62ee0c70277bc69b033eeb55409 to your computer and use it in GitHub Desktop.

Select an option

Save Rashmi-278/633fb62ee0c70277bc69b033eeb55409 to your computer and use it in GitHub Desktop.

Next-Auth with faunaDB adapter

1. Clone the repository and install dependancies

git clone https://github.com/nextauthjs/next-auth-example.git
cd next-auth-example
npm i

2. Configure your local environment

Copy the .env.local.example file in this directory to .env.local (which will be ignored by Git):

cp .env.local.example .env.local`

Add details for one or more providers (e.g. Google, Twitter, GitHub, Email, etc). When configuring your database you should also install an appropriate node_module.

FaunaDB

npm i faunadb

Add Ennironment variable FAUNA_SECRET_KEY and assign your server key Create the following Collections and Indexes in FaunaDB

Collections:

  1. users
  2. accounts
  3. sessions
  4. verificationRequests

Indexes:

  1. index_users_id
  2. index_users_email
  3. index_accounts_providerId_providerAccountId
  4. index_verificationRequests_token
  5. index_sessions_id
  6. index_sessions_sessionToken

Make sure, NEXTAUTH_DATABASE_URL = https://graphql.fauna.com/graphql

Add the adapter code in your next-auth node_module by creating a folder called faunadb-adapter

node_modules/next-auth/dist/adapters/faunadb-adapter/index.js

from this gist https://gist.github.com/s-kris/fbb9e5d7ba5e9bb3a5f7bd11f3c42b96

Configure your adapter

node_modules/next-auth/dist/adapters/index.js

"use strict";
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _typeorm = _interopRequireDefault(require("./typeorm"));
var _prisma = _interopRequireDefault(require("./prisma"));
var _faunadb = _interopRequireDefault(require("./faunadb-adapter"))
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _default = {
  Default: _faunadb.default.Adapter,
  TypeORM: _typeorm.default,
  Prisma: _prisma.default,
  FaunaDB: _faunadb.default
};
exports.default = _default;

3. Setup [...nextauth].js

import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
import faunadb from "faunadb"
import Adapters from 'next-auth/adapters'
const faunaClient = new faunadb.Client({
  secret: process.env.FAUNADB_SECRET_KEY,
});
const options = {
  // @link https://next-auth.js.org/configuration/providers
  providers: [
    Providers.Email({
      // SMTP connection string or nodemailer configuration object https://nodemailer.com/
      server: process.env.NEXTAUTH_EMAIL_SERVER,
      // Email services often only allow sending email from a valid/verified address
      from: process.env.NEXTAUTH_EMAIL_FROM,
    }),
    // When configuring oAuth providers make sure you enabling requesting
    // permission to get the users email address (required to sign in)
    Providers.Google({
      clientId: process.env.NEXTAUTH_GOOGLE_ID,
      clientSecret: process.env.NEXTAUTH_GOOGLE_SECRET,
    }),
    Providers.Facebook({
      clientId: process.env.NEXTAUTH_FACEBOOK_ID,
      clientSecret: process.env.NEXTAUTH_FACEBOOK_SECRET,
    }),
  ],
  adapter: Adapters.Default({faunaClient}),
  // @link https://next-auth.js.org/configuration/databases
  database: process.env.NEXTAUTH_DATABASE_URL,
}
const Auth = (req, res) => NextAuth(req, res, options)
export default Auth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment