简体   繁体   English

在AngularJS应用中使用Google用户登录

[英]Sign-in with google user in an AngularJS app

I read this tutorial in order to connect my AngularJS app with google sign-in. 我阅读本教程是为了将我的AngularJS应用程序与谷歌登录连接起来。 I added the google button as follows (just copy-pasting the tutorial): 我添加了google按钮,如下所示(只需复制粘贴教程):

In the head I added the meta tag: 在头部我添加了元标记:

<meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">

And then added the button itself: 然后添加按钮本身:

<div class="g-signin2" data-onsuccess="onSignIn"></div>

At first I just copied the onSignIn method (that's just a generic handler so I'm not copying it to the question) from the tutorial and put it in a <script>...</script> tag and it worked. 起初我只是从onSignIn复制了onSignIn方法(这只是一个通用处理程序,所以我没有将它复制到问题中)并将其放在<script>...</script>标记中并且它有效。 I now want to put this method in an Angular controller. 我现在想把这个方法放在一个Angular控制器中。 So I created a controller as follows: 所以我创建了一个控制器如下:

app.controller('GoogleCtrl', function() {
  function onSignIn(googleUser) {
    var profile = googleUser.getBasicProfile();
    console.log('ID: ' + profile.getId());
    console.log('Name: ' + profile.getName());
    console.log('Image URL: ' + profile.getImageUrl());
    console.log('Email: ' + profile.getEmail());
   }
}

And wrapped the button with a div: 并用div包裹按钮:

<div ng-controller="GoogleCtrl">
    <div class="g-signin2" data-onsuccess="onSignIn"></div>
</div>

My code doesn't get to the onSignIn method now and I'm trying to figure out what can I do. 我的代码现在没有进入onSignIn方法,我正在试图弄清楚我能做些什么。

If you follow the instructions, what you will end up with is window. onSignIn 如果您按照说明操作,最终会得到的是window. onSignIn window. onSignIn - try to run it in your browser JS console, now to have the same behaviour you will need to crate that function from your controller. window. onSignIn - 尝试在浏览器JS控制台中运行它,现在具有从控制器创建该功能所需的相同行为。

app.controller('GoogleCtrl', function() {
  function onSignIn(googleUser) {
    var profile = googleUser.getBasicProfile();
    console.log('ID: ' + profile.getId());
    console.log('Name: ' + profile.getName());
    console.log('Image URL: ' + profile.getImageUrl());
    console.log('Email: ' + profile.getEmail());
   }
  window.onSignIn = onSignIn;
}

Remember that code executed by 3rd party like onSignIn will need to call $scope.$digest , so angular is aware of model changes. 请记住,第三方执行的代码(如onSignIn需要调用$scope.$digest ,因此angular可以onSignIn模型更改。

Looking at your code, it seems like you might have to add listener for your function. 查看您的代码,您似乎可能需要为您的函数添加侦听器。 To keep things simple, you can easily integrate google login in your app with this plugin. 为了简单起见,您可以轻松地将Android应用中的google登录与此插件集成。 https://github.com/sahat/satellizer https://github.com/sahat/satellizer

You didn't specify your version of AngularJS, but it shouldn't matter. 您没有指定AngularJS的版本,但它无关紧要。 I solved this for AngularJS 1.x as follows: 我为AngularJS 1.x解决了这个问题,如下所示:

 allControllers.controller('authenticateController', ['$rootScope', $scope', function($rootScope, $scope) { // do whatever you're doing on this page... // initialize The GoogleAuth API explicitly // The load an init below can be placed in your app.js in you feel like you want to implement the whole lifecycle Google provides. gapi.load('auth2', function() { // Loads the auth2 component of gapi gapi.auth2.init({ // initialize the auth2 using your credentials client_id: $GOOGLE_API_CLIENT_ID }).then(function onInit() { // on complete of init gapi.signin2.render("g-signin2", { // render the HTML button on the screen providing the ID of the element (g-signin2) onsuccess: function(googleUser) { // This executes when a user successfully authorizes you to their data by clicking the button and selecting their account. var profile = googleUser.getBasicProfile(); console.log('ID: ' + profile.getId()); console.log('Name: ' + profile.getName()); console.log('Image URL: ' + profile.getImageUrl()); console.log('Email: ' + profile.getEmail()); // Do whatever you need to do to authenticate on your site. } }); }); }); }]); 
 // In your index.html <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.8/angular.min.js"></script> <script type="text/javascript" src="https://apis.google.com/js/platform.js" async defer></script> // In your login fragment <div id="g-signin2" style="width: 200px; margin: 20px auto 20px auto;"></div> 

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

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