Created
November 20, 2024 03:54
-
-
Save whchi/a02de985046c4958fac0e82d975ac613 to your computer and use it in GitHub Desktop.
google-auth
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
| export const googleLoginActions = { | |
| getAuthUrl: () => { | |
| const query = { | |
| client_id: oauth_google.client_id, | |
| redirect_uri: oauth_google.redirect_uri, | |
| response_type: "code", | |
| scope: oauth_google.scopes, | |
| } | |
| const url = new URL(oauth_google.endpoint) | |
| url.search = new URLSearchParams(query).toString() | |
| return url.toString() | |
| }, | |
| verifyGoogleCode: async ({ code }: { code: string }) => { | |
| log.info({ code }, "verifying google auth code") | |
| try { | |
| const tokenResponse = await fetch("https://oauth2.googleapis.com/token", { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/x-www-form-urlencoded", | |
| }, | |
| body: new URLSearchParams({ | |
| client_id: oauth_google.client_id, | |
| client_secret: oauth_google.client_secret, | |
| code, | |
| grant_type: "authorization_code", | |
| redirect_uri: oauth_google.redirect_uri, | |
| }), | |
| }) | |
| const tokenData = (await tokenResponse.json()) as { id_token: string } | |
| const ticket = await client.verifyIdToken({ | |
| idToken: tokenData.id_token, | |
| audience: oauth_google.client_id, | |
| }) | |
| const userData = ticket.getPayload() | |
| log.info({ userData }, "google auth successful") | |
| return userData | |
| } catch (error) { | |
| log.error({ error }, "failed to verify google auth code") | |
| throw new Error("Failed to verify Google authentication") | |
| } | |
| }, | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment