From 02e2135380798ae0f0cdbefb16b4556fa553b916 Mon Sep 17 00:00:00 2001 From: Fabian Kutsche Date: Mon, 25 Nov 2024 10:42:32 +0100 Subject: [PATCH] fix: get token properly in auth api client --- package.json | 2 +- src/auth/AuthApiClient.ts | 24 +++++++++++++++--------- src/auth/AuthClient.ts | 17 +++++++++++++---- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 7aa5b09..14a7d8a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kodado/kodado-js", - "version": "1.0.5", + "version": "1.0.6", "browser": "dist/index.js", "main": "dist_node/index.js", "types": "dist/index.d.ts", diff --git a/src/auth/AuthApiClient.ts b/src/auth/AuthApiClient.ts index 1cc60cb..167e5f2 100644 --- a/src/auth/AuthApiClient.ts +++ b/src/auth/AuthApiClient.ts @@ -44,34 +44,36 @@ export class AuthApiClient { fullName, companyName, emailNotifications, + token, }: { fullName?: string; companyName?: string; emailNotifications?: string; + token: string; }) { await fetch(`${this.endpoint}/auth/profile`, { method: "PUT", body: JSON.stringify({ fullName, companyName, emailNotifications }), headers: { - Authorization: this.session?.getIdToken().getJwtToken() || "", + Authorization: token, }, }); } - async deleteUserProfile() { + async deleteUserProfile(token: string) { await fetch(`${this.endpoint}/auth`, { method: "DELETE", headers: { - Authorization: this.session?.getIdToken().getJwtToken() || "", + Authorization: token, }, }); } - async uploadUserProfileImage(image: any) { + async uploadUserProfileImage(image: any, token: string) { const response = await fetch(`${this.endpoint}/auth/profile/image`, { method: "POST", headers: { - Authorization: this.session?.getIdToken().getJwtToken() || "", + Authorization: token, }, }); @@ -85,7 +87,7 @@ export class AuthApiClient { return data; } - async getUserKeys() { + async getUserKeys(token: string) { const response = await fetch(`${this.endpoint}/keys/user`, { method: "POST", headers: { @@ -101,7 +103,7 @@ export class AuthApiClient { const response = await fetch(`${this.endpoint}/keys/user`, { method: "POST", headers: { - Authorization: this.session?.getIdToken().getJwtToken() || "", + Authorization: token, }, body: JSON.stringify({ page: index + 2 }), }); @@ -118,12 +120,16 @@ export class AuthApiClient { return keys; } - async updateItemKeys(encryptionPublicKey: string, encryptedItemKeys: any) { + async updateItemKeys( + encryptionPublicKey: string, + encryptedItemKeys: any, + token: string + ) { await fetch(`${this.endpoint}/auth/password`, { method: "POST", headers: { - Authorization: this.session?.getIdToken().getJwtToken() || "", + Authorization: token, }, body: JSON.stringify({ encryptionPublicKey, diff --git a/src/auth/AuthClient.ts b/src/auth/AuthClient.ts index b19160e..d61df18 100644 --- a/src/auth/AuthClient.ts +++ b/src/auth/AuthClient.ts @@ -257,6 +257,7 @@ export class AuthClient { fullName, companyName, emailNotifications: JSON.stringify(emailNotifications), + token: (await this.getCurrentAuthorizationToken()) || "", }); await this.cognitoClient.updateCognitoProfile(this.session, { fullName, @@ -271,7 +272,10 @@ export class AuthClient { } async uploadProfileImage(image: any) { - await this.apiClient.uploadUserProfileImage(image); + await this.apiClient.uploadUserProfileImage( + image, + (await this.getCurrentAuthorizationToken()) || "" + ); } signOut() { @@ -298,7 +302,9 @@ export class AuthClient { user.setSignInUserSession(this.session); - await this.apiClient.deleteUserProfile(); + await this.apiClient.deleteUserProfile( + (await this.getCurrentAuthorizationToken()) || "" + ); this.user = null; this.session = null; @@ -342,7 +348,9 @@ export class AuthClient { newPassword, }); - const keys = await this.apiClient.getUserKeys(); + const keys = await this.apiClient.getUserKeys( + (await this.getCurrentAuthorizationToken()) || "" + ); const decryptedItemKeys = keys.map((key: any) => ({ ...key, @@ -388,7 +396,8 @@ export class AuthClient { await this.apiClient.updateItemKeys( encodeBase64(newKeys.encryptionPublicKey), - encryptedItemKeys + encryptedItemKeys, + (await this.getCurrentAuthorizationToken()) || "" ); if (this.keys) {