Detecting new user signup in [...nextauth].js

Viewed 18

I'm building an app with NextAuth's email provider as the sole means of user registration and login.

In local development everything worked fine, but when deploying the app to Vercel I keep getting Unhandled Promise Rejection errors from Mongo, so I decided to switch to NextAuth's unstable_getServerSession and everything is running very smoothly now.

Before making the switch I used an extra parameter to communicate whether the user was just logging in or newly signing up. Based on this, I sent different emails (a welcome email with login link vs. a shorter message with just the login link).

Here is how the [...nextauth].js file looked before:

export default async function auth(req, res) {

    const signup = req.body.signup || false

    return await NextAuth(req, res, {
        adapter: MongoDBAdapter(clientPromise),
        ...
        providers: [
            EmailProvider({
            ...
          sendVerificationRequest(server) {
            customLogin(
                server.identifier,
                server.url,
                server.provider, // passing the above data for email content
                signup // custom parameter to decide which email to send
            )
          }
          })
        ],
        ...
    })
}

I adjusted the code sample from the documentation, but wasn't able to pass a custom parameter to it.

Here is my new code:

export const authOptions = {
    adapter: MongoDBAdapter(promise),
    ...
    providers: [
        EmailProvider({
        ...
      sendVerificationRequest(server) {
        customLogin(
            server.identifier,
            server.url,
            server.provider,
        )
      }
      })
    ],
    ...
}


export default NextAuth(authOptions)

The customLogin function doesn't do anything except construction the different email options.

Among the things I have tried are wrapping authOptions in a handler function with req and res, setting up a separate API route, and passing parameters to the NextAuth function, but none of those worked.

Any advise on how to implement a custom parameter here would be highly appreciated.

0 Answers
Related