Skip to content

Instantly share code, notes, and snippets.

@jackrobertscott
Last active September 28, 2019 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackrobertscott/226b2676048115c7ff4be92b9daa85ae to your computer and use it in GitHub Desktop.
Save jackrobertscott/226b2676048115c7ff4be92b9daa85ae to your computer and use it in GitHub Desktop.
/**
* Helper function to get the library with access to the google plus api.
*/
function getGooglePlusApi(auth) {
return google.plus({ version: 'v1', auth });
}
/**
* Extract the email and id of the google account from the "code" parameter.
*/
function getGoogleAccountFromCode(code) {
// get the auth "tokens" from the request
const data = await auth.getToken(code);
const tokens = data.tokens;
// add the tokens to the google api so we have access to the account
const auth = createConnection();
auth.setCredentials(tokens);
// connect to google plus - need this to get the user's email
const plus = getGooglePlusApi(auth);
const me = await plus.people.get({ userId: 'me' });
// get the google id and email
const userGoogleId = me.data.id;
const userGoogleEmail = me.data.emails && me.data.emails.length && me.data.emails[0].value;
// return so we can login or sign up the user
return {
id: userGoogleId,
email: userGoogleEmail,
tokens: tokens, // you can save these to the user if you ever want to get their details without making them log in again
};
}
@stevanpopo
Copy link

Hey Jack - When this code executes i get the error "ReferenceError: auth is not defined" - where is auth meant to have been defined?

@Dziejo93
Copy link

I'm getting same error as stevan and can't get to find what is going wrong

@chapmanjacobd
Copy link

await is a reserved word

@619frank
Copy link

Hey Jack - When this code executes i get the error "ReferenceError: auth is not defined" - where is auth meant to have been defined?

const auth = createConnection();
put this before line number 14

@gitlobato
Copy link

Hi I am getting error on the below line
const data = await auth.getToken(code);

await can not be used... how to fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment