question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not working with Credentials provider

See original GitHub issue

Environment

Nuxi 3.0.0-rc.11 Nuxt 3.0.0-rc.11 with Nitro 0.5.4 @sidebase/nuxt-auth 0.0.1-beta.3

Reproduction

No response

Describe the bug

Thank you for this awesome module, it is the missing piece for the Nuxt universe.

I have try to setup nuxt-auth with the credentials Provider which is the most common type when using a decoupled backend.

this is my auth config in nuxt.config.ts copied from official docs in NextAuth

NUXT_URL = http://localhost/api/auth/

  auth: {
    nextAuth: {
      url: process.env.NUXT_URL,
      options: {
        secret: process.env.NUXT_SECRET,
        providers: [
          CredentialsProvider({
            // The name to display on the sign in form (e.g. 'Sign in with...')
            name: 'Credentials',
            // The credentials is used to generate a suitable form on the sign in page.
            // You can specify whatever fields you are expecting to be submitted.
            // e.g. domain, username, password, 2FA token, etc.
            // You can pass any HTML attribute to the <input> tag through the object.
            credentials: {
              username: { label: 'Username', type: 'text', placeholder: 'jsmith' },
              password: { label: 'Password', type: 'password' },
            },
            async authorize(credentials, req) {
              // You need to provide your own logic here that takes the credentials
              // submitted and returns either a object representing a user or value
              // that is false/null if the credentials are invalid.
              // e.g. return { id: 1, name: 'J Smith', email: 'jsmith@example.com' }
              // You can also use the `req` object to obtain additional parameters
              // (i.e., the request IP address)
              // eslint-disable-next-line no-console
              console.log(credentials)
              const user = { id: '1', name: 'J Smith', email: 'jsmith@example.com' }

              if (user) {
                // Any object returned will be saved in `user` property of the JWT
                return user
              }
              else {
                // If you return null then an error will be displayed advising the user to check their details.
                return null

                // You can also Reject this callback with an Error thus the user will be sent to the error page with the error message as a query parameter
              }
            },
          }),
        ],
      },
    },

Login form its showing but not matter what is the input for username and password it always show the error: “Sign in failed. Check the details you provided are correct.”

the authorize function its never reach when submitting the form.

Is there anything else I am missing in order to make it work

Additional context

No response

Logs

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
BracketJohncommented, Nov 7, 2022

@jorgv checkout 0.0.1-beta.4 -> credentials flow should work. Please give feedback here (:

@zoey-kaiser please update the nuxt auth demo!

Note to both: You will habe to change some stuff, as the way to setup nuxt-auth changed -> please refer to the quick start (and give feedbaxk where applicable (: )

1reaction
jorgvcommented, Nov 7, 2022

@BracketJohn works perfectly! Thanks so much. I will leave my […].ts in case someone else needs it for Credentials provider:

import CredentialsProvider from 'next-auth/providers/credentials'
import { NuxtAuthHandler } from '#auth'

export default NuxtAuthHandler({
  providers: [
    // @ts-expect-error Import is exported on .default during SSR, so we need to call it this way. May be fixed via Vite at some point
    CredentialsProvider.default({
      name: 'Credentials',

      credentials: {
        username: { label: 'Username', type: 'text', placeholder: 'jsmith' },
        password: { label: 'Password', type: 'password' },
      },
      async authorize(credentials, req) {
        
        // const user = { id: '1', name: 'J Smith', email: 'jsmith@example.com' }
        const token = Buffer.from(`${credentials.username}:${credentials.password}`).toString('base64')
        const res = await fetch('http://localhost/login', {
          method: 'post',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': `Basic ${token}`,
          },
        })
        const user = await res.json()
        // If no error and we have user data, return it
        if (res.ok && user) {
          return user
        }
        else {
          // If you return null then an error will be displayed advising the user to check their details.
          return null
          // You can also Reject this callback with an Error thus the user will be sent to the error page with the error message as a query parameter
        }
      },
    }),
  ],
})

Read more comments on GitHub >

github_iconTop Results From Across the Web

Credentials Provider Not Firing Session Callback · Issue #3970
Hello,. I'm having trouble with using Credentials Provider where it's not firing the session callback. I've logged the whole process from signin ...
Read more >
My Custom Credentials Provider not working intermittently on ...
My Custom Credentials Provider not working intermittently on Windows 10. I have developed the Credential Provider using Microsoft Sample ...
Read more >
Working with AWS Credentials - AWS SDK for Java 1.x
To make requests to Amazon Web Services, you must supply AWS credentials to the AWS SDK for Java. You can do this in...
Read more >
Troubleshoot the Central Credential Provider installation
Uninstall the Central Credential Provider and run the installation again. Option 2: Check whether the specified Credential Provider user has already been ......
Read more >
Custom Credential Providers Fails to Load on Windows 10 ...
Therefore, custom credential providers do not take effect. To work around this issue, you'll need to disable the automatic system logon of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found