From dc326dcf3c2122971ef73c4446934c0fcbe4a192 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Fri, 12 Jan 2024 11:45:07 +0900 Subject: [PATCH] refactor: checkLoggedIn --- src/controllers/serviceController.ts | 11 +++++++---- src/service.ts | 14 +++++++------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/controllers/serviceController.ts b/src/controllers/serviceController.ts index 7f636b1..7f65b6d 100644 --- a/src/controllers/serviceController.ts +++ b/src/controllers/serviceController.ts @@ -120,13 +120,16 @@ export default class ServiceController { return (await this.ain.getValue(Path.app(serviceName).billingConfig())).depositAddress; } - isLoggedIn(): void { - if(!this.ain.getDefaultAccount()) - throw new Error('You should login First.'); + checkLoggedIn(): void { + try { + !this.ain.getAddress(); + } catch(error) { + throw new Error('You should login first.'); + } } async isAdmin(serviceName: string): Promise { - this.isLoggedIn(); + this.checkLoggedIn(); const adminPath = `/manage_app/${serviceName}/config/admin`; const adminList = await this.ain.getValue(adminPath); if(!adminList[this.ain.getAddress()]) { diff --git a/src/service.ts b/src/service.ts index 5f94786..8e7524c 100644 --- a/src/service.ts +++ b/src/service.ts @@ -42,7 +42,7 @@ export default class Service { * @returns {string} Transaction hash. */ async chargeCredit(amount: number) { - this.isLoggedIn(); + this.checkLoggedIn(); return await this.serviceController.chargeCredit(this.serviceName, amount); } @@ -52,7 +52,7 @@ export default class Service { * @returns {string} Transaction hash. */ async withdrawCredit(amount: number) { - this.isLoggedIn(); + this.checkLoggedIn(); return await this.serviceController.withdrawCredit(this.serviceName, amount); } @@ -61,7 +61,7 @@ export default class Service { * @returns {number} Amount of credit balance. */ async getCreditBalance() { - this.isLoggedIn(); + this.checkLoggedIn(); return await this.serviceController.getCreditBalance(this.serviceName); } @@ -70,7 +70,7 @@ export default class Service { * @returns {creditHistories} Histories of credit deposit and usage. */ async getCreditHistory() { - this.isLoggedIn(); + this.checkLoggedIn(); return await this.serviceController.getCreditHistory(this.serviceName); } @@ -80,7 +80,7 @@ export default class Service { * @returns {string} Response data from service. */ async request(requestData: any, requestKey?: string) { - this.isLoggedIn(); + this.checkLoggedIn(); return await this.serviceController.request(this.serviceName, requestData, requestKey); } @@ -113,7 +113,7 @@ export default class Service { return this.serviceController.isAdmin(this.serviceName); } - private isLoggedIn() { - return this.serviceController.isLoggedIn(); + private checkLoggedIn() { + return this.serviceController.checkLoggedIn(); } }