You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
2.8 KiB
64 lines
2.8 KiB
3 weeks ago
|
"use strict";
|
||
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||
|
};
|
||
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
||
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.AuthService = void 0;
|
||
|
const common_1 = require("@nestjs/common");
|
||
|
const jwt_1 = require("@nestjs/jwt");
|
||
|
const users_service_1 = require("../users/users.service");
|
||
|
const bcrypt = require("bcrypt");
|
||
|
let AuthService = class AuthService {
|
||
|
userService;
|
||
|
jwtService;
|
||
|
constructor(userService, jwtService) {
|
||
|
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) {
|
||
|
try {
|
||
|
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) };
|
||
|
}
|
||
|
catch (error) {
|
||
|
if (error instanceof common_1.UnauthorizedException)
|
||
|
throw error;
|
||
|
throw new common_1.InternalServerErrorException('Login failed (Internal)');
|
||
|
}
|
||
|
}
|
||
|
async signup(dto) {
|
||
|
try {
|
||
|
const hashed = await bcrypt.hash(dto.password, 10);
|
||
|
return this.userService.create({ ...dto, password: hashed });
|
||
|
}
|
||
|
catch (error) {
|
||
|
throw new common_1.BadRequestException('Signup failed');
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
exports.AuthService = AuthService;
|
||
|
exports.AuthService = AuthService = __decorate([
|
||
|
(0, common_1.Injectable)(),
|
||
|
__metadata("design:paramtypes", [users_service_1.UsersService, jwt_1.JwtService])
|
||
|
], AuthService);
|
||
|
//# sourceMappingURL=auth.service.js.map
|