Login & set password not returns a valid JWT

This commit is contained in:
Maxime Lalo 2023-11-24 10:49:18 +01:00
parent 65d6e548d1
commit 624dc26dca

View File

@ -22,7 +22,7 @@ export default class AuthController extends ApiController {
return; return;
} }
let customer = await this.customerService.getOne({ const customer = await this.customerService.getOne({
where: { where: {
contact: { contact: {
email, email,
@ -41,8 +41,16 @@ export default class AuthController extends ApiController {
// if no sms code has been generated, generate it // if no sms code has been generated, generate it
// if code has expired, regenerate it // if code has expired, regenerate it
const now = new Date().getTime(); const now = new Date().getTime();
if (!customer.smsCodeExpire || now > customer.smsCodeExpire.getTime()) { if (customer.smsCodeExpire && now < customer.smsCodeExpire.getTime()) {
customer = await this.customerService.generateSmsCode(customer); 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) { if (!customer.password) {
@ -128,8 +136,12 @@ export default class AuthController extends ApiController {
return; return;
} }
const customerHydrated = Customer.hydrate<Customer>(customer);
const payload = await this.authService.getCustomerJwtPayload([customerHydrated]);
const accessToken = this.authService.generateAccessToken(payload);
const refreshToken = this.authService.generateRefreshToken(payload);
try { try {
this.httpSuccess(response, { customer: Customer.hydrate<Customer>(customer) }); this.httpSuccess(response, { accessToken, refreshToken });
} catch (error) { } catch (error) {
console.log(error); console.log(error);
this.httpInternalError(response); this.httpInternalError(response);
@ -192,8 +204,12 @@ export default class AuthController extends ApiController {
const hashedPassword = await this.authService.hashPassword(password); const hashedPassword = await this.authService.hashPassword(password);
await this.customerService.setPassword(customer, hashedPassword); await this.customerService.setPassword(customer, hashedPassword);
const customerHydrated = Customer.hydrate<Customer>(customer);
const payload = await this.authService.getCustomerJwtPayload([customerHydrated]);
const accessToken = this.authService.generateAccessToken(payload);
const refreshToken = this.authService.generateRefreshToken(payload);
try { try {
this.httpSuccess(response, { email }); this.httpSuccess(response, { accessToken, refreshToken });
} catch (error) { } catch (error) {
console.log(error); console.log(error);
this.httpInternalError(response); this.httpInternalError(response);