Skip to content

Commit

Permalink
Chore: auth service implemented at user service 🪚🛡️
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-0x29a committed Sep 25, 2024
1 parent 0ab6071 commit 7d587cc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import { LogService } from "../services/log.service";


class UserController extends BaseController {
private Service = new UserService(UserModel)
private LogService = new LogService()
public readonly router = Router()

constructor() {
Expand All @@ -24,6 +22,9 @@ class UserController extends BaseController {
}
}

private Service = new UserService(UserModel, this.getApplicationSecret())
private LogService = new LogService()

private loadRoutes() {
this.router.get('/', Guard, this.getAll)
this.router.get('/:id', Guard, this.getById)
Expand Down
2 changes: 1 addition & 1 deletion src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { Environment as EnvironmentService } from "./evironment.service";
export { Environment as EnvironmentService } from "./environment.service";
2 changes: 1 addition & 1 deletion src/services/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let UserModel = {
findOne: jest.fn()
} as unknown as ModelCtor<Model<any, any>>

const service = new UserService(UserModel)
const service = new UserService(UserModel, 'key')

const hash = '$2b$10$1Q6Zz1'

Expand Down
21 changes: 10 additions & 11 deletions src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,27 @@ import { User, UserModel } from "../models";
import { searchEntity } from "../utils/searchEntity";
import * as bcrypt from 'bcrypt'
import { LegendHttpError } from "../web/errors";
import jwt from 'jsonwebtoken'
import { Auth as AuthService } from "./auth.service";


class UserService {
constructor(private readonly userModel: typeof UserModel) {}
constructor(private readonly userModel: typeof UserModel, private readonly applicationSecret: string = '') {}

async signIn(signInDto: SignInDto): Promise<string> {
const user = await searchEntity<User>(this.userModel, { username: signInDto.username }, false, false)

if (user === null) {
throw new LegendHttpError(401, 'User or password invalid.')
}

const isPasswordMatch = await bcrypt.compare(signInDto.password, user.password)
const isPasswordMatch = await bcrypt.compare(
signInDto.password,
user?.password || ''
)

if (!isPasswordMatch) {
if ((user === null) || !isPasswordMatch) {
throw new LegendHttpError(401, 'User or password invalid.')
}

const token = await jwt.sign({ id: user.id, username: user.username }, process.env.JWT_SECRET, {
expiresIn: '20min'
})
const auth = new AuthService(user, this.applicationSecret)

const token = auth.signToken()

return token
}
Expand Down

0 comments on commit 7d587cc

Please sign in to comment.