Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use token events to subscribe to the token changes #2830

Merged
merged 5 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
pavanjoshi914 marked this conversation as resolved.
Show resolved Hide resolved
});

if (this.config.oAuthToken) {
try {
if (authClient.isAccessTokenExpired()) {
const token = await authClient.refreshAccessToken();
await this._updateOAuthToken(token.token);
await authClient.refreshAccessToken();
pavanjoshi914 marked this conversation as resolved.
Show resolved Hide resolved
}
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
Loading