Skip to content

Commit

Permalink
fix: get token properly in auth api client
Browse files Browse the repository at this point in the history
  • Loading branch information
Fubinator committed Nov 25, 2024
1 parent ab1adb7 commit 02e2135
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
24 changes: 15 additions & 9 deletions src/auth/AuthApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});

Expand All @@ -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: {
Expand All @@ -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 }),
});
Expand All @@ -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,
Expand Down
17 changes: 13 additions & 4 deletions src/auth/AuthClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ export class AuthClient {
fullName,
companyName,
emailNotifications: JSON.stringify(emailNotifications),
token: (await this.getCurrentAuthorizationToken()) || "",
});
await this.cognitoClient.updateCognitoProfile(this.session, {
fullName,
Expand All @@ -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() {
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -388,7 +396,8 @@ export class AuthClient {

await this.apiClient.updateItemKeys(
encodeBase64(newKeys.encryptionPublicKey),
encryptedItemKeys
encryptedItemKeys,
(await this.getCurrentAuthorizationToken()) || ""
);

if (this.keys) {
Expand Down

0 comments on commit 02e2135

Please sign in to comment.