"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 bcrypt = require("bcryptjs"); const users_service_1 = require("../users/users.service"); let AuthService = class AuthService { constructor(usersService, jwtService) { this.usersService = usersService; this.jwtService = jwtService; } async register(dto) { const existing = await this.usersService.findByUsername(dto.username); if (existing) throw new common_1.ConflictException('Användarnamnet är redan taget'); const passwordHash = await bcrypt.hash(dto.password, 12); const user = await this.usersService.create({ username: dto.username, email: dto.email, passwordHash, }); return this.issueToken(user.id, user.username, user.role, user.isPremium); } async login(dto) { const user = await this.usersService.findByUsername(dto.username); if (!user) throw new common_1.UnauthorizedException('Felaktigt användarnamn eller lösenord'); const valid = await bcrypt.compare(dto.password, user.passwordHash); if (!valid) throw new common_1.UnauthorizedException('Felaktigt användarnamn eller lösenord'); return this.issueToken(user.id, user.username, user.role, user.isPremium); } issueToken(userId, username, role, isPremium) { const payload = { sub: userId, username, role, isPremium }; return { accessToken: this.jwtService.sign(payload), userId, username, role, isPremium, }; } }; 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