11using CodeWorks . Auth . Extensions ;
22using CodeWorks . Auth . Interfaces ;
3+ using CodeWorks . Auth . Models ;
34using CodeWorks . Auth . Security ;
45using Microsoft . AspNetCore . Identity ;
56
67namespace CodeWorks . Auth . Services ;
78
89public interface IAuthService < TIdentity > where TIdentity : IAccountIdentity
910{
10- Task < AuthResult > LoginAsync ( string email , string password ) ;
11- Task < AuthResult > RegisterAsync ( TIdentity user , string password ) ;
12- Task < AuthResult > ResetPasswordAsync ( string email , string newPassword ) ;
13- Task < AuthResult > RefreshAuthToken ( string token , int refreshExtensionInHours = 1 ) ;
14- AuthResult GenerateAuthToken ( IAccountIdentity user ) ;
11+ Task < AuthResult < TIdentity > > LoginAsync ( string email , string password ) ;
12+ Task < AuthResult < TIdentity > > RegisterAsync ( TIdentity user , string password ) ;
13+ Task < AuthResult < TIdentity > > ResetPasswordAsync ( string email , string newPassword ) ;
14+ Task < AuthResult < TIdentity > > RefreshAuthToken ( string token , int refreshExtensionInHours = 1 ) ;
15+ AuthResult < TIdentity > GenerateAuthToken ( TIdentity user ) ;
1516
1617}
1718
@@ -20,59 +21,59 @@ public class AuthService<TIdentity>(IAccountIdentityStore<TIdentity> store, IJwt
2021 private readonly IAccountIdentityStore < TIdentity > _store = store ;
2122 private readonly IJwtService _jwt = jwt ;
2223
23- public async Task < AuthResult > RegisterAsync ( TIdentity user , string password )
24+ public async Task < AuthResult < TIdentity > > RegisterAsync ( TIdentity user , string password )
2425 {
2526 if ( await _store . EmailExistsAsync ( user . Email ) )
26- return AuthResult . Failure ( "Email already registered." ) ;
27+ return AuthResult < TIdentity > . Failure ( "Email already registered." ) ;
2728
28- user . PasswordHash = PasswordHelper < IAccountIdentity > . HashPassword ( user , password ) ;
29+ user . PasswordHash = PasswordHelper < TIdentity > . HashPassword ( user , password ) ;
2930 await _store . SaveAsync ( user ) ;
30- return AuthResult . Success ( user , _jwt . GenerateToken ( user ) ) ;
31+ return AuthResult < TIdentity > . Success ( user , _jwt . GenerateToken ( user ) ) ;
3132 }
3233
33- public async Task < AuthResult > LoginAsync ( string email , string password )
34+ public async Task < AuthResult < TIdentity > > LoginAsync ( string email , string password )
3435 {
3536 var user = await _store . FindByEmailAsync ( email ) ;
3637 if ( user == null )
37- return AuthResult . Failure ( "Invalid credentials." ) ;
38+ return AuthResult < TIdentity > . Failure ( "Invalid credentials." ) ;
3839
39- var result = PasswordHelper < IAccountIdentity > . VerifyPassword ( user , user . PasswordHash , password ) ;
40+ var result = PasswordHelper < TIdentity > . VerifyPassword ( user , user . PasswordHash , password ) ;
4041 if ( result == PasswordVerificationResult . Failed )
41- return AuthResult . Failure ( "Invalid credentials." ) ;
42+ return AuthResult < TIdentity > . Failure ( "Invalid credentials." ) ;
4243
43- return AuthResult . Success ( user , _jwt . GenerateToken ( user ) ) ;
44+ return AuthResult < TIdentity > . Success ( user , _jwt . GenerateToken ( user ) ) ;
4445 }
4546
46- public async Task < AuthResult > ResetPasswordAsync ( string email , string newPassword )
47+ public async Task < AuthResult < TIdentity > > ResetPasswordAsync ( string email , string newPassword )
4748 {
4849 var user = await _store . FindByEmailAsync ( email ) ;
4950 if ( user == null )
50- return AuthResult . Failure ( "User not found." ) ;
51+ return AuthResult < TIdentity > . Failure ( "User not found." ) ;
5152
52- user . PasswordHash = PasswordHelper < IAccountIdentity > . HashPassword ( user , newPassword ) ;
53+ user . PasswordHash = PasswordHelper < TIdentity > . HashPassword ( user , newPassword ) ;
5354 await _store . SaveAsync ( user ) ;
54- return AuthResult . Success ( user , _jwt . GenerateToken ( user ) ) ;
55+ return AuthResult < TIdentity > . Success ( user , _jwt . GenerateToken ( user ) ) ;
5556 }
5657
57- public AuthResult GenerateAuthToken ( IAccountIdentity user )
58+ public AuthResult < TIdentity > GenerateAuthToken ( TIdentity user )
5859 {
5960 if ( user == null )
60- return AuthResult . Failure ( "Invalid credentials." ) ;
61- return AuthResult . Success ( user , _jwt . GenerateToken ( user ) ) ;
61+ return AuthResult < TIdentity > . Failure ( "Invalid credentials." ) ;
62+ return AuthResult < TIdentity > . Success ( user , _jwt . GenerateToken ( user ) ) ;
6263 }
6364
6465
65- public async Task < AuthResult > RefreshAuthToken ( string token , int refreshExtensionInHours = 1 )
66+ public async Task < AuthResult < TIdentity > > RefreshAuthToken ( string token , int refreshExtensionInHours = 1 )
6667 {
6768 var email = _jwt . GetEmailFromToken ( token ) ;
6869 var user = await _store . FindByEmailAsync ( email ) ;
6970 if ( user == null )
70- return AuthResult . Failure ( "Invalid credentials." ) ;
71+ return AuthResult < TIdentity > . Failure ( "Invalid credentials." ) ;
7172
7273 var result = _jwt . RefreshToken ( token , user , refreshExtensionInHours ) ;
7374 if ( result == null )
74- return AuthResult . Failure ( "Invalid token." ) ;
75- return AuthResult . Success ( user , result ) ;
75+ return AuthResult < TIdentity > . Failure ( "Invalid token." ) ;
76+ return AuthResult < TIdentity > . Success ( user , result ) ;
7677 }
7778
7879}
0 commit comments