@@ -3,6 +3,7 @@ import { JwtService } from '@nestjs/jwt';
33import { ConfigService } from '@nestjs/config' ;
44import * as bcrypt from 'bcrypt' ;
55import { UserService } from '../user/user.service' ;
6+ import { UserRepository } from '../user/user.repository' ;
67import { RegisterDto } from './dto/register.dto' ;
78import { LoginDto } from './dto/login.dto' ;
89import { RefreshTokenDto } from './dto/refresh-token.dto' ;
@@ -13,6 +14,7 @@ export class AuthService {
1314
1415 constructor (
1516 private userService : UserService ,
17+ private userRepository : UserRepository ,
1618 private jwtService : JwtService ,
1719 private configService : ConfigService ,
1820 ) { }
@@ -44,6 +46,11 @@ export class AuthService {
4446 throw new UnauthorizedException ( 'Неверный email или пароль' ) ;
4547 }
4648
49+ // Проверяем, что пользователь имеет роль ADMIN
50+ if ( user . role !== 'ADMIN' ) {
51+ throw new UnauthorizedException ( 'Доступ разрешен только администраторам' ) ;
52+ }
53+
4754 const payload = { email : user . email , sub : user . id } ;
4855 const accessTokenExpiresIn = this . configService . get < string > ( 'JWT_EXPIRES_IN' ) || '24h' ;
4956 const refreshTokenExpiresIn = this . configService . get < string > ( 'JWT_REFRESH_EXPIRES_IN' ) || '7d' ;
@@ -72,12 +79,19 @@ export class AuthService {
7279 try {
7380 const refreshTokenSecret = this . configService . get < string > ( 'JWT_REFRESH_SECRET' ) || this . configService . get < string > ( 'JWT_SECRET' ) || 'your-secret-key' ;
7481 const payload = this . jwtService . verify ( refreshTokenDto . refresh_token , { secret : refreshTokenSecret } ) ;
75- const user = await this . userService . findOne ( payload . sub ) ;
82+
83+ // Используем репозиторий для получения оригинальной роли (без форматирования)
84+ const user = await this . userRepository . findById ( payload . sub ) ;
7685
7786 if ( ! user ) {
7887 throw new UnauthorizedException ( 'Пользователь не найден' ) ;
7988 }
8089
90+ // Проверяем, что пользователь имеет роль ADMIN
91+ if ( user . role !== 'ADMIN' ) {
92+ throw new UnauthorizedException ( 'Доступ разрешен только администраторам' ) ;
93+ }
94+
8195 const newPayload = { email : user . email , sub : user . id } ;
8296 const accessTokenExpiresIn = this . configService . get < string > ( 'JWT_EXPIRES_IN' ) || '24h' ;
8397 const refreshTokenExpiresIn = this . configService . get < string > ( 'JWT_REFRESH_EXPIRES_IN' ) || '7d' ;
0 commit comments