Skip to content

Commit 17e6a3b

Browse files
committed
enable replace token function
1 parent e44b9e4 commit 17e6a3b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

lib/context.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ async function refresh({ tokenEndpointParams } = {}) {
4747
return this.accessToken;
4848
}
4949

50+
async function replace(refreshToken) {
51+
let { config, req } = weakRef(this);
52+
53+
const client = await getClient(config);
54+
const newTokenSet = await client.refresh(refreshToken);
55+
56+
// Update the session
57+
const session = req[config.session.name];
58+
Object.assign(session, {
59+
id_token: newTokenSet.id_token,
60+
access_token: newTokenSet.access_token,
61+
refresh_token: newTokenSet.refresh_token || refreshToken,
62+
token_type: newTokenSet.token_type,
63+
expires_at: newTokenSet.expires_at,
64+
});
65+
66+
// Delete the old token set
67+
const cachedTokenSet = weakRef(session);
68+
69+
delete cachedTokenSet.value;
70+
71+
return this.accessToken;
72+
}
73+
5074
function tokenSet() {
5175
const contextCache = weakRef(this);
5276
const session = contextCache.req[contextCache.config.session.name];
@@ -111,6 +135,7 @@ class RequestContext {
111135
expires_in,
112136
isExpired: isExpired.bind(this),
113137
refresh: refresh.bind(this),
138+
replace: replace.bind(this),
114139
};
115140
} catch (err) {
116141
return undefined;

0 commit comments

Comments
 (0)