Next-Auth EmailProvider and FaunaAdapter "Unable to Sign In"

974 Views Asked by At

I've followed the next-auth docs to hook up its EmailProvider and FaunaAdapter in a brand new Next.js project, but I am unable to sign in.

My dependencies:

"@next-auth/fauna-adapter": "^1.0.2"
"faunadb": "^4.5.2"
"next": "^12.1.0"
"next-auth": "^4.2.1"
"nodemailer": "^6.7.2"
"react": "^17.0.2"
"react-dom": "^17.0.2"

I've wrapped my pages/_app.jsx in SessionProvider, so I can use useSession() in pages/index.jsx.

The real work happens in the only other file in the repo, pages/api/auth/[...nextauth].js:

import NextAuth from 'next-auth';
import EmailProvider from 'next-auth/providers/email'
import { Client as FaunaClient } from 'faunadb';
import { FaunaAdapter } from '@next-auth/fauna-adapter';


const client = new FaunaClient({
  secret: process.env.FAUNA_CLIENT_SECRET,
  scheme: 'https',
  domain: 'db.fauna.com',
  port: 443,
});

export default NextAuth({
  providers: [
    EmailProvider({
      server: {
        host: process.env.EMAIL_SERVER_HOST,
        port: process.env.EMAIL_SERVER_PORT,
        auth: {
          user: process.env.EMAIL_SERVER_USER,
          pass: process.env.EMAIL_SERVER_PASSWORD
        }
      },
      from: process.env.EMAIL_FROM,
      maxAge: 10 * 60 * 60,
    }),
  ],
  adapter: FaunaAdapter(client),
  secret: process.env.AUTH_SECRET,
});

The Problem

When I attempt to sign in, I successfully receive the email in my inbox and the Fauna dashboard shows a new entry in the verification_tokens collection. The expires property in the new token is ten hours in the future and the identifier is the correct email address.

However, when I click the link in the email, I'm redirected to http://localhost:3000/api/auth/error?error=Verification and I see the message "Unable to sign in. The sign in link is no longer valid. It may have been used already or it may have expired."

In the Fauna dashboard, both users and sessions collections remain empty.

I'm using Postmark to handle the SMTP email – I'm curious whether it may pre-click the link when it sends the email?

Gmail is the receiving email client, but I doubt it's pre-clicking the link?

My local node version is 16.14.0, and to eliminate complications I've reverted from yarn to npm. I have a demo repository here: https://github.com/tatwater/nextauth-fauna

Thanks for the help!

1

There are 1 best solutions below

0
On

So, I had a better look and I found out that the adapter is calling the fauna Match(Index('verification_token_by_identifier_and_token'), [identifier, token]) in this specific order while the Index is expecting to receive the terms the other way around [token, identifier].

Looks like when the index is created through the Fauna Shell it always sets the terms in the "wrong" way. A temporary solution is to delete the index and recreate it manually setting the identifier as the first term and token as the second.

fauna admin with Index

A more permanent solution would probably be update the adapter so that it uses the index in the same way it is created from the Shell.