From b955f12f7fa41a7051964884154bc3996c60d079 Mon Sep 17 00:00:00 2001 From: rizkifuad Date: Tue, 14 Dec 2021 20:59:50 +0700 Subject: [PATCH] feat: use same token for teams --- components/api/api.ts | 6 ++++++ components/users/user.ts | 16 ++++++---------- lib/components/api/api.js | 7 +++++++ lib/components/users/user.js | 12 ++++++------ 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/components/api/api.ts b/components/api/api.ts index 831e365..dd238ba 100644 --- a/components/api/api.ts +++ b/components/api/api.ts @@ -29,8 +29,14 @@ export default class Api extends Component { this.apiClient.basePath = basePath; this.bearer = this.apiClient.authentications.Bearer; const currentLogin = this.helper.getProp("current_login") as string || "user"; + const currentUserType = this.helper.getProp("current_user_type") as string || "user"; const tokenObj = this.helper.getProp("token") as JsonObject || {}; this.bearer.apiKey = `Bearer ${tokenObj[currentLogin]}`; + if (currentUserType === "team") { + const teamId = this.helper.getProp("team_id"); + console.log({teamId}) + this.apiClient.defaultHeaders = { "X-auth-teamid": teamId }; + } this.botApi = new this.zaun.BotApi(); this.authApi = new this.zaun.AuthApi(); diff --git a/components/users/user.ts b/components/users/user.ts index a399c4e..81885db 100644 --- a/components/users/user.ts +++ b/components/users/user.ts @@ -153,16 +153,12 @@ export default class User extends Component { response.body.teams.filter((team: any) => team.username === name) : []; if (teams.length > 0) { - const result = await this.helper.toPromise( - this.api.authApi, - this.api.authApi.tokensPost, - { - type: "team", - teamId: teams[0].teamId - } - ); - const token = result.data.id; - this.setToken({ name, type: "team" }, token); + const team = teams[0]; + const currentToken = this.helper.getProp("token") as JsonObject || {} + const currentLogin = this.helper.getProp("current_login") as string || "user"; + + this.setToken({ name, type: "team" }, currentToken[currentLogin] as string); + this.helper.setProp("team_id", team.teamId); this.helper.setProp("current_login", name); this.helper.setProp("current_user_type", "team"); console.log(`Switched to team: ${colors.green(name)}`); diff --git a/lib/components/api/api.js b/lib/components/api/api.js index 514250c..315f270 100644 --- a/lib/components/api/api.js +++ b/lib/components/api/api.js @@ -11,8 +11,15 @@ class Api extends merapi_1.Component { this.apiClient.basePath = basePath; this.bearer = this.apiClient.authentications.Bearer; const currentLogin = this.helper.getProp("current_login") || "user"; + const currentUserType = this.helper.getProp("current_user_type") || "user"; const tokenObj = this.helper.getProp("token") || {}; this.bearer.apiKey = `Bearer ${tokenObj[currentLogin]}`; + console.log("MNgigla", currentUserType); + if (currentUserType === "team") { + const teamId = this.helper.getProp("team_id"); + console.log({ teamId }); + this.apiClient.defaultHeaders = { "X-auth-teamid": teamId }; + } this.botApi = new this.zaun.BotApi(); this.authApi = new this.zaun.AuthApi(); this.userApi = new this.zaun.UserApi(); diff --git a/lib/components/users/user.js b/lib/components/users/user.js index 701b890..b372b03 100644 --- a/lib/components/users/user.js +++ b/lib/components/users/user.js @@ -150,12 +150,12 @@ class User extends merapi_1.Component { const teams = response && response.body ? response.body.teams.filter((team) => team.username === name) : []; if (teams.length > 0) { - const result = yield this.helper.toPromise(this.api.authApi, this.api.authApi.tokensPost, { - type: "team", - teamId: teams[0].teamId - }); - const token = result.data.id; - this.setToken({ name, type: "team" }, token); + const team = teams[0]; + console.log({ team }); + const currentToken = this.helper.getProp("token") || {}; + const currentLogin = this.helper.getProp("current_login") || "user"; + this.setToken({ name, type: "team" }, currentToken[currentLogin]); + this.helper.setProp("team_id", team.teamId); this.helper.setProp("current_login", name); this.helper.setProp("current_user_type", "team"); console.log(`Switched to team: ${colors.green(name)}`);