Created
February 28, 2019 09:23
-
-
Save kohendrix/79cd4aa6d80a73a0ca8ba1bb941085fc to your computer and use it in GitHub Desktop.
Google sign-in OAuth2 sample
This file contains hidden or 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
| 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