简体   繁体   English

如何使用 auth0 和 node.js 在服务器中获取 user.email?

[英]How to get user.email in server using auth0 and node.js?

On my frontend, I can see the email through:在我的前端,我可以通过以下方式查看电子邮件:

  console.log(auth.profile.email)

Then I call this service to retrieve some information from the backend, it's protected so you need to be logged in to get anything back:然后我调用此服务从后端检索一些信息,它受到保护,因此您需要登录才能取回任何信息:

var metadata_req = {
        method: "GET",
        url: "http://localhost:80/protected/all/metadata"
    }

On the backend, I'm using node.js and this to verify that the user is logged in before doing a request:在后端,我使用 node.js 并在执行请求之前验证用户是否已登录:

var jwtCheck = express-jwt({
    secret: new Buffer(config.secret, 'base64'),
    audience: config.client_id
});

But if I print out req.user, I only see iss, sub, aud, exp and iat.但是如果我打印出req.user,我只能看到iss、sub、aud、exp 和iat。 I would like to see the email as well.我也想看看邮件。

You can get profile information in req.user by including the email permission when your user initially logs in.您可以通过在用户最初登录时包含电子邮件权限来获取req.user的配置文件信息。

For example, using lock in angular would look like this:例如,在 angular 中使用锁看起来像这样:

auth.signin({
    authParams: {
      scope: 'openid email'
    }
  }, function(profile, token) {
    // stuff
  }, function(err) {
    // error handling
  });

Change your API definition from "all/metadata" to "all/metadata/:emailId"将您的 API 定义从"all/metadata"更改为"all/metadata/:emailId"

Call your API调用您的 API

var metadata_req = {
        method: "GET",
        url: "http://localhost:80/protected/all/metadata/{your_email_id}"
    }

In NodeJS retrieve you email_id from req.params.emailId在 NodeJS 中,从req.params.emailId检索您的req.params.emailId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM