Skip to content

Commit

Permalink
fix: move decodeCredential to account namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
kasvith committed Oct 30, 2022
1 parent 5466bc8 commit ede9fc2
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 53 deletions.
26 changes: 26 additions & 0 deletions docs/helpers/account.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,29 @@ idRevoke('1618033988749895', done => {
```

- **See More**: [Docs](https://developers.google.com/identity/gsi/web/reference/js-reference#google.accounts.id.revoke)

## decodeCredential()

- **Type**

```ts
function decodeCredential(credential: string): DecodedGoogleUser;
```

- **Details**

Helper method to decode the JWT token retrieved from the GoogleSignIn onSuccess response into a usable Object

Google returns a jwt token encoded using base64url. This method will help you get a typed object to manipulate the data for your application.

:::info
This is not an official method exposed by google
:::

- **Example**

<<< @/helpers/snippets/decodeJwt.vue

**Output:**

<<< @/helpers/snippets/jwtTokenExample.json
22 changes: 0 additions & 22 deletions docs/helpers/oauth2.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,3 @@ revokeAccessToken("ACCESS TOKEN FROM GOOGLE", () => {
```

- **See More**: [Docs](https://developers.google.com/identity/oauth2/web/reference/js-reference#google.accounts.oauth2.revoke)

## decodeJWTToken()

- **Type**

```ts
function decodeJWTToken(jwt: string): JwtToken;
```

- **Details**

Helper method to decode the JWT token retrieved from the GoogleSignIn onSuccess response into a usable Object

Google returns a jwt token encoded using base64url. This method will help you get a typed object to manipulate the data for your application.

- **Example**

<<< @/helpers/snippets/decodeJwt.vue

**Output:**

<<< @/helpers/snippets/jwtTokenExample.json
2 changes: 1 addition & 1 deletion docs/helpers/snippets/decodeJwt.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { GoogleSignInButton, decodeCredential } from "vue3-google-signin";
const handleSignInSuccess = (response: CredentialResponse) => {
const { credential } = response;
const decodedCredential = decodeCredential(credential);
console.log("Credentials", decodedCredential);
console.log("User:", decodedCredential);
};
const handleSignInError = () => {
Expand Down
30 changes: 30 additions & 0 deletions src/utils/account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { RevocationCallback } from "@/interfaces/accounts";
import type { DecodedGoogleUser } from "./types";

/**
* Helper method for [google.accounts.id.revoke](https://developers.google.com/identity/gsi/web/reference/js-reference#google.accounts.id.revoke)
Expand All @@ -13,3 +14,32 @@ export function idRevoke(hint: string, callback?: RevocationCallback) {
callback?.(resp);
});
}

/**
* Decode the credential token retrieved from the GoogleSignIn onSuccess response into a usable Object
*
* @param {string} credential
* @returns {DecodedGoogleUser}
*/
export function decodeCredential(credential: string): DecodedGoogleUser {
const base64Url = credential.split(".")[1];
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
const jsonPayload = decodeURIComponent(
window
.atob(base64)
.split("")
.map((c) => `%${("00" + c.charCodeAt(0).toString(16)).slice(-2)}`)
.join("")
);
const decodedToken = JSON.parse(jsonPayload);
return {
email: decodedToken.email,
email_verified: decodedToken.email_verified,
hd: decodedToken.hd,
family_name: decodedToken.family_name,
given_name: decodedToken.given_name,
name: decodedToken.name,
picture: decodedToken.picture,
id: decodedToken.sub,
};
}
30 changes: 0 additions & 30 deletions src/utils/oauth2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { CodeClientConfig, TokenResponse } from "@/interfaces/oauth2";
import type { DecodedGoogleUser } from "./types";

/**
* Helper method for [google.accounts.oauth2.hasGrantedAllScopes](https://developers.google.com/identity/oauth2/web/reference/js-reference#google.accounts.oauth2.hasGrantedAllScopes)
Expand Down Expand Up @@ -115,32 +114,3 @@ export function buildCodeRequestRedirectUrl(

return `${baseUrl}?${queryParams.toString()}`;
}

/**
* Decode the credential token retrieved from the GoogleSignIn onSuccess response into a usable Object
*
* @param {string} credential
* @returns {DecodedGoogleUser}
*/
export function decodeCredential(credential: string): DecodedGoogleUser {
const base64Url = credential.split(".")[1];
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
const jsonPayload = decodeURIComponent(
window
.atob(base64)
.split("")
.map((c) => `%${("00" + c.charCodeAt(0).toString(16)).slice(-2)}`)
.join("")
);
const decodedToken = JSON.parse(jsonPayload);
return {
email: decodedToken.email,
email_verified: decodedToken.email_verified,
hd: decodedToken.hd,
family_name: decodedToken.family_name,
given_name: decodedToken.given_name,
name: decodedToken.name,
picture: decodedToken.picture,
id: decodedToken.sub,
};
}

0 comments on commit ede9fc2

Please sign in to comment.