Skip to content

Commit

Permalink
feat: extract refresh function
Browse files Browse the repository at this point in the history
  • Loading branch information
strackfeldt committed Dec 11, 2024
1 parent d8c7c01 commit a9ff532
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions packages/openauth/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ export function createClient(input: {
refresh: json.refresh_token as string,
}
},
async refresh(token: string) {
const tokens = await f(issuer + "/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
grant_type: "refresh_token",
refresh_token: token,
}).toString(),
})
const json = (await tokens.json()) as any
if (!tokens.ok) {
console.error(json)
throw new InvalidRefreshTokenError()
}
return {
access: json.access_token as string,
refresh: json.refresh_token as string,
}
},
async verify<T extends SubjectSchema>(
subjects: T,
token: string,
Expand Down Expand Up @@ -151,30 +172,17 @@ export function createClient(input: {
}
} catch (e) {
if (e instanceof errors.JWTExpired && options?.refresh) {
const wk = await getIssuer()
const tokens = await f(wk.token_endpoint, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
grant_type: "refresh_token",
refresh_token: options.refresh,
}).toString(),
})
const json = (await tokens.json()) as any
if (!tokens.ok) {
console.error(json)
throw new InvalidRefreshTokenError()
}
const verified = await result.verify(subjects, json.access_token, {
refresh: json.refresh_token,
const tokens = await this.refresh(options.refresh)

const verified = await result.verify(subjects, tokens.access, {
refresh: tokens.refresh,
issuer,
fetch: options?.fetch,
})

verified.tokens = {
access: json.access_token,
refresh: json.refresh_token,
access: tokens.access,
refresh: tokens.refresh,
}
return verified
}
Expand Down

0 comments on commit a9ff532

Please sign in to comment.