Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tokens from Google Sign-In instead of calling getTokens. #8604

Merged
merged 2 commits into from
Apr 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions packages/google-oauth/google_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ var hasOwn = Object.prototype.hasOwnProperty;
Google.whitelistedFields = ['id', 'email', 'verified_email', 'name', 'given_name',
'family_name', 'picture', 'locale', 'timezone', 'gender'];

function getServiceData(query) {
var response = getTokens(query);
var expiresAt = (+new Date) + (1000 * parseInt(response.expiresIn, 10));
var accessToken = response.accessToken;
var idToken = response.idToken;
function getServiceDataFromTokens(tokens) {
var accessToken = tokens.accessToken;
var idToken = tokens.idToken;
var scopes = getScopes(accessToken);
var identity = getIdentity(accessToken);

var serviceData = {
accessToken: accessToken,
idToken: idToken,
expiresAt: expiresAt,
scope: scopes
};

if (hasOwn.call(tokens, "expiresAt")) {
serviceData.expiresAt =
Date.now() + 1000 * parseInt(tokens.expiresIn, 10);
}

var fields = Object.create(null);
Google.whitelistedFields.forEach(function (name) {
if (hasOwn.call(identity, name)) {
Expand All @@ -33,12 +34,17 @@ function getServiceData(query) {
// only set the token in serviceData if it's there. this ensures
// that we don't lose old ones (since we only get this on the first
// log in attempt)
if (response.refreshToken)
serviceData.refreshToken = response.refreshToken;
if (tokens.refreshToken) {
serviceData.refreshToken = tokens.refreshToken;
}

return {
serviceData: serviceData,
options: {profile: {name: identity.name}}
options: {
profile: {
name: identity.name
}
}
};
}

Expand All @@ -47,18 +53,26 @@ Accounts.registerLoginHandler(function (request) {
return;
}

const { serviceData } = getServiceDataFromTokens({
accessToken: request.accessToken,
refreshToken: request.refreshToken,
idToken: request.idToken,
});

return Accounts.updateOrCreateUserFromExternalService("google", {
id: request.userId,
idToken: request.idToken,
accessToken: request.accessToken,
email: request.email,
picture: request.imageUrl,
...getServiceData({
code: request.serverAuthCode
}).serviceData,
...serviceData,
});
});

function getServiceData(query) {
return getServiceDataFromTokens(getTokens(query));
}

OAuth.registerService('google', 2, null, getServiceData);

// returns an object containing:
Expand Down
2 changes: 1 addition & 1 deletion packages/google-oauth/package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
summary: "Google OAuth flow",
version: "1.2.2"
version: "1.2.3"
});

var cordovaPluginGooglePlusURL =
Expand Down