【问题标题】:how to get email of authenticated user in angularjs using satellizer如何使用 satellizer 在 angularjs 中获取经过身份验证的用户的电子邮件
【发布时间】:2016-02-04 15:05:44
【问题描述】:

我正在 angularJs 中创建一个 webapp,并使用satellizer 进行身份验证。我可以使用 google 进行身份验证,但我无法弄清楚如何在我的 UI 代码中的某处获取经过身份验证的用户的电子邮件 ID。我也找不到任何功能来获取documentation 中的电子邮件 ID。

【问题讨论】:

  • 你应该指出你想得到的范围。例如:$authProvider.google({ url: '/auth/google', scope: ['profile', 'email']});
  • 因此,您可以从服务$auth.login(user) .then(function(response) { // Could be your user returned from the server. })得到响应
  • 我认为您有该响应,因为您没有更改服务器端。我建议你应该从 auth/google 返回用户。或者您可以立即调用用户/我。它提供当前用户。 github.com/sahat/satellizer/blob/master/examples/server/node/…
  • 在这里查看我的答案stackoverflow.com/questions/34740186/…

标签: angularjs authentication satellizer


【解决方案1】:

我所要做的就是,使用保存在浏览器本地内存中的 access_token satelizer 访问 googleapis,它会返回用户信息,以下是供您参考的服务:

angular.module('MyApp')
    .factory('Google', ['$resource', '$auth', Google]);

function Google($resource, $auth) {
    return $resource("https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=" + $auth.getToken(), {}, {})
}

您只需执行以下操作即可获取登录用户的电子邮件 ID:

var googleResponse = Google.get({}, function () {
    var email = googleResponse.email;
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 2016-01-27
    • 1970-01-01
    相关资源
    最近更新 更多