4

I am try to implement google auth in angualjs. But I researched and get code in javascript.

Code:

<html lang="en">
    <head>
        <meta name="google-signin-scope" content="profile email">
        <meta name="google-signin-client_id" content="148699057185-ug1ge86g4dn4uffffekth3rb7382cl2333323238fau.apps.googleusercontent.com">
        <script src="https://apis.google.com/js/platform.js" async defer></script>
    </head>
    <body>
        <div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
        <script>

            function onSignIn(googleUser) {
                // Useful data for your client-side scripts:
                var profile = googleUser.getBasicProfile();
                console.log("ID: " + profile.getId());
                // Don't send this directly to your server!
                console.log("Name: " + profile.getName());
                console.log("Image URL: " + profile.getImageUrl());
                console.log("Email: " + profile.getEmail());
                // The ID token you need to pass to your backend:
                var id_token = googleUser.getAuthResponse().id_token;
                console.log("ID Token: " + id_token);

                alert(id_token);

                // All HTML5 Rocks properties support CORS.

            }
        </script>
    </body>
</html>

I am getting alert the id_token.

But i need to convert into angularjs.

I tried to implement to angularjs but its not working.

Please anyone help me.

Expect: I need to get only id_token(not access token) from google oauth in angularjs.

1 Answer 1

1

I think you should try angular-google-plus module. It is a complete angular module which handles the login with the Google+ API.

Here is a DEMO (Don't forget to insert your client ID in the code.)

Example usage:

var app = angular.module('app', ['googleplus']);

app.config(['GooglePlusProvider', function(GooglePlusProvider) {
     GooglePlusProvider.init({
        clientId: 'YOUR_CLIENT_ID',
        apiKey: 'YOUR_API_KEY'
     });
}]);

app.controller('AuthCtrl', ['$scope', 'GooglePlus', function ($scope, GooglePlus) {
    $scope.login = function () {
        GooglePlus.login().then(function (authResult) {
            console.log(authResult);

            GooglePlus.getUser().then(function (user) {
                console.log(user);
            });
        }, function (err) {
            console.log(err);
        });
    };
}]);

I think authResult object should contain what you want.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for late reply. I expect the authResult including id_token. but now its not getting id_token

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.