Skip to content

Commit 846e91c

Browse files
Merge pull request #1 from SculptTechProject/feature/auth/identifier
Support login via username or email
2 parents ac5fa73 + 48ffbed commit 846e91c

File tree

7 files changed

+26
-12
lines changed

7 files changed

+26
-12
lines changed

src/Controllers/AuthController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public async Task<IActionResult> Register([FromBody] RegisterRequest request, Ca
3232
[HttpPost("login")]
3333
public async Task<IActionResult> Login([FromBody] LoginRequest request, CancellationToken ct)
3434
{
35-
var result = await _authService.LoginAsync(request.Email, request.Password, ct);
35+
var result = await _authService.LoginAsync(request.Identifier, request.Password, ct);
3636
if (result is null)
3737
{
3838
return Unauthorized(new { message = "Invalid credentials" });

src/Controllers/ProfileController.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ namespace sparkly_server.Controllers
1212
public class ProfileController : ControllerBase
1313
{
1414
private readonly ICurrentUser _currentUser;
15-
private readonly IUserService _userService;
16-
private readonly IJwtProvider _jwtProvider;
17-
private readonly IAuthService _authService;
1815
private readonly AppDbContext _db;
1916

2017
public ProfileController(ICurrentUser currentUser, AppDbContext db)

src/DTO/Auth.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace sparkly_server.DTO
22
{
33
public record RegisterRequest(string Username, string Email, string Password);
4-
public record LoginRequest(string Email, string Password);
4+
public record LoginRequest(string Identifier, string Password);
55
public record AuthResponse(string AccessToken, string RefreshToken);
66
}

src/Services/Auth/AuthService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public AuthService(IUserService userService, IJwtProvider jwtProvider)
1313
_jwtProvider = jwtProvider;
1414
}
1515

16-
public async Task<AuthResult?> LoginAsync(string email, string password, CancellationToken ct = default)
16+
public async Task<AuthResult?> LoginAsync(string identifier, string password, CancellationToken ct = default)
1717
{
18-
var user = await _userService.ValidateUserAsync(email, password, ct);
18+
var user = await _userService.ValidateUserAsync(identifier, password, ct);
1919
if (user is null)
2020
{
2121
return null;

src/Services/Auth/IAuthService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ DateTime RefreshTokenExpiresAt
99

1010
public interface IAuthService
1111
{
12-
Task<AuthResult> LoginAsync(string email, string password, CancellationToken ct = default);
12+
Task<AuthResult?> LoginAsync(string identifier, string password, CancellationToken ct = default);
1313
Task<AuthResult> RefreshAsync(string refreshToken, CancellationToken ct = default);
1414
Task LogoutAsync(string refreshToken, CancellationToken ct = default);
1515
}

src/Services/Users/IUserService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace sparkly_server.Services.Users
55
public interface IUserService
66
{
77
Task<User> RegisterAsync(string userName, string email, string password, CancellationToken ct = default);
8-
Task<User?> ValidateUserAsync(string email, string password, CancellationToken ct = default);
8+
Task<User?> ValidateUserAsync(string identifier, string password, CancellationToken ct = default);
99
}
1010
}

src/Services/Users/UserService.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,31 @@ public async Task<User> RegisterAsync(string userName, string email, string pass
4545
return user;
4646
}
4747

48-
public async Task<User?> ValidateUserAsync(string email, string password, CancellationToken ct = default)
48+
public async Task<User?> ValidateUserAsync(string identifier, string password, CancellationToken ct = default)
4949
{
50-
var user = await _users.GetByEmailAsync(email, ct);
51-
if (user is null)
50+
if (string.IsNullOrWhiteSpace(identifier))
5251
{
5352
return null;
5453
}
5554

55+
User? user;
56+
57+
if (identifier.Contains("@"))
58+
{
59+
// traktujemy jako email
60+
user = await _users.GetByEmailAsync(identifier, ct);
61+
}
62+
else
63+
{
64+
// traktujemy jako username
65+
user = await _users.GetByUserNameAsync(identifier, ct);
66+
}
67+
68+
if (user is null)
69+
{
70+
return null;
71+
}
72+
5673
var result = _passwordHasher.VerifyHashedPassword(user, user.PasswordHash, password);
5774
if (result is PasswordVerificationResult.Failed)
5875
{

0 commit comments

Comments
 (0)