-
Description 🐜Using Auth0Provider with https://auth0.com/ set up to ask a username, I can not get the username/prefered_username attribute from the data coming from the response. I tried to get all data from the response (scope: 'openid profile email'), but the prefered username is not there.
Expected behavior: What happens: eg.: [email protected] --> username will be email Is this a bug in your own project?Yes How to reproduce ☕️import NextAuth from "next-auth";
import Auth0Provider from "next-auth/providers/auth0";
import { MongoDBAdapter } from "@next-auth/mongodb-adapter";
import clientPromise from "@lib/mongodb";
export default async function auth(req, res) {
return await NextAuth(req, res, {
providers: [
Auth0Provider({
clientId: process.env.AUTH0_CLIENT_ID,
clientSecret: process.env.AUTH0_CLIENT_SECRET,
issuer: process.env.AUTH0_ISSUER,
scope: "openid profile email",
profile(profile) {
console.log(profile);
return {
id: profile.sub,
name: profile.nickname,
email: profile.email,
image: profile.picture,
username: profile.preferred_username, //not working
};
},
}),
],
callbacks: {
async session({ session, user, token }) {
return session;
},
},
adapter: MongoDBAdapter({
db: (await clientPromise).db("user-data"),
}),
events: {
async session({ session }) {
//console.log(session);
},
async signIn({ user, profile }) {
console.log(user, profile);
},
},
debug: true,
});
}
Screenshots / Logs 📽
Environment 🖥System: Contributing 🙌🏽No, I am afraid I cannot help regarding this |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The OAuthProfile is the 1:1 response from Auth0, so if something is missing there, you probably need an extra scope. https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims Try adding the To forward it to your frontend, check out the jwt and session callbacks docs https://next-auth.js.org/configuration/callbacks |
Beta Was this translation helpful? Give feedback.
-
After countless hours of research, reading documents and trying things, I got an acceptable solution.At Auth0.com there is Added this rule: function addAttributes(user, context, callback) {
context.idToken['https://example.com/username'] = user.username;
callback(null, user, context);
} (If I only used In NextAuth I tried to override default behavior and log the profile hoping to see something new there. providers: [
Auth0Provider({
clientId: process.env.AUTH0_CLIENT_ID,
clientSecret: process.env.AUTH0_CLIENT_SECRET,
issuer: process.env.AUTH0_ISSUER,
profile(profile) {
console.log("profile is:");
console.log(profile);
return {
id: profile.sub,
name: profile["https://example.com/username"],
email: profile.email,
image: profile.picture,
};
},
}),
], https://next-auth.js.org/configuration/providers#options 'https://example.com/username' : 'username' So as @balazsorban44 suggested I added the property at the callbacks: {
async jwt({ token, profile }) {
if (profile) {
token.name = profile["https://example.com/username"];
}
return token;
},
}, If someone can suggest another solution, I'd be glad to read it. |
Beta Was this translation helpful? Give feedback.
After countless hours of research, reading documents and trying things,
I got an acceptable solution.
At Auth0.com there is
Auth Pipeline
->Rules
.Added this rule:
(If I only used
username
attribute it did not work).https://auth0.com/docs/configure/apis/scopes/sample-use-cases-scopes-and-claims#add-custom-claims-to-a-token
https://auth0.com/docs/security/tokens/json-web-tokens/create-namespaced-custom-claims
In NextAuth I tried to override default behavior and log the profile hoping to see something new there.