Created
September 28, 2015 21:31
-
-
Save Swimburger/226649881336a499dc02 to your computer and use it in GitHub Desktop.
Manually log in Google Auth in Angularjs controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en" ng-app="app"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Document</title> | |
</head> | |
<body> | |
<div ng-view></div> | |
<script src="https://apis.google.com/js/platform.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.min.js"></script> | |
<script src="https://code.angularjs.org/1.4.6/angular-route.min.js"></script> | |
<script> | |
angular.module('app',['ngRoute']) | |
.config(['$routeProvider',function($routeProvider){ | |
$routeProvider | |
.when('/log-in', { | |
template: '<button ng-click="logInCtrl.onLogInButtonClick()">Log In</button>', | |
controller: 'LogInController', | |
controllerAs: 'logInCtrl' | |
}).otherwise({ | |
redirectTo:'/log-in' | |
}); | |
}]) | |
.controller('LogInController',function(){ | |
var self = this; //to be able to reference to it in a callback, you could use $scope instead | |
gapi.load('auth2', function() {//load in the auth2 api's, without it gapi.auth2 will be undefined | |
gapi.auth2.init( | |
{ | |
client_id: 'CLIENT_ID.apps.googleusercontent.com' | |
} | |
); | |
var GoogleAuth = gapi.auth2.getAuthInstance();//get's a GoogleAuth instance with your client-id, needs to be called after gapi.auth2.init | |
self.onLogInButtonClick=function(){//add a function to the controller so ng-click can bind to it | |
GoogleAuth.signIn().then(function(response){//request to sign in | |
console.log(response); | |
}); | |
}; | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment