Skip to content

Commit

Permalink
🥅 Catch expired authcore access token
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Sep 12, 2024
1 parent 68e4790 commit dddfb34
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"@walletconnect/utils": "2.8.0",
"classnames": "^2.3.1",
"cosmjs-types": "^0.5.1",
"jwt-decode": "^4.0.0",
"qrcode.react": "^3.1.0",
"react-intl": "^6.2.5"
},
Expand Down
24 changes: 17 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createRoot, Root } from 'react-dom/client';
import { AccountData } from '@cosmjs/proto-signing';
import { IQRCodeModal } from '@walletconnect/legacy-types';
import EventEmitter from 'events';
import { jwtDecode } from "jwt-decode";

import { AuthcoreDialog } from './components/authcore-dialog';
import { ConnectionMethodSelectionDialog } from './components/connection-method-selection-dialog';
Expand Down Expand Up @@ -362,12 +363,26 @@ export class LikeCoinWalletConnector {
language = this.options.language,
initialScreen?: AuthCoreInitialScreen
) => {
let initiator: Promise<LikeCoinWalletConnectorInitResponse>;
let initiator: Promise<LikeCoinWalletConnectorInitResponse> | null = null;

switch (methodType) {
case LikeCoinWalletConnectorMethodType.LikerId:
const { accessToken } = params || {};
if (!accessToken) {
if (accessToken) {
try {
const decoded = jwtDecode(accessToken);
if (decoded.exp && decoded.exp * 1000 < Date.now()) {
throw new Error('TOKEN_EXPIRED');
}
initiator = initAuthcore(this.options, {
accessToken,
initialScreen,
});
} catch (err) {
console.error(err);
}
}
if (!initiator) {
initiator = new Promise(resolve => {
this._renderingRoot.render(
<IntlProvider language={language}>
Expand Down Expand Up @@ -401,11 +416,6 @@ export class LikeCoinWalletConnector {
</IntlProvider>
);
});
} else {
initiator = initAuthcore(this.options, {
accessToken,
initialScreen,
});
}
break;

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10283,6 +10283,11 @@ junk@^3.1.0:
resolved "https://registry.yarnpkg.com/junk/-/junk-3.1.0.tgz#31499098d902b7e98c5d9b9c80f43457a88abfa1"
integrity sha512-pBxcB3LFc8QVgdggvZWyeys+hnrNWg4OcZIU/1X59k5jQdLBlCsYGRQaz234SqoRLTCgMH00fY0xRJH+F9METQ==

jwt-decode@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/jwt-decode/-/jwt-decode-4.0.0.tgz#2270352425fd413785b2faf11f6e755c5151bd4b"
integrity sha512-+KJGIyHgkGuIq3IEBNftfhW/LfWhXUIY6OmyVWjliu5KH1y0fw7VQ8YndE2O4qZdMSd9SqbnC8GOcZEy0Om7sA==

keyvaluestorage-interface@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/keyvaluestorage-interface/-/keyvaluestorage-interface-1.0.0.tgz#13ebdf71f5284ad54be94bd1ad9ed79adad515ff"
Expand Down

0 comments on commit dddfb34

Please sign in to comment.