be
This commit is contained in:
33
backend/dist/auth/auth.service.js
vendored
33
backend/dist/auth/auth.service.js
vendored
@@ -21,28 +21,25 @@ let AuthService = class AuthService {
|
||||
this.userService = userService;
|
||||
this.jwtService = jwtService;
|
||||
}
|
||||
async validateUser(name, password) {
|
||||
const user = await this.userService.findByName(name);
|
||||
if (user && await bcrypt.compare(password, user.password)) {
|
||||
const { password, ...rest } = user;
|
||||
return rest;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
async login(dto) {
|
||||
const user = await this.userService.findByName(dto.name);
|
||||
if (!user)
|
||||
throw new common_1.UnauthorizedException('Login failed');
|
||||
const passwordCheck = await bcrypt.compare(dto.password, user.password);
|
||||
if (!passwordCheck)
|
||||
throw new common_1.UnauthorizedException('Login failed');
|
||||
const payload = { username: user.name, sub: user.id };
|
||||
return { access_token: this.jwtService.sign(payload) };
|
||||
}
|
||||
async signup(dto) {
|
||||
const hashed = await bcrypt.hash(dto.password, 10);
|
||||
return this.userService.create({ ...dto, password: hashed });
|
||||
}
|
||||
async login(dto) {
|
||||
const user = await this.userService.findByName(dto.name);
|
||||
if (!user || !await bcrypt.compare(dto.password, user.password))
|
||||
throw new common_1.UnauthorizedException('Login failed');
|
||||
const payload = { username: user.name, sub: user.id };
|
||||
const token = this.jwtService.sign(payload);
|
||||
return {
|
||||
access_token: token,
|
||||
user: {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
exports.AuthService = AuthService;
|
||||
exports.AuthService = AuthService = __decorate([
|
||||
|
Reference in New Issue
Block a user