-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move business logic from User controller to user service
- Loading branch information
Showing
18 changed files
with
177 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1 @@ | ||
/* Models */ | ||
export * from "./src/auth/entities/user.entity" | ||
export * from "./src/listings/entities/listing.entity" | ||
export * from "./src/units/entities/unit.entity" | ||
export { ListingEventType } from "./src/listings/types/listing-event-type-enum" | ||
export { ListingStatus } from "./src/listings/types/listing-status-enum" | ||
export { ListingEventDto } from "./src/listings/dto/listing-event.dto" | ||
export { ApplicationMethodType } from "./src/listings/types/application-method-type-enum" | ||
export { ApplicationMethodDto } from "./src/listings/dto/application-method.dto" | ||
export { MinMax } from "./src/units/types/min-max" | ||
export { MinMaxCurrency } from "./src/units/types/min-max-currency" | ||
export { UnitSummary } from "./src/units/types/unit-summary" | ||
export { UnitSummaryByReservedType } from "./src/units/types/unit-summary-by-reserved-type" | ||
export { UnitSummaryByAMI } from "./src/units/types/unit-summary-by-ami" | ||
export { HMI } from "./src/units/types/hmi" | ||
export { UnitsSummarized } from "./src/units/types/units-summarized" | ||
export { UserRole } from "./src/auth/enum/user-role-enum" | ||
export {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 13 additions & 4 deletions
17
backend/core/src/auth/passport-strategies/local.strategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Injectable } from "@nestjs/common" | ||
import { User } from "../../auth/entities/user.entity" | ||
import { randomBytes, scrypt } from "crypto" | ||
import { SALT_SIZE, SCRYPT_KEYLEN } from "../constants" | ||
import { InjectRepository } from "@nestjs/typeorm" | ||
import { Repository } from "typeorm" | ||
|
||
@Injectable() | ||
export class PasswordService { | ||
constructor(@InjectRepository(User) private readonly userRepository: Repository<User>) {} | ||
|
||
// passwordHash is a hidden field - we need to build a query to get it directly | ||
public async getUserWithPassword(user: User) { | ||
return await this.userRepository | ||
.createQueryBuilder() | ||
.addSelect("user.passwordHash") | ||
.from(User, "user") | ||
.where("user.id = :id", { id: user.id }) | ||
.getOne() | ||
} | ||
|
||
public async verifyUserPassword(user: User, password: string) { | ||
const userWithPassword = await this.getUserWithPassword(user) | ||
const [salt, savedPasswordHash] = userWithPassword.passwordHash.split("#") | ||
const verifyPasswordHash = await this.hashPassword(password, Buffer.from(salt, "hex")) | ||
return savedPasswordHash === verifyPasswordHash | ||
} | ||
|
||
public async passwordToHash(password: string) { | ||
const salt = this.generateSalt() | ||
const hash = await this.hashPassword(password, salt) | ||
return `${salt.toString("hex")}#${hash}` | ||
} | ||
|
||
private async hashPassword(password: string, salt: Buffer) { | ||
return new Promise<string>((resolve, reject) => | ||
scrypt(password, salt, SCRYPT_KEYLEN, (err, key) => | ||
err ? reject(err) : resolve(key.toString("hex")) | ||
) | ||
) | ||
} | ||
|
||
private generateSalt(size = SALT_SIZE) { | ||
return randomBytes(size) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.