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.
tryFullStack/backend/dist/auth/auth.service.js

58 lines
2.4 KiB

2 months 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");
2 months ago
const bcrypt = require("bcryptjs");
2 months ago
let AuthService = class AuthService {
userService;
jwtService;
constructor(userService, jwtService) {
this.userService = userService;
this.jwtService = jwtService;
}
2 months ago
async signup(dto) {
const hashed = await bcrypt.hash(dto.password, 10);
return this.userService.create({ ...dto, password: hashed });
2 months ago
}
async login(dto) {
2 months ago
const user = await this.userService.findByName(dto.name);
2 months ago
if (!user || !(await bcrypt.compare(dto.password, user.password)))
2 months ago
throw new common_1.UnauthorizedException('Login failed');
const payload = { username: user.name, sub: user.id };
2 months ago
const token = this.jwtService.sign(payload);
return {
access_token: token,
user: {
id: user.id,
name: user.name,
email: user.email,
2 months ago
},
2 months ago
};
2 months ago
}
2 months ago
async isNameAvailable(name) {
const user = await this.userService.findByName(name);
return !user;
}
async isEmailAvailabe(email) {
const user = await this.userService.findByEmail(email);
return !user;
}
2 months ago
};
exports.AuthService = AuthService;
exports.AuthService = AuthService = __decorate([
(0, common_1.Injectable)(),
2 months ago
__metadata("design:paramtypes", [users_service_1.UsersService,
jwt_1.JwtService])
2 months ago
], AuthService);
//# sourceMappingURL=auth.service.js.map