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.
50 lines
2.2 KiB
50 lines
2.2 KiB
"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("bcryptjs");
|
|
let AuthService = class AuthService {
|
|
userService;
|
|
jwtService;
|
|
constructor(userService, jwtService) {
|
|
this.userService = userService;
|
|
this.jwtService = jwtService;
|
|
}
|
|
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([
|
|
(0, common_1.Injectable)(),
|
|
__metadata("design:paramtypes", [users_service_1.UsersService,
|
|
jwt_1.JwtService])
|
|
], AuthService);
|
|
//# sourceMappingURL=auth.service.js.map
|