-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(auth): refactoring crypt logic
- Loading branch information
Showing
9 changed files
with
93 additions
and
30 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
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,14 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import type { PasswordCryptor } from '@undb/core' | ||
import * as bcrypt from 'bcrypt' | ||
|
||
@Injectable() | ||
export class CryptService implements PasswordCryptor { | ||
hash(password: string): Promise<string> { | ||
return bcrypt.hash(password, 10) | ||
} | ||
|
||
compare(p1: string, p2: string): Promise<boolean> { | ||
return bcrypt.compare(p1, p2) | ||
} | ||
} |
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,11 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { UserService, type IUserRepository } from '@undb/core' | ||
import { InjectUserRepository } from '../core/user/adapters/index.js' | ||
import { CryptService } from './crypt.service.js' | ||
|
||
@Injectable() | ||
export class NestUserService extends UserService { | ||
constructor(cryptor: CryptService, @InjectUserRepository() repo: IUserRepository) { | ||
super(cryptor, repo) | ||
} | ||
} |
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 +1,2 @@ | ||
export * from './anonymous.service.js' | ||
export * from './user.service.js' |
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,40 @@ | ||
import { WithUserColor } from '../specifications/user-color.js' | ||
import { WithUserEmail } from '../specifications/user-email.specification.js' | ||
import { WithUserId } from '../specifications/user-id.specification.js' | ||
import { WithUserPassword } from '../specifications/user-password.specification.js' | ||
import { WithUsername } from '../specifications/username.specification.js' | ||
import { UserFactory } from '../user.factory.js' | ||
import type { User } from '../user.js' | ||
import type { IUserRepository } from '../user.repository.js' | ||
|
||
export interface IUserService { | ||
register(email: string, password: string): Promise<User> | ||
} | ||
|
||
export interface PasswordCryptor { | ||
hash(password: string): Promise<string> | ||
compare(p1: string, p2: string): Promise<boolean> | ||
} | ||
|
||
export class UserService implements IUserService { | ||
constructor( | ||
protected readonly cryptor: PasswordCryptor, | ||
protected readonly repo: IUserRepository, | ||
) {} | ||
|
||
async register(email: string, password: string): Promise<User> { | ||
const hashedPassword = await this.cryptor.hash(password) | ||
|
||
const user = UserFactory.create( | ||
WithUserEmail.fromString(email), | ||
WithUserPassword.fromString(hashedPassword), | ||
WithUserId.create(), | ||
WithUsername.fromEmail(email), | ||
WithUserColor.random(), | ||
) | ||
|
||
await this.repo.insert(user) | ||
|
||
return user | ||
} | ||
} |
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