Skip to content

Instantly share code, notes, and snippets.

@neomadara
Forked from jdnichollsc/app.js
Created December 28, 2016 02:48
Show Gist options
  • Save neomadara/1679682dab75e9db2168d4038569d687 to your computer and use it in GitHub Desktop.
Save neomadara/1679682dab75e9db2168d4038569d687 to your computer and use it in GitHub Desktop.
Ionic Google OAuth Authentication, Firebase 3 and ngCordovaOauth plugin
angular.module('App', ['ionic', 'ngCordova', 'ngAnimate', 'ngCordovaOauth', 'firebase'])
.run(['$ionicPlatform',
'$rootScope',
'$firebaseAuth',
function($ionicPlatform, $rootScope, $firebaseAuth) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
$firebaseAuth().$onAuthStateChanged(function(user) {
$rootScope.user = user;
});
}])
//Google scopes - https://developers.google.com/identity/protocols/googlescopes
//myConfig - Angular constants
(function(firebase) {
'use strict';
angular
.module('App')
.factory('Auth', Auth);
Auth.$inject = ['$cordovaOauth', 'myConfig'];
function Auth($cordovaOauth, myConfig) {
return {
login : function () {
if(ionic.Platform.isWebView()){
return $cordovaOauth.google(myConfig.googleClientId + '&include_profile=true', ["email", "profile"]).then(function (result) {
var credential = firebase.auth.GoogleAuthProvider.credential(result.id_token);
return firebase.auth().signInWithCredential(credential);
});
}
else{
var provider = new firebase.auth.GoogleAuthProvider();
provider.addScope('email');
provider.addScope('profile');
return firebase.auth().signInWithPopup(provider);
}
}
};
}
})(firebase);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment