Skip to content

Commit ae1e5db

Browse files
fix: only set authorization header when token is string (#950)
1 parent 551a6fb commit ae1e5db

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

src/core/auth.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,12 @@ export class Auth {
342342
if (!_endpoint.headers) {
343343
_endpoint.headers = {}
344344
}
345-
if (!_endpoint.headers[tokenName] && isSet(token) && token) {
345+
if (
346+
!_endpoint.headers[tokenName] &&
347+
isSet(token) &&
348+
token &&
349+
typeof token === 'string'
350+
) {
346351
_endpoint.headers[tokenName] = token
347352
}
348353

src/inc/request-handler.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ export class RequestHandler {
1616
this.interceptor = null
1717
}
1818

19-
setHeader(token: string | boolean): void {
19+
setHeader(token: string): void {
2020
if (this.scheme.options.token.global) {
2121
// Set Authorization token for all axios requests
22-
this.axios.setHeader(this.scheme.options.token.name, token + '')
22+
this.axios.setHeader(this.scheme.options.token.name, token)
2323
}
2424
}
2525

@@ -115,7 +115,10 @@ export class RequestHandler {
115115
// Refresh tokens if token has expired
116116

117117
private _getUpdatedRequestConfig(config, token: string | boolean) {
118-
config.headers[this.scheme.options.token.name] = token
118+
if (typeof token === 'string') {
119+
config.headers[this.scheme.options.token.name] = token
120+
}
121+
119122
return config
120123
}
121124

src/inc/token.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,22 @@ export class Token {
2424
const token = addTokenPrefix(tokenValue, this.scheme.options.token.type)
2525

2626
this._setToken(token)
27-
this.scheme.requestHandler.setHeader(token)
2827
this._updateExpiration(token)
2928

29+
if (typeof token === 'string') {
30+
this.scheme.requestHandler.setHeader(token)
31+
}
32+
3033
return token
3134
}
3235

3336
sync(): string | boolean {
3437
const token = this._syncToken()
3538
this._syncExpiration()
36-
this.scheme.requestHandler.setHeader(token)
39+
40+
if (typeof token === 'string') {
41+
this.scheme.requestHandler.setHeader(token)
42+
}
3743

3844
return token
3945
}

src/utils/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ export function addTokenPrefix(
158158
token: string | boolean,
159159
tokenType: string | false
160160
): string | boolean {
161-
if (!token || !tokenType || (token + '').startsWith(tokenType)) {
161+
if (
162+
!token ||
163+
!tokenType ||
164+
typeof token !== 'string' ||
165+
token.startsWith(tokenType)
166+
) {
162167
return token
163168
}
164169

@@ -169,11 +174,11 @@ export function removeTokenPrefix(
169174
token: string | boolean,
170175
tokenType: string | false
171176
): string | boolean {
172-
if (!token || !tokenType) {
177+
if (!token || !tokenType || typeof token !== 'string') {
173178
return token
174179
}
175180

176-
return (token + '').replace(tokenType + ' ', '')
181+
return token.replace(tokenType + ' ', '')
177182
}
178183

179184
export function urlJoin(...args: string[]): string {

0 commit comments

Comments
 (0)