#How to use nextAuth for just a password authentication (no username needed)

14 messages · Page 1 of 1 (latest)

finite brook
#

Make a custom provider

#

Credentials({
            authorize({ email }: any) {
              return {
                email,
              };
            },
            credentials: {
              email: { label: `Email`, type: `text` },
            },
            name: `Test Mode`,
          } as any)
#

This an example from one of codebases

#

It automatically signs in a user given any email

#

You can take a similar approach for a password only provider

shut valley
#

I tried using this, but since it is for any email, it is also any password (which I don't want).

This is what my code is currently (with the username):

import NextAuth from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials";


const isCorrectCredentials = credentials =>
  credentials.username === process.env.NEXTAUTH_USERNAME &&
  credentials.password === process.env.NEXTAUTH_PASSWORD

const options = {

  providers: [

    
    CredentialsProvider({
      name: "Credentials",

      credentials: {
        username: { label: "Username", type: "text", placeholder: "jsmith" },
        password: { label: "Password", type: "password" },
      },

      authorize: async credentials => {
        if (isCorrectCredentials(credentials)) {
          const user = { id: 1, name: "Admin" }
          return Promise.resolve(user)
        } else {
          return Promise.resolve(null)
        }
      },
    }),

  ],
  pages: {
    signIn: '/signin',
  },
}

export default (req, res) => NextAuth(req, res, options)
#

And I also tried it without the username:

import NextAuth from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials";


const isCorrectCredentials = credentials =>
  credentials.password === process.env.NEXTAUTH_PASSWORD

const options = {
  providers: [
    CredentialsProvider({
      name: "Credentials",

      credentials: {
        password: { label: "Password", type: "password" },
      },

      authorize: async credentials => {
        if (isCorrectCredentials(credentials)) {
          const user = { id: 1, name: "Admin" }
          return Promise.resolve(user)
        } else {
          // If you return null or false then the credentials will be rejected
          return Promise.resolve(null)
        }
      },
    }),

  ],
  pages: {
    signIn: '/signin',
  },
}

export default (req, res) => NextAuth(req, res, options)
#

However without the username, it doesn't authenticate, and I am not sure why

#

In case if this matters, the form has these:

<form method="post" action="/api/auth/callback/credentials">
            <label>
              <input name="csrfToken" type="hidden" defaultValue={csrfToken} />
              Username
              <input name="username" type="text" placeholder="{random}" />
            </label>
            <label>
              Password
              <input name="password" type="password" placeholder="password" />
            </label>
            <button type="submit">Sign in</button>
          </form>

and similarly without the username part:

<form method="post" action="/api/auth/callback/credentials">
            <label>
              Password
              <input name="password" type="password" placeholder="password" />
            </label>
            <button type="submit">Sign in</button>
          </form>
shut valley
#

@finite brook sorry, wanted to check if you have advice on this

finite brook
#

This is too much code for me to read/debug at the moment I’m afraid

#

Good luck solving this.

shut valley
# finite brook This is too much code for me to read/debug at the moment I’m afraid

Oh okay! Makes sense! Thank you for the advice though!

If anyone else comes by this post, honestly the only code is this:

import NextAuth from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials";

const isCorrectCredentials = credentials =>
  credentials.username === process.env.NEXTAUTH_USERNAME &&
  credentials.password === process.env.NEXTAUTH_PASSWORD

const options = {
  providers: [
    CredentialsProvider({
      name: "Credentials",

      credentials: {
        username: { label: "Username", type: "text", placeholder: "jsmith" },
        password: { label: "Password", type: "password" },
      },

      authorize: async credentials => {
        if (isCorrectCredentials(credentials)) {
          const user = { id: 1, name: "Admin" }
          return Promise.resolve(user)
        } else {
          return Promise.resolve(null)
        }
      },
    }),

  ],
  pages: {
    signIn: '/signin',
  },
}

export default (req, res) => NextAuth(req, res, options)

(The other parts were just the variation without the username part, and had comments in it)