From 2c14649edafb6fbc65d7a9f387b4ab483c6d37ac Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Tue, 1 Aug 2023 23:32:45 +0700 Subject: [PATCH 1/5] fix: fix nostr tools version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bb6a5d0..003804b 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ }, "dependencies": { "crypto-js": "^4.1.1", - "nostr-tools": "^1.13.1" + "nostr-tools": "1.13.1" }, "devDependencies": { "@types/crypto-js": "^4.1.1", From 200419f6d29919b54e0163d05527b0ff41a0614d Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Wed, 26 Jul 2023 08:21:08 +0200 Subject: [PATCH 2/5] v2.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 003804b..edb9f52 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "alby-js-sdk", - "version": "2.1.0", + "version": "2.1.1", "description": "The SDK to integrate with Nostr Wallet Connect and the Alby API", "repository": "https://github.com/getAlby/alby-js-sdk.git", "bugs": "https://github.com/getAlby/alby-js-sdk/issues", From 8ad8a4e88ded24dc3975cb3ff9688e582d010df0 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Wed, 26 Jul 2023 18:24:28 +0200 Subject: [PATCH 3/5] v2.1.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index edb9f52..0f3e419 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "alby-js-sdk", - "version": "2.1.1", + "version": "2.1.2", "description": "The SDK to integrate with Nostr Wallet Connect and the Alby API", "repository": "https://github.com/getAlby/alby-js-sdk.git", "bugs": "https://github.com/getAlby/alby-js-sdk/issues", From 772a21dc3ace1f0343ffe76e28f40c8cf2db4340 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Tue, 1 Aug 2023 19:54:49 +0200 Subject: [PATCH 4/5] yarn lock --- yarn.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn.lock b/yarn.lock index 2b4dc55..c7661f0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4665,7 +4665,7 @@ normalize-url@^6.0.1: resolved "https://registry.yarnpkg.com/normalize-url/-/normalize-url-6.1.0.tgz#40d0885b535deffe3f3147bec877d05fe4c5668a" integrity sha512-DlL+XwOy3NxAQ8xuC0okPgK46iuVNAK01YN7RueYBqqFeGsBjV9XmCAzAdgt+667bCl5kPh9EqKKDwnaPG1I7A== -nostr-tools@^1.13.1: +nostr-tools@1.13.1: version "1.13.1" resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-1.13.1.tgz#191298d85a977cc50790e1c1d1cb83b86d3a9656" integrity sha512-DTwpbxTH1/ar+afWd4gmVdpHH8CF290kdaxi00Llra88SHE6e38XuyzlRABVTcrBaceLMnoDdHmV3x16MoEFJg== From eea29e1cae4db3c20583d25f489076b659af9ed8 Mon Sep 17 00:00:00 2001 From: pavanjoshi914 Date: Thu, 3 Aug 2023 17:08:30 +0530 Subject: [PATCH 5/5] token changed callback --- package.json | 1 + src/OAuth2User.ts | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 0f3e419..cd41245 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "dependencies": { "crypto-js": "^4.1.1", "nostr-tools": "1.13.1" + "events": "^3.3.0" }, "devDependencies": { "@types/crypto-js": "^4.1.1", diff --git a/src/OAuth2User.ts b/src/OAuth2User.ts index 81bf796..b64b140 100644 --- a/src/OAuth2User.ts +++ b/src/OAuth2User.ts @@ -2,6 +2,7 @@ import CryptoJS from 'crypto-js'; import { buildQueryString, basicAuthHeader } from "./utils"; import { OAuthClient, AuthHeader, GetTokenResponse, Token, GenerateAuthUrlOptions } from "./types"; import { RequestOptions, rest } from "./request"; +import EventEmitter from 'events'; const AUTHORIZE_URL = "https://getalby.com/oauth"; @@ -39,13 +40,17 @@ export class OAuth2User implements OAuthClient { code_verifier?: string; code_challenge?: string; private _refreshAccessTokenPromise: Promise<{token: Token}> | null; + private _tokenEvents = new EventEmitter(); constructor(options: OAuth2UserOptions) { const { token, ...defaultOptions } = options; this.options = {client_secret: '', ...defaultOptions}; this.token = token; this._refreshAccessTokenPromise = null; } - + + on(eventName: string, listener: (tokens: Token) => void) { + return this._tokenEvents.on(eventName, listener); + } /** * Refresh the access token */ @@ -177,7 +182,10 @@ export class OAuth2User implements OAuthClient { async getAuthHeader(): Promise { if (!this.token?.access_token) throw new Error("access_token is required"); - if (this.isAccessTokenExpired()) await this.refreshAccessToken(); + if (this.isAccessTokenExpired()){ + await this.refreshAccessToken() + this._tokenEvents.emit("tokens", this.token); + }; return { Authorization: `Bearer ${this.token.access_token}`, };