Skip to content

Commit

Permalink
Merge pull request #11 from playhaste/feature/add-avatar-web-sdk
Browse files Browse the repository at this point in the history
Feature/add avatar web sdk
  • Loading branch information
rallieon committed Nov 5, 2021
2 parents 4754c1d + 970ebff commit 95d8a0a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ The `getTokenDetails` function will return a `HasteAuthentication` object with t
export type HasteAuthentication = {
token: string;
isAuthenticated: boolean;
picture: string; // Url
displayName: string;
};
```
Expand Down
2 changes: 2 additions & 0 deletions packages/web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ The `getTokenDetails` function will return a `HasteAuthentication` object with t
export type HasteAuthentication = {
token: string;
isAuthenticated: boolean;
picture: string; // Url
displayName: string;
};
```

Expand Down
21 changes: 18 additions & 3 deletions packages/web/src/api/hasteClient.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { isBrowser } from '../util/environmentCheck';
import createAuth0Client, { Auth0Client } from '@auth0/auth0-spa-js';
import { Auth0Client } from '@auth0/auth0-spa-js';
import { HasteClientConfiguration } from '../config/hasteClientConfiguration';
import SecureLS from 'secure-ls';
import jwtDecode, { JwtPayload } from 'jwt-decode';

export type HasteAuthentication = {
token: string;
isAuthenticated: boolean;
picture: string;
displayName: string;
};

export class HasteClient {
Expand Down Expand Up @@ -82,35 +84,48 @@ export class HasteClient {
const decoded = jwtDecode<JwtPayload>(cachedToken);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const expiration = decoded.exp;

if (expiration && new Date(expiration * 1000) > new Date()) {
return {
token: cachedToken,
// eslint-disable-next-line dot-notation
picture: decoded['picture'],
displayName: decoded['https://hastearcade.com/displayName'],
isAuthenticated: true,
} as HasteAuthentication;
} else {
this.ls.remove('haste:config');
return {
token: '',
picture: '',
displayName: '',
isAuthenticated: false,
} as HasteAuthentication;
}
} else if (idToken) {
this.ls.set('haste:config', idToken);
urlSearchParams.delete('id_token');
const decoded = jwtDecode<JwtPayload>(idToken);
const plainUrl = window.location.href.split('?')[0];
window.history.pushState({}, document.title, `${plainUrl}${urlSearchParams.toString()}`);
return {
token: idToken,
// eslint-disable-next-line dot-notation
picture: decoded['picture'],
displayName: decoded['https://hastearcade.com/displayName'],
isAuthenticated: true,
} as HasteAuthentication;
} else {
const accessToken = await this.auth0Client.getTokenSilently();
const idTokenClaims = await this.auth0Client.getIdTokenClaims();
const idToken = idTokenClaims.__raw;

const decoded = jwtDecode<JwtPayload>(idToken);
if (accessToken) {
return {
token: idTokenClaims.__raw,
token: idToken,
// eslint-disable-next-line dot-notation
picture: decoded['picture'],
displayName: decoded['https://hastearcade.com/displayName'],
isAuthenticated: true,
} as HasteAuthentication;
}
Expand Down

0 comments on commit 95d8a0a

Please sign in to comment.