Last active
May 8, 2017 18:31
-
-
Save jdnichollsc/17bae6581d405321937fe433410172c9 to your computer and use it in GitHub Desktop.
Ionic Google OAuth Authentication, Firebase 3 and (ngCordovaOauth plugin or cordova-plugin-googleplus)
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
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; | |
}); | |
}]) |
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
//https://github.com/EddyVerbruggen/cordova-plugin-googleplus | |
(function(firebase) { | |
'use strict'; | |
angular | |
.module('App') | |
.factory('Auth', Auth); | |
Auth.$inject = ['$q', 'myConfig']; | |
function Auth($q, myConfig) { | |
var _nativeLogin = function(){ | |
return $q(function(resolve, reject) { | |
window.plugins.googleplus.login( | |
{ | |
'scopes': 'email profile', | |
'webClientId': myConfig.googleClientId | |
'offline': true, | |
}, | |
function (obj) { | |
resolve(obj) | |
}, | |
function (msg) { | |
reject(msg); | |
} | |
); | |
}); | |
} | |
return { | |
login : function () { | |
if(ionic.Platform.isWebView()){ | |
return _nativeLogin().then(function (result) { | |
var credential = firebase.auth.GoogleAuthProvider.credential(result.idToken); | |
return firebase.auth().signInWithCredential(credential); | |
}); | |
} | |
else{ | |
var provider = new firebase.auth.GoogleAuthProvider(); | |
provider.addScope('email'); | |
provider.addScope('profile'); | |
return firebase.auth().signInWithPopup(provider); | |
} | |
} | |
}; | |
} | |
})(firebase); |
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
//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); |
Where were you 6 months ago!, haha great plugin thank you for your work!.
@alxanthony haha you're welcome!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't know about Anonymous login. Guys, my example was updated using the native way with cordova-plugin-googleplus