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 1 commit
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: 12 additions & 7 deletions src/extension/background-script/connectors/alby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,18 @@ export default class Alby implements Connector {
token: this.config.oAuthToken, // initialize with existing token
});

authClient.on("tokenRefreshed", async (token: Token) => {
await this._updateOAuthToken(token);
pavanjoshi914 marked this conversation as resolved.
Show resolved Hide resolved
});

authClient.on("tokenRefreshFailed", async (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,7 +309,6 @@ 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);
Expand All @@ -311,10 +317,9 @@ export default class Alby implements Connector {

throw error;
} finally {
const newToken = this._authUser.token;
if (newToken && newToken !== oldToken) {
await this._updateOAuthToken(newToken);
}
this._authUser.on("tokenRefreshed", async (token: Token) => {
pavanjoshi914 marked this conversation as resolved.
Show resolved Hide resolved
await this._updateOAuthToken(token);
});
}
return result;
}
Expand Down
9 changes: 5 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -572,12 +572,13 @@
resolved "https://registry.npmjs.org/@eslint/js/-/js-8.48.0.tgz"
integrity sha512-ZSjtmelB7IJfWD2Fvb7+Z+ChTIKWq6kjda95fLcQKNS5aheVHn4IkfgRQE3sIIzTcSLwLcLZUD9UBt+V7+h+Pw==

"@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