fix: refresh token

This commit is contained in:
Kieran 2024-01-04 20:43:42 +00:00
parent e73aa303a8
commit b3f04c8cd9
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941

View File

@ -12,6 +12,7 @@ import {
WalletInvoice, WalletInvoice,
WalletInvoiceState, WalletInvoiceState,
} from "."; } from ".";
import { base64 } from "@scure/base";
export default class AlbyWallet implements LNWallet { export default class AlbyWallet implements LNWallet {
#token: OAuthToken; #token: OAuthToken;
@ -125,6 +126,26 @@ export default class AlbyWallet implements LNWallet {
async #refreshToken() { async #refreshToken() {
if (this.#token.created_at + this.#token.expires_in < unixNow()) { if (this.#token.created_at + this.#token.expires_in < unixNow()) {
// refresh // refresh
const params = new URLSearchParams();
params.set("refresh_token", this.#token.refresh_token);
params.set("grant_type", "refresh_token");
const req = await fetch("https://api.getalby.com/oauth/token", {
method: "POST",
body: params,
headers: {
accept: "application/json",
"content-type": "application/x-www-form-urlencoded",
authorization: `Basic ${base64.encode(
new TextEncoder().encode(`${CONFIG.alby?.clientId}:${CONFIG.alby?.clientSecret}`),
)}`,
},
});
const json = await req.json();
if (req.ok) {
this.#token = json as OAuthToken;
this.onChange(this.#token);
}
} }
} }
} }