Skip to content

Commit 49b7f38

Browse files
committed
fix: handle 401 response for sessions
1 parent 16016df commit 49b7f38

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

api/rest-client.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ export class RingRestClient {
104104
headers: {
105105
'content-type': 'application/json',
106106
'2fa-support': 'true',
107-
'2fa-code': twoFactorAuthCode || ''
107+
'2fa-code': twoFactorAuthCode || '',
108+
hardware_id: hardwareId
108109
}
109110
})
110111

@@ -172,7 +173,14 @@ export class RingRestClient {
172173
try {
173174
return await this.fetchNewSession(authToken)
174175
} catch (e) {
175-
if (e && e.response && e.response.status === 429) {
176+
const response = e.response || {}
177+
178+
if (response.status === 401) {
179+
this.refreshAuth()
180+
return this.getSession()
181+
}
182+
183+
if (response.status === 429) {
176184
const retryAfter = e.response.headers['retry-after'],
177185
waitSeconds = isNaN(retryAfter) ? 200 : Number.parseInt(retryAfter)
178186

@@ -204,18 +212,19 @@ export class RingRestClient {
204212
json?: boolean
205213
responseType?: ResponseType
206214
}): Promise<T & ExtendedResponse> {
207-
await this.sessionPromise
208-
const { method, url, data, json, responseType } = options,
209-
authTokenResponse = await this.authPromise,
210-
headers: { [key: string]: string } = {
211-
'content-type': json
212-
? 'application/json'
213-
: 'application/x-www-form-urlencoded',
214-
authorization: `Bearer ${authTokenResponse.access_token}`,
215-
hardware_id: hardwareId
216-
}
215+
const { method, url, data, json, responseType } = options
217216

218217
try {
218+
await this.sessionPromise
219+
const authTokenResponse = await this.authPromise,
220+
headers: { [key: string]: string } = {
221+
'content-type': json
222+
? 'application/json'
223+
: 'application/x-www-form-urlencoded',
224+
authorization: `Bearer ${authTokenResponse.access_token}`,
225+
hardware_id: hardwareId
226+
}
227+
219228
return await requestWithRetry<T>({
220229
method: method || 'GET',
221230
url,

0 commit comments

Comments
 (0)