Skip to content

Commit

Permalink
Merge pull request #2830 from getAlby/feat/use-token-events
Browse files Browse the repository at this point in the history
feat: use token events to subscribe to the token changes
  • Loading branch information
rolznz authored Nov 6, 2023
2 parents 8c37bb8 + 39df0ba commit 8463370
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"dependencies": {
"@bitcoin-design/bitcoin-icons-react": "^0.1.10",
"@bitcoinerlab/secp256k1": "^1.0.5",
"@getalby/sdk": "^2.2.3",
"@getalby/sdk": "^2.5.0",
"@headlessui/react": "^1.7.16",
"@lightninglabs/lnc-web": "^0.2.4-alpha",
"@noble/curves": "^1.1.0",
Expand Down
19 changes: 11 additions & 8 deletions src/extension/background-script/connectors/alby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,20 @@ export default class Alby implements Connector {
token: this.config.oAuthToken, // initialize with existing token
});

authClient.on("tokenRefreshed", (token: Token) => {
this._updateOAuthToken(token);
});
// Currently the JS SDK guarantees request of a new refresh token is done synchronously.
// The only way a refresh should fail is if the refresh token has expired, which is handled when the connector is initialized.
// If a token refresh fails after init then the connector will be unusable, but we will still log errors here so that this can be debugged if it does ever happen.
authClient.on("tokenRefreshFailed", (error: Error) => {
console.error("Failed to Refresh token", error);
});

if (this.config.oAuthToken) {
try {
if (authClient.isAccessTokenExpired()) {
const token = await authClient.refreshAccessToken();
await this._updateOAuthToken(token.token);
await authClient.refreshAccessToken();
}
return authClient;
} catch (error) {
Expand Down Expand Up @@ -302,19 +311,13 @@ export default class Alby implements Connector {
if (!this._authUser || !this._client) {
throw new Error("Alby client was not initialized");
}
const oldToken = this._authUser?.token;
let result: T;
try {
result = await func(this._client);
} catch (error) {
console.error(error);

throw error;
} finally {
const newToken = this._authUser.token;
if (newToken && newToken !== oldToken) {
await this._updateOAuthToken(newToken);
}
}
return result;
}
Expand Down
9 changes: 5 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -666,12 +666,13 @@
resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.51.0.tgz#6d419c240cfb2b66da37df230f7e7eef801c32fa"
integrity sha512-HxjQ8Qn+4SI3/AFv6sOrDB+g6PpUTDwSJiQqOrnneEk8L71161srI9gjzzZvYVbzHiVg/BvcH95+cK/zfIt4pg==

"@getalby/sdk@^2.2.3":
version "2.2.3"
resolved "https://registry.npmjs.org/@getalby/sdk/-/sdk-2.2.3.tgz"
integrity sha512-8NvzGtpyne8EmRlCcg/sF3kUZWeHRXqZwS1HNuP1ytNNfmKFUZpo3GOauwzEpFFmaD+Fht5bjT3Y/XLk0QtFSw==
"@getalby/sdk@^2.5.0":
version "2.5.0"
resolved "https://registry.yarnpkg.com/@getalby/sdk/-/sdk-2.5.0.tgz#d1b0a22cbcf986755c4b684096d97f52ed0b469d"
integrity sha512-MRLgI6WxCCLgrar+qDqm/UhKs+V6yXzNm4y1bJRAuN72nkKT+TjTJHCmk9GjTngR3FrOfLbeMsPwBxCmbvfrLQ==
dependencies:
crypto-js "^4.1.1"
events "^3.3.0"
nostr-tools "1.13.1"

"@headlessui/react@^1.7.16":
Expand Down

0 comments on commit 8463370

Please sign in to comment.