✨ Send the right error when asking for a new code
This commit is contained in:
parent
9dc1049ce7
commit
b931e98c29
@ -8,6 +8,7 @@ import CustomersService, {
|
|||||||
NotRegisteredCustomerError,
|
NotRegisteredCustomerError,
|
||||||
PasswordAlreadySetError,
|
PasswordAlreadySetError,
|
||||||
SmsNotExpiredError,
|
SmsNotExpiredError,
|
||||||
|
TooSoonForNewCode,
|
||||||
TotpCodeExpiredError,
|
TotpCodeExpiredError,
|
||||||
} from "@Services/customer/CustomersService/CustomersService";
|
} from "@Services/customer/CustomersService/CustomersService";
|
||||||
import AuthService from "@Services/common/AuthService/AuthService";
|
import AuthService from "@Services/common/AuthService/AuthService";
|
||||||
@ -219,7 +220,7 @@ export default class AuthController extends ApiController {
|
|||||||
}
|
}
|
||||||
this.httpSuccess(response, { partialPhoneNumber: customer.contact?.cell_phone_number.replace(/\s/g, "").slice(-4) });
|
this.httpSuccess(response, { partialPhoneNumber: customer.contact?.cell_phone_number.replace(/\s/g, "").slice(-4) });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof InvalidTotpCodeError || error instanceof TotpCodeExpiredError) {
|
if (error instanceof TooSoonForNewCode || error instanceof TotpCodeExpiredError) {
|
||||||
this.httpUnauthorized(response, error.message);
|
this.httpUnauthorized(response, error.message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,12 @@ export class PasswordAlreadySetError extends Error {
|
|||||||
super("Password already set");
|
super("Password already set");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class TooSoonForNewCode extends Error {
|
||||||
|
constructor() {
|
||||||
|
super("You need to wait at least 30 seconds before asking for a new code");
|
||||||
|
}
|
||||||
|
}
|
||||||
@Service()
|
@Service()
|
||||||
export default class CustomersService extends BaseService {
|
export default class CustomersService extends BaseService {
|
||||||
constructor(
|
constructor(
|
||||||
@ -258,10 +264,10 @@ export default class CustomersService extends BaseService {
|
|||||||
const lastCode = customerHydrated.totpCodes?.find((totpCode) => {
|
const lastCode = customerHydrated.totpCodes?.find((totpCode) => {
|
||||||
return totpCode.expire_at && totpCode.expire_at.getTime() > now;
|
return totpCode.expire_at && totpCode.expire_at.getTime() > now;
|
||||||
});
|
});
|
||||||
if (!lastCode) throw new SmsNotExpiredError();
|
if (!lastCode) throw new TotpCodeExpiredError();
|
||||||
|
|
||||||
// 3: Check if it was created more than 30 seconds ago
|
// 3: Check if it was created more than 30 seconds ago
|
||||||
if (lastCode.created_at && lastCode.created_at.getTime() > now - 30000) throw new SmsNotExpiredError();
|
if (lastCode.created_at && lastCode.created_at.getTime() > now - 30000) throw new TooSoonForNewCode();
|
||||||
|
|
||||||
// 4: Generate a new SMS code
|
// 4: Generate a new SMS code
|
||||||
const totpPin = this.generateTotp();
|
const totpPin = this.generateTotp();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user