From 68b4b16006dcb07b74269689ace4005e1104a5cc Mon Sep 17 00:00:00 2001 From: Naijith Gopal Date: Thu, 17 Feb 2022 23:08:13 +0530 Subject: [PATCH] bazaar: use token for backend requests Signed-off-by: Naijith Gopal --- plugins/bazaar/src/api.ts | 57 ++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 12 deletions(-) diff --git a/plugins/bazaar/src/api.ts b/plugins/bazaar/src/api.ts index 2d661506bc..71dd607606 100644 --- a/plugins/bazaar/src/api.ts +++ b/plugins/bazaar/src/api.ts @@ -58,37 +58,53 @@ export class BazaarClient implements BazaarApi { async updateProject(bazaarProject: any): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + const { token } = await this.identityApi.getCredentials(); return await fetch(`${baseUrl}/projects`, { method: 'PUT', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, + headers: token + ? { + Authorization: `Bearer ${token}`, + Accept: 'application/json', + 'Content-Type': 'application/json', + } + : { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, body: JSON.stringify(bazaarProject), }).then(resp => resp.json()); } async addProject(bazaarProject: any): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + const { token } = await this.identityApi.getCredentials(); return await fetch(`${baseUrl}/projects`, { method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, + headers: token + ? { + Authorization: `Bearer ${token}`, + Accept: 'application/json', + 'Content-Type': 'application/json', + } + : { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, body: JSON.stringify(bazaarProject), }).then(resp => resp.json()); } async getProjectById(id: number): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + const { token } = await this.identityApi.getCredentials(); const response = await fetch( `${baseUrl}/projects/${encodeURIComponent(id)}`, { method: 'GET', + headers: token ? { Authorization: `Bearer ${token}` } : {}, }, ); @@ -97,11 +113,13 @@ export class BazaarClient implements BazaarApi { async getProjectByRef(entityRef: string): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + const { token } = await this.identityApi.getCredentials(); const response = await fetch( `${baseUrl}/projects/${encodeURIComponent(entityRef)}`, { method: 'GET', + headers: token ? { Authorization: `Bearer ${token}` } : {}, }, ); @@ -110,11 +128,13 @@ export class BazaarClient implements BazaarApi { async getMembers(id: number): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + const { token } = await this.identityApi.getCredentials(); return await fetch( `${baseUrl}/projects/${encodeURIComponent(id)}/members`, { method: 'GET', + headers: token ? { Authorization: `Bearer ${token}` } : {}, }, ).then(resp => resp.json()); } @@ -122,6 +142,7 @@ export class BazaarClient implements BazaarApi { async addMember(id: number, userId: string): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); const { picture } = await this.identityApi.getProfileInfo(); + const { token } = await this.identityApi.getCredentials(); await fetch( `${baseUrl}/projects/${encodeURIComponent( @@ -129,10 +150,16 @@ export class BazaarClient implements BazaarApi { )}/member/${encodeURIComponent(userId)}`, { method: 'PUT', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, + headers: token + ? { + Authorization: `Bearer ${token}`, + Accept: 'application/json', + 'Content-Type': 'application/json', + } + : { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, body: JSON.stringify({ picture }), }, ); @@ -140,6 +167,7 @@ export class BazaarClient implements BazaarApi { async deleteMember(id: number, userId: string): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + const { token } = await this.identityApi.getCredentials(); await fetch( `${baseUrl}/projects/${encodeURIComponent( @@ -147,23 +175,28 @@ export class BazaarClient implements BazaarApi { )}/member/${encodeURIComponent(userId)}`, { method: 'DELETE', + headers: token ? { Authorization: `Bearer ${token}` } : {}, }, ); } async getProjects(): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + const { token } = await this.identityApi.getCredentials(); return await fetch(`${baseUrl}/projects`, { method: 'GET', + headers: token ? { Authorization: `Bearer ${token}` } : {}, }).then(resp => resp.json()); } async deleteProject(id: number): Promise { const baseUrl = await this.discoveryApi.getBaseUrl('bazaar'); + const { token } = await this.identityApi.getCredentials(); await fetch(`${baseUrl}/projects/${encodeURIComponent(id)}`, { method: 'DELETE', + headers: token ? { Authorization: `Bearer ${token}` } : {}, }); } }