forked from Kieran/snort
fix: refresh token
This commit is contained in:
parent
e73aa303a8
commit
b3f04c8cd9
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user