Search code examples
node.jsauth0alexa-skills-kit

Node JS & Auth0 | Get profile


Hey developer friends,

I'm building a small alexa skill & use auth0 as the authentication system. Now I want to get the userinfo/profile of the user, because I need the userId. In the request from alexa is an an accessToken. With that token, I want to be able to get the information from auth0.

var AuthenticationClient = require('auth0').AuthenticationClient;

var auth0 = new AuthenticationClient({
  domain: '[MY NAME].eu.auth0.com',
  clientId: '[MY CLIENT ID]',
  clientSecret: '[MY CLIENT SECRET]'
});

Then in the actual function:

const access_token = session.user.accessToken;

console.log("ACCESSTOKEN:", access_token)

auth0.getProfile(access_token, function (err, userInfo) {
    if(err) {
        console.log("failed to retrieve profile", err)
    } else {
        const userId = JSON.parse(userInfo)['sub'];
        console.log(userId);
    }
}

When I run the code, I get the error 401 Unauthorized from auth0, although I use the provided accessToken. The accessToken is something like this in the amazon request: "VDMj7VBJ0EaJ1XZhvVRUfPgYNtxviro"

Any suggestions on how to do that properly?


Solution

  • I initalized the auth module twice, fixed it & now it works fine!