Renaming smsCode into TotpCode

This commit is contained in:
Maxime Lalo 2023-11-27 15:32:52 +01:00
parent 3835127d63
commit 6c00162544
5 changed files with 22 additions and 30 deletions

View File

@ -56,7 +56,7 @@
"file-type-checker": "^1.0.8",
"fp-ts": "^2.16.1",
"jsonwebtoken": "^9.0.0",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.97",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.98",
"module-alias": "^2.2.2",
"monocle-ts": "^2.3.13",
"multer": "^1.4.5-lts.1",

View File

@ -1,6 +1,4 @@
-- AlterTable
ALTER TABLE "customers" ADD COLUMN "password" VARCHAR(255),
ADD COLUMN "passwordCode" VARCHAR(255),
ADD COLUMN "passwordcodeExpire" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "smsCode" VARCHAR(255),
ADD COLUMN "smsCodeExpire" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;
ADD COLUMN "totpCode" VARCHAR(255),
ADD COLUMN "totpCodeExpire" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP;

View File

@ -102,10 +102,8 @@ model Customers {
office_folders OfficeFolders[] @relation("OfficeFolderHasCustomers")
documents Documents[]
password String? @db.VarChar(255)
smsCode String? @db.VarChar(255)
smsCodeExpire DateTime? @default(now())
passwordCode String? @db.VarChar(255)
passwordcodeExpire DateTime? @default(now())
totpCode String? @db.VarChar(255)
totpCodeExpire DateTime? @default(now())
@@map("customers")

View File

@ -5,10 +5,8 @@ import { Customers, ECivility, ECustomerStatus, Prisma } from "@prisma/client";
import { Customer } from "le-coffre-resources/dist/SuperAdmin";
type IExcludedCustomerVars = {
smsCode?: string;
smsCodeExpire?: Date;
passwordCode?: string;
passwordcodeExpire?: Date;
totpCode?: string;
totpCodeExpire?: Date;
password?: string;
};
@Service()
@ -95,10 +93,8 @@ export default class CustomersRepository extends BaseRepository {
address: {},
},
},
smsCode: excludedVars && excludedVars.smsCode,
smsCodeExpire: excludedVars && excludedVars.smsCodeExpire,
passwordCode: excludedVars && excludedVars.passwordCode,
passwordcodeExpire: excludedVars && excludedVars.passwordcodeExpire,
totpCode: excludedVars && excludedVars.totpCode,
totpCodeExpire: excludedVars && excludedVars.totpCodeExpire,
password: excludedVars && excludedVars.password,
},
};

View File

@ -75,7 +75,7 @@ export default class CustomersService extends BaseService {
if (!customer) return null;
const now = new Date().getTime();
// Check if the SMS code is still valid
if (customer.smsCodeExpire && now < customer.smsCodeExpire.getTime()) throw new SmsNotExpiredError();
if (customer.totpCodeExpire && now < customer.totpCodeExpire.getTime()) throw new SmsNotExpiredError();
const totpPin = this.generateTotp();
@ -96,11 +96,11 @@ export default class CustomersService extends BaseService {
* 6: Set the password in database
* 7: Returns the customer
* @param email
* @param smsCode
* @param totpCode
* @param password
* @returns
*/
public async setFirstPassword(email: string, smsCode: string, password: string): Promise<Customer | null> {
public async setFirstPassword(email: string, totpCode: string, password: string): Promise<Customer | null> {
// 1: Check if the customer exists
const customer = await this.getByEmail(email);
if (!customer) return null;
@ -109,11 +109,11 @@ export default class CustomersService extends BaseService {
if (customer.password) throw new PasswordAlreadySetError();
// 3: Check if the SMS code is existing and is not expired
if (!customer.smsCode || !customer.smsCodeExpire || new Date().getTime() > customer.smsCodeExpire.getTime())
if (!customer.totpCode || !customer.totpCodeExpire || new Date().getTime() > customer.totpCodeExpire.getTime())
throw new TotpCodeExpiredError();
// 4: Check if the SMS code is valid
if (customer.smsCode !== smsCode) throw new InvalidTotpCodeError();
if (customer.totpCode !== totpCode) throw new InvalidTotpCodeError();
// 5: Hash the password
const hashedPassword = await this.authService.hashPassword(password);
@ -135,21 +135,21 @@ export default class CustomersService extends BaseService {
* 5: Check if the password is valid
* 6: Return the customer
* @param email
* @param smsCode
* @param totpCode
* @param password
* @returns Customer | null
*/
public async login(email: string, smsCode: string, password: string): Promise<Customer | null> {
public async login(email: string, totpCode: string, password: string): Promise<Customer | null> {
// 1: Check if the customer exists
const customer = await this.getByEmail(email);
if (!customer) return null;
// 2: Check if the SMS code is existing and is not expired
if (!customer.smsCode || !customer.smsCodeExpire || new Date().getTime() > customer.smsCodeExpire.getTime())
if (!customer.totpCode || !customer.totpCodeExpire || new Date().getTime() > customer.totpCodeExpire.getTime())
throw new TotpCodeExpiredError();
// 3: Check if the SMS code is valid
if (customer.smsCode !== smsCode) throw new InvalidTotpCodeError();
if (customer.totpCode !== totpCode) throw new InvalidTotpCodeError();
// 4: Check if the user has a password or it's their first login
if (!customer.password) throw new NotRegisteredCustomerError();
@ -201,8 +201,8 @@ export default class CustomersService extends BaseService {
...customer,
}),
{
smsCode: totpPin.toString(),
smsCodeExpire: expireAt,
totpCode: totpPin.toString(),
totpCodeExpire: expireAt,
},
);
}
@ -226,11 +226,11 @@ export default class CustomersService extends BaseService {
if (!customer) return null;
// 2: Check if the SMS code is existing and is not expired
if (!customer.smsCode || !customer.smsCodeExpire || new Date().getTime() > customer.smsCodeExpire.getTime())
if (!customer.totpCode || !customer.totpCodeExpire || new Date().getTime() > customer.totpCodeExpire.getTime())
throw new TotpCodeExpiredError();
// 3: Check if the SMS code is valid
if (customer.smsCode !== totpCode) throw new InvalidTotpCodeError();
if (customer.totpCode !== totpCode) throw new InvalidTotpCodeError();
// 4: Return the customer
return customer;