diff --git a/src/services/nativeAuth/nativeAuth.ts b/src/services/nativeAuth/nativeAuth.ts index ca61f650e..cc376718d 100644 --- a/src/services/nativeAuth/nativeAuth.ts +++ b/src/services/nativeAuth/nativeAuth.ts @@ -53,6 +53,10 @@ export const nativeAuth = (config?: NativeAuthConfigType) => { initProps?.noCache )); + if (!response) { + return ''; + } + const { hash, timestamp } = response; const encodedExtraInfo = nativeAuthClient.encodeValue( JSON.stringify({ @@ -64,8 +68,8 @@ export const nativeAuth = (config?: NativeAuthConfigType) => { const encodedOrigin = nativeAuthClient.encodeValue(origin); return `${encodedOrigin}.${hash}.${expirySeconds}.${encodedExtraInfo}`; - } catch (err) { - console.error('Error getting native auth token: ', err); + } catch (err: any) { + console.error('Error getting native auth token: ', err.toString()); return ''; } }; diff --git a/src/services/nativeAuth/tests/nativeAuth.test.ts b/src/services/nativeAuth/tests/nativeAuth.test.ts index 3abc10136..855fef490 100644 --- a/src/services/nativeAuth/tests/nativeAuth.test.ts +++ b/src/services/nativeAuth/tests/nativeAuth.test.ts @@ -98,19 +98,21 @@ describe('Native Auth', () => { expect(token).toStrictEqual(TOKEN); }); - it('Internal server error', async () => { + it('should return empty string when API call fails', async () => { server.use(...handlers.serverError); //this will make sure to expire the cache jest .useFakeTimers() .setSystemTime(new Date().setSeconds(new Date().getSeconds() + 60)); + const client = nativeAuth({ origin: ORIGIN, apiAddress: API_URL }); - await expect(client.initialize()).rejects.toThrow(); + const nativeAuthToken = await client.initialize(); + expect(nativeAuthToken).toStrictEqual(''); }); it('Generate Access token', () => {