Skip to content

Instantly share code, notes, and snippets.

@kohendrix
Created February 28, 2019 09:23
Show Gist options
  • Select an option

  • Save kohendrix/79cd4aa6d80a73a0ca8ba1bb941085fc to your computer and use it in GitHub Desktop.

Select an option

Save kohendrix/79cd4aa6d80a73a0ca8ba1bb941085fc to your computer and use it in GitHub Desktop.
Google sign-in OAuth2 sample
import { OAuth2Client } from 'google-auth-library';
const p = console.log;
/**
* this establishes the connection
* @return { OAuth2Client }
*/
function getOAuth2Client() {
return new OAuth2Client('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET', 'REDIRECT_URL');
}
/**
* fetch the fresh info with token
* @param { string } token
*/
async function verifyIdToken(token) {
try {
const client = getOAuth2Client();
const ticket = await client.verifyIdToken({
idToken: token,
audience: 'YOUR_CLIENT_ID'
});
const payload = ticket.getPayload();
p('payload', JSON.stringify(payload, null, 2));
const id = payload.sub;
return id;
} catch (e) {
p(e);
return null;
}
}
export { verifyIdToken };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment