Регистрация теперь тоже возвращает jwt

This commit is contained in:
2026-06-16 00:25:21 +03:00
parent 99973eba04
commit e55c54e75d
2 changed files with 5 additions and 3 deletions
+2 -2
View File
@@ -13,8 +13,8 @@ namespace URL_Shortener.Controllers
[HttpPost("register")] [HttpPost("register")]
public async Task<IActionResult> RegisterAsync(RegisterUserDTO dto) public async Task<IActionResult> RegisterAsync(RegisterUserDTO dto)
{ {
await _userService.RegisterAsync(dto.Name, dto.Email, dto.Password); var jwt = await _userService.RegisterAsync(dto.Name, dto.Email, dto.Password);
return Ok(); return Ok(jwt);
} }
[HttpPost("login")] [HttpPost("login")]
+3 -1
View File
@@ -9,7 +9,7 @@ namespace URL_Shortener.Services
private readonly AppDbContext _db = db; private readonly AppDbContext _db = db;
private readonly JwtService _jwtService = jwtService; private readonly JwtService _jwtService = jwtService;
public async Task RegisterAsync(string userName, string email, string password, CancellationToken cancellationToken = default) public async Task<string> RegisterAsync(string userName, string email, string password, CancellationToken cancellationToken = default)
{ {
if (await _db.Users.FirstOrDefaultAsync(u => u.Email == email, cancellationToken) != null) if (await _db.Users.FirstOrDefaultAsync(u => u.Email == email, cancellationToken) != null)
throw new Exception("This user already exists."); throw new Exception("This user already exists.");
@@ -23,6 +23,8 @@ namespace URL_Shortener.Services
await _db.Users.AddAsync(user, cancellationToken); await _db.Users.AddAsync(user, cancellationToken);
await _db.SaveChangesAsync(cancellationToken); await _db.SaveChangesAsync(cancellationToken);
return _jwtService.GenerateToken(user);
} }
public async Task<string> LoginAsync(string email, string password, CancellationToken cancellationToken = default) public async Task<string> LoginAsync(string email, string password, CancellationToken cancellationToken = default)