SANGEETH KUMAR S G
SANGEETH KUMAR S G

Reputation: 598

ngCordova: oauth with google fails

I am working on ng-Cordova to build a project. I tried so many examples to get logging with google. My need is to login to my app using google account. I have login button. When I click the button, it needs to redirect to google account and sign in with the google account and need to come back to my app. I have a google client id. I hereby post my index.html file and app.js file.

Index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
<script src="js/ng-cordova.min.js"></script>
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter">

    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">Ionic Blank Starter</h1>
      </ion-header-bar>
      <ion-content>
      </ion-content>
    </ion-pane>
  </body>
</html>

my app.js file

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
var exampleApp = angular.module('starter', ['ionic', 'ngCordova']);

//.run(function($ionicPlatform) {
 // $ionicPlatform.ready(function() {
    //// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    //// for form inputs)
//    if(window.cordova && window.cordova.plugins.Keyboard) {
//      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
//    }
//    if(window.StatusBar) {
//      StatusBar.styleDefault();
 //   }
var requestToken = "";
var accessToken = "";
var clientId = "";
var clientSecret = "";
 
var exampleApp = angular.module('example', ['ionic'])
    .config(function($stateProvider, $urlRouterProvider) {
        $stateProvider
            .state('login', {
                url: '/login',
                templateUrl: 'templates/login.html',
                controller: 'LoginController'
            })
            .state('secure', {
                url: '/secure',
                templateUrl: 'templates/secure.html',
                controller: 'SecureController'
            });
        $urlRouterProvider.otherwise('/login');
    });
 
 
exampleApp.controller('LoginController', function($scope, $http, $location) {
 
    $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
 
    $scope.login = function() {
        var ref = window.open('https://accounts.google.com/o/oauth2/auth?client_id=' + clientId + '&redirect_uri=http://localhost/callback&scope=https://www.googleapis.com/auth/urlshortener&approval_prompt=force&response_type=code&access_type=offline', '_blank', 'location=no');
        ref.addEventListener('loadstart', function(event) { 
            if((event.url).startsWith("http://localhost/callback")) {
                requestToken = (event.url).split("code=")[1];
                $http({method: "post", url: "https://accounts.google.com/o/oauth2/token", data: "client_id=" + clientId + "&client_secret=" + clientSecret + "&redirect_uri=http://localhost/callback" + "&grant_type=authorization_code" + "&code=" + requestToken })
                    .success(function(data) {
                        accessToken = data.access_token;
                        $location.path("/secure");
                    })
                    .error(function(data, status) {
                        alert("ERROR: " + data);
                    });
                ref.close();
            }
        });
    }
 
    if (typeof String.prototype.startsWith != 'function') {
        String.prototype.startsWith = function (str){
            return this.indexOf(str) == 0;
        };
    }
    
});
 
exampleApp.controller('SecureController', function($scope, $http) {
 
    $scope.accessToken = accessToken;
    
});

I created two html pages additionally after searching some examples.

login.html

<ion-view ng-controller="LoginController" title="Oauth Login">
    <ion-content>
        <div>
            <div class="padding">
                <button ng-click="login()" class="button button-block button-stable">
                    Sign In
                </button>
            </div>
        </div>
    </ion-content>
</ion-view>

and secure.html

<ion-view ng-controller="SecureController" title="Oauth Secure">
    <ion-content>
        <div>
            <div class="padding">
                {{accessToken}}
                <button class="button button-block button-stable">
                    Do Something
                </button>
            </div>
        </div>
    </ion-content>
</ion-view>

When I run the app, only Index file with blank page is showing. I don't know what is the problem. Can somebody help me?

Upvotes: 0

Views: 1214

Answers (1)

Anil kumar
Anil kumar

Reputation: 928

Hi in github i uploaded a sample project to login via google before you execute the project please follow the steps in README.md file of github

Upvotes: 1

Related Questions