git clone https://github.com/nextauthjs/next-auth-example.git
cd next-auth-example
npm i
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.
npm i faunadb
Add Ennironment variable FAUNA_SECRET_KEY and assign your server key Create the following Collections and Indexes in FaunaDB
Collections:
- users
- accounts
- sessions
- verificationRequests
Indexes:
- index_users_id
- index_users_email
- index_accounts_providerId_providerAccountId
- index_verificationRequests_token
- index_sessions_id
- 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;
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