Last active
January 3, 2019 08:43
-
-
Save syeduzairshah/37cf00a4f45f33cd5f2f6ae8b25da424 to your computer and use it in GitHub Desktop.
Inject this module in your application and you can use Google Factory for Google authentication, accessing access token and user profile
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
(function(){ | |
angular.module('googleSsoApp', []).factory("Google",['$http', '$q', function ($http, $q) { | |
var currentWindowUrl = location.protocol + '//' + location.host + location.pathname; | |
var accessType = 'offline'; | |
var state = encodeURIComponent(currentWindowUrl); | |
var responseType = 'code'; | |
var scope = 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email' | |
return{ | |
authenticate: function(clientId, redirectUri){ | |
var googleBaseUrl = | |
"https://accounts.google.com/o/oauth2/v2/auth?" + | |
"access_type="+ accessType + | |
"&state="+ state + | |
"&client_id=" + clientId + | |
"&redirect_uri=" + redirectUri + | |
"&response_type=" + responseType + | |
"&scope=" + scope + | |
"&include_granted_scopes=true"; | |
window.location = googleBaseUrl; | |
}, | |
authToken: function(clientId, clientSecret, code){ | |
var defer = $q.defer(); | |
console.log(clientId) | |
$http.post( | |
'https://www.googleapis.com/oauth2/v4/token', | |
{ | |
code: code, | |
client_id: clientId, | |
client_secret: clientSecret, | |
redirect_uri: redirectUri, | |
grant_type: 'authorization_code' | |
} | |
).success(function(data, status, headers, config){ | |
defer.resolve(data.access_token); | |
}).error(function(data, status, headers, config){ | |
defer.reject(data); | |
}); | |
return defer.promise; | |
}, | |
fetchProfile: function(authCode){ | |
var defer = $q.defer(); | |
$http({ | |
method: "GET", | |
url:"https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token="+authCode | |
}).success(function(data, status, headers, config){ | |
defer.resolve(data); | |
}).error(function(data, status, headers, config){ | |
defer.reject(data); | |
}); | |
return defer.promise; | |
} | |
} | |
}]); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment