Skip to content

Commit

Permalink
✨ feat: UserRepository loginUser 메소드 구현(#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinddings committed Nov 6, 2024
1 parent 4b1ed0e commit 5003ca6
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion BE/src/auth/user.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { InjectDataSource } from '@nestjs/typeorm';
import { DataSource, Repository } from 'typeorm';
import * as bcrypt from 'bcrypt';
Expand All @@ -18,4 +18,14 @@ export class UserRepository extends Repository<User> {
const user = this.create({ email, password: hashedPassword });
await this.save(user);
}

async loginUser(authCredentialsDto: AuthCredentialsDto) {
const { email, password } = authCredentialsDto;
const user = await this.findOne({ where: { email } });
if (user && (await bcrypt.compare(password, user.password))) {
return 'Login successful';
}

throw new UnauthorizedException('Login failed');
}
}

0 comments on commit 5003ca6

Please sign in to comment.