首页 > 解决方案 > 使用选定主机域的角度 gmail 登录

问题描述

我在我的 Angular 应用程序中使用 angular-6-social-login 进行谷歌登录。它工作正常。但我想在此指定托管域。所以只有两个指定的域用户能够登录它。目前此模块不支持此功能。有没有办法覆盖它。

现有代码:

`GoogleLoginProvider.prototype.initialize = function () {
    var _this = this;
    return new Promise(function (resolve, reject) {
        _this.loadScript(_this.loginProviderObj, function () {
            gapi.load('auth2', function () {
                _this.auth2 = gapi.auth2.init({
                    client_id: _this.clientId,
                    scope: 'email'
                });
                _this.auth2.then(function () {
                    if (_this.auth2.isSignedIn.get()) {
                        resolve(_this.drawUser());
                    }
                });
            });
        });
    });
   };`

我的要求:

    `GoogleLoginProvider.prototype.initialize = function () {
     var _this = this;
     return new Promise(function (resolve, reject) {
        _this.loadScript(_this.loginProviderObj, function () {
            gapi.load('auth2', function () {
                _this.auth2 = gapi.auth2.init({
                    client_id: _this.clientId,
                    scope: 'email',
                    hosted_domain:'abc.com'
                });
                _this.auth2.then(function () {
                    if (_this.auth2.isSignedIn.get()) {
                        resolve(_this.drawUser());
                    }
                });
            });
        });
    });
   };`

标签: angular6google-signin

解决方案


Angular 6 社交登录包不支持自定义配置来指定托管域。相反,您可以更改 node_modules 中的 angular-6-social-login.umd.js 文件。

更改 GoogleLoginProvider.prototype.initialize 函数并在 gapi.auth2.init 函数中添加以下详细信息
1. 范围应为“电子邮件” 2. fetch_basic_profile: false 3. hosts_domain: 您的域。

gapi.load('auth2', function () {
                _this.auth2 = gapi.auth2.init({
                    client_id: _this.clientId,
                    scope: 'email',
                    fetch_basic_profile: false,
                    hosted_domain: <your-domain> ,
                });
                _this.auth2.then(function () {
                    if (_this.auth2.isSignedIn.get()) {
                        resolve(_this.drawUser());
                    }
                });
            });

这将适用于指定的域。

第二个选项您可以使用angularx-social-login为您提供在配置选项中配置托管域。


推荐阅读