diff --git a/src/app/api/customer/AuthController.ts b/src/app/api/customer/AuthController.ts index 5492bd52..e57f695c 100644 --- a/src/app/api/customer/AuthController.ts +++ b/src/app/api/customer/AuthController.ts @@ -22,7 +22,7 @@ export default class AuthController extends ApiController { return; } - let customer = await this.customerService.getOne({ + const customer = await this.customerService.getOne({ where: { contact: { email, @@ -41,8 +41,16 @@ export default class AuthController extends ApiController { // if no sms code has been generated, generate it // if code has expired, regenerate it const now = new Date().getTime(); - if (!customer.smsCodeExpire || now > customer.smsCodeExpire.getTime()) { - customer = await this.customerService.generateSmsCode(customer); + if (customer.smsCodeExpire && now < customer.smsCodeExpire.getTime()) { + this.httpBadRequest(response, "Last sms code is still valid"); + return; + } + + try { + await this.customerService.generateSmsCode(customer); + } catch (error) { + console.log(error); + this.httpInternalError(response); } if (!customer.password) { @@ -128,8 +136,12 @@ export default class AuthController extends ApiController { return; } + const customerHydrated = Customer.hydrate(customer); + const payload = await this.authService.getCustomerJwtPayload([customerHydrated]); + const accessToken = this.authService.generateAccessToken(payload); + const refreshToken = this.authService.generateRefreshToken(payload); try { - this.httpSuccess(response, { customer: Customer.hydrate(customer) }); + this.httpSuccess(response, { accessToken, refreshToken }); } catch (error) { console.log(error); this.httpInternalError(response); @@ -192,8 +204,12 @@ export default class AuthController extends ApiController { const hashedPassword = await this.authService.hashPassword(password); await this.customerService.setPassword(customer, hashedPassword); + const customerHydrated = Customer.hydrate(customer); + const payload = await this.authService.getCustomerJwtPayload([customerHydrated]); + const accessToken = this.authService.generateAccessToken(payload); + const refreshToken = this.authService.generateRefreshToken(payload); try { - this.httpSuccess(response, { email }); + this.httpSuccess(response, { accessToken, refreshToken }); } catch (error) { console.log(error); this.httpInternalError(response);