angularjs - 身份验证后不会调用卫星器

标签 angularjs node.js authentication satellizer

我对 Angular 非常陌生,所以我的知识是基于教程的,即使这样我也没有成功。 我需要使用谷歌帐户进行身份验证。这有效,我得到了一个可以授权我的 api 调用的 token 。但是登录后,弹出窗口应该消失,我应该被重定向到主页。这不起作用。

这是我的 Controller

angular.module('MyApp').controller('loginController', ['$scope', '$auth', '$location','loginService', loginController]);

function loginController($scope, $auth, $location, loginService) {
    $scope.authenticate = function(provider) {
        $auth.authenticate(provider).then(function(data) {
            loginService.saveToken(data.data.token);
            console.log('You have successfully signed in with ' + provider + '!');
            $location.path('http://localhost/#/home');
        });
    };
};

在 app.js 中我有我的配置。这不是我的工作,而是一个和我一样是实习生的 friend ,他负责一个移动应用程序,他使用相同的功能来获取他的 token ,并且它有效。

authProvider.google({
            clientId: CLIENT_ID,
            redirectUri: 'http://localhost:3000/api/users/signIn'
        });
        $authProvider.storage = 'localStorage'; // or 'sessionStorage'
        $authProvider.loginRedirect = 'http://localhost/#/home';

这是 Node 中的 Controller ,URL 重定向到该 Controller (Google 开发者控制台)

router.get('/signIn', function(req, res) {
    //console.log(req);
    var code = req.query.code;
    oauth2Client.getToken(code, function(err, tokens) {
        if (!err) {
            https.get("https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=" + tokens.access_token, function(response) {
                // Continuously update stream with data
                var body = '';
                response.setEncoding('utf8');
                response.on('data', function(d) {
                    body += d;
                });

                // Data fetched
                response.on('end', function() {
                    var parsed = JSON.parse(body);
                    // Check if client_id is from the right app
                    if (parsed.issued_to == '343234242055-vd082vo0o8r8lmfvp1a973736fd98dht.apps.googleusercontent.com') {
                        User.getGoogleId(parsed.user_id, function(err, user) {
                            if (err) {
                                res.status(500).send({
                                    message: 'not authorized app'
                                });
                            }

                            // No user returned, create one
                            if (!user) {

                                // Request user info
                                oauth2Client.setCredentials(tokens);
                                plus.people.get({
                                    userId: 'me',
                                    auth: oauth2Client
                                }, function(err, plusUser) {
                                    if (err) res.status(500).send({
                                        message: 'not authorized app'
                                    });
                                    else {

                                        // Create new user
                                        User.create(plusUser.name.givenName, plusUser.name.familyName, (plusUser.name.givenName + "." + plusUser.name.familyName + "@cozmos.be").toLowerCase(), parsed.user_id, function(err, newUser) {
                                            if (err) res.status(500).send({
                                                message: 'not authorized app'
                                            });
                                            else {
                                                res.statusCode = 200;
                                                return res.send({
                                                    response: 'Success',
                                                    id: user._id,
                                                    firstName: user.firstName,
                                                    lastName: user.lastName,
                                                    email: user.email,
                                                    token: tokens.access_token
                                                });
                                            }
                                        });
                                    }
                                });
                            } else {
                                // Return user
                                res.statusCode = 200;
                                return res.send({
                                    response: 'Success',
                                    id: user._id,
                                    firstName: user.firstName,
                                    lastName: user.lastName,
                                    email: user.email,
                                    token: tokens.access_token
                                });
                            }
                        });
                    }

                    // if not right app, return unauthorized response
                    else {
                        res.status(500).send({
                            message: 'not authorized app'
                        });
                    }
                });
            });
        }
    });

});

因此,我登录后,系统要求我授予应用程序使用我的帐户信息的权限,我收到一个 json 响应,在其中我可以看到我的姓名、电子邮件和 token ,仅此而已

最佳答案

即使在我工作的公司内,也没有人能找到答案。所以我自己提出了一个解决方案。我不再使用卫星了。

 .when('/access_token=:access_token', {
            template: '',
            controller: function($window, $http, $location, $rootScope) {
                var hash = $location.path().substr(1);

                var splitted = hash.split('&');
                var params = {};

                for (var i = 0; i < splitted.length; i++) {
                    var param = splitted[i].split('=');
                    var key = param[0];
                    var value = param[1];
                    params[key] = value;
                    $rootScope.accesstoken = params;
                }
                console.log(params.access_token);
                var json = {
                    Token: params.access_token
                };
                $window.localStorage['token'] = params.access_token;
                $http.post('http://localhost:3000/api/users/signIn', json).success(function(data, status) {
                    console.log(data);
                }).error(function(err) {
                    console.log(err);
                });

                $location.path("/home");
            }

            /*controller: 'createNewsFeed',
          templateUrl: 'homepage.html'*/
        }).

因此自行重定向页面。因为身份验证在后端进行,所以我可以获得访问 token ,这是我将来使用其余 api 时真正需要的唯一东西。我定义了一条路线,在收到带有 token 的 json 后,我的浏览器将手动重定向到 $window.location。因此,当加载该页面时(用户不可见,速度太快而无法注意到),我分析 token ,保存 token ,分析身份验证,成功后我手动重定向到主页。

关于angularjs - 身份验证后不会调用卫星器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36694488/

相关文章:

javascript - 从 JSON 对象数组返回特定​​对象

angularjs - 对 Angularjs 的嵌入和隔离范围和绑定(bind)感到困惑

c# - User.Identity.IsAuthenticated 在 PasswordSignInAsync() 成功后为 false

javascript - 服务返回异常数据

javascript - 为了使我的代码正常工作,我必须更改 Cheerio 的一些核心功能,如何将其保留在我的模块中?

node.js - 带有 Typescript 的 NodeJS 的 REST 客户端

node.js - 使用 CoffeeScript 从 node.js 上的 Redis 中提取对象数组的更好方法

authentication - 允许使用电子邮件地址登录,同时允许多个用户使用相同的电子邮件地址

linux - Fedora 19 根凭证是什么?

javascript - 在 Angular 中添加 CSS 类