Skip to content

Commit facead4

Browse files
committed
fix: improve refresh grant flow
1 parent 1a73b08 commit facead4

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/service.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -88,33 +88,32 @@ export class KeycloakService {
8888
}
8989

9090
async refreshGrant(): Promise<TokenSet | undefined | null> {
91-
if (this.tokenSet && !this.tokenSet.expired()) {
92-
return this.tokenSet
93-
}
94-
9591
if (!this.tokenSet) {
96-
this.logger.warn(`Missing token set on refreshGrant.`)
97-
return null
92+
await this.initialize()
9893
}
9994

100-
const { refresh_token } = this.tokenSet
95+
if (!this.tokenSet?.expired()) {
96+
return this.tokenSet
97+
}
10198

102-
if (!refresh_token) {
99+
if (!this.tokenSet.refresh_token) {
103100
this.logger.debug(`Refresh token is missing. Reauthenticating.`)
104101

105102
this.tokenSet = await this.issuerClient?.grant({
106103
clientId: this.options.clientId,
107104
clientSecret: this.options.clientSecret,
108105
grant_type: 'client_credentials',
109106
})
110-
if (this.tokenSet?.access_token) this.client.setAccessToken(this.tokenSet?.access_token)
107+
if (this.tokenSet?.access_token) {
108+
this.client.setAccessToken(this.tokenSet?.access_token)
109+
}
111110

112111
return this.tokenSet
113112
}
114113

115114
this.logger.debug(`Refreshing grant token`)
116115

117-
this.tokenSet = await this.issuerClient?.refresh(refresh_token)
116+
this.tokenSet = await this.issuerClient?.refresh(this.tokenSet.refresh_token)
118117

119118
return this.tokenSet
120119
}

0 commit comments

Comments
 (0)