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.

Custom fields for Credentials Provider

See original GitHub issue

Describe the feature

I would like to suggest a custom/optional field in the user interface object to store additional data. F.e when logging in with Strapi using Credential flow, the server will return a JWT. This token needs to stay unaltered otherwise the secret is no longer valid. I would like to propose a extra optional field for users, so that they can add in content that can be then safely stored within the encoded session jwt. By using getToken in Nitro we could then reacquire the original raw JWT token from Strapi.

Additional information

Within the […].ts auth catch all endpoint:

`if (user) {

      const u = {
        id: user.id,
        name: user.user.username,
        // Passing the OG JWT through the email field.
        email: user.jwt
      };

      return u;
    } 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
    }`

And then in Nitro Api I hack it out the email field currently with getToken: `const token = await getToken({ event });

const settings = {
  method: "GET",
  headers: {
    "Content-Type": "application/json",
    Authorization: "Bearer " + token.email,
  },
};`

No response

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
BracketJohncommented, Dec 8, 2022

Hey @jorgv, Hey @darwebdb,

this is a question that has been answered by @JoaoPedroAS51 here: https://github.com/sidebase/nuxt-auth/issues/61#issuecomment-1330946022

It will also be part of our documentation once #90 is merged: https://sidebase.io/nuxt-auth/recipes/custom-session-data

I’m closing this issue for now, please open a new one if there’s any further need for clarification (:

Thanks to both of you (:

0reactions
jorgvcommented, Nov 18, 2022

Thanks for your reply @BracketJohn

I am using a similar situation like @darwebdb. We have a remote backend, so we use the credentials provider. We authenticate the user and we get a token and the user information as response:

  data: {
    token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYX',
    user: {
      id: '2',
      username: 'myuser',
      firstName: 'John',
      lastName: 'Doe',
      email: 'email@aaa.com'
     otherThings: {...}
      }
  }      

In the authorize function from the credentials provider, I can pass only name, email and image. So like @darwebdb I am passing the backend token to the image field in order to access it after with the useSession and getSession

Data: {
  "user": {
    "name": "John",
    "email": "email@aaa.com",
    "image": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYX"
  },
  "expires": "2022-12-18T08:34:11.586Z"
}

So, my question is how can I store the rest of the response fields in the session, can I do it during the authorize function? or how can I pass it to a callback so I can store it.

Or is it better to extend the user interface with the fields that I need, if this is the best way how do I replace the typeinterface for the custom one, as the link you provided from AuthNext I already have the custom type/next-auth.ts file but I am not sure how to replace it in the module.

thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Credentials provider with a custom backend in ...
Using Credentials provider with a custom backend in NextAuth.js! ... Shown below the input field in my example setEmail(router.query.email) ...
Read more >
Custom Login Experiences: Credential Providers in Windows ...
Create Custom Login Experiences With Credential Providers For Windows ... Credential::GetStringValue (dwFieldID = 2; password field) 21.
Read more >
Use Custom Fields to Run Provider Searches - Salesforce Help
To create a custom field for use in provider search: From Object Manager, find and select Care Provider Searchable Field. Create a text...
Read more >
Credentials | NextAuth.js
The Credentials provider allows you to handle signing in with arbitrary credentials, such as a username and password, domain, or two factor ...
Read more >
Custom credential provider for *additional* authentication ...
In my case the only accessible for user field is "password type" edit for entering PIN-code. As a result my CP serialize user's...
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