33using System . IdentityModel . Tokens . Jwt ;
44using System . Security . Claims ;
55using System . Text ;
6+ using Extensions ;
67using Interfaces ;
78using Microsoft . AspNetCore . Identity ;
89using Microsoft . AspNetCore . Mvc ;
@@ -18,7 +19,6 @@ public class UserService : IUserService
1819 private readonly UserManager < AppUser > _userManager ;
1920 private readonly RoleManager < AppRole > _roleManager ;
2021 private readonly IConfiguration _configuration ;
21- private const string UserRoleId = "5ad1268f-f61f-4b1c-b690-cbf8c3d35019" ;
2222 private readonly TableBookingContext _dbContext ;
2323
2424 public UserService ( UserManager < AppUser > userManager ,
@@ -42,36 +42,38 @@ public async Task<IActionResult> Register(UserRegisterDto dto)
4242 if ( emailExists != null )
4343 return new BadRequestObjectResult ( $ "User with the same email found: { dto . Email } .") ;
4444
45- var appUserRole = await _roleManager . FindByIdAsync ( UserRoleId ) ;
45+ var appUserRole = await _roleManager . FindByNameAsync ( "User" ) ;
4646 if ( appUserRole == null )
47- return new BadRequestObjectResult ( $ "Can't find role by UserRoleId: { UserRoleId } ") ;
47+ return new BadRequestObjectResult ( $ "Can't find role by name 'User'. ") ;
4848
4949 var user = new AppUser
5050 {
5151 Email = dto . Email ,
5252 SecurityStamp = Guid . NewGuid ( ) . ToString ( ) ,
5353 UserName = dto . Username ,
54- AppRoleId = appUserRole . Id
54+ AppRoleId = appUserRole . Id ,
55+ AppRole = appUserRole
5556 } ;
56-
57+
5758 var result = await _userManager . CreateAsync ( user , dto . Password ) ;
5859
5960 if ( ! result . Succeeded )
60- return new BadRequestObjectResult ( "Invalid password lenght Or Bad Email" ) ;
61+ return new BadRequestObjectResult ( "Invalid password length or Bad Email" ) ;
6162
6263 return new OkObjectResult ( new ResultDto { Status = "Success" , Message = "User created successfully!" } ) ;
6364 }
6465
6566 public async Task < IActionResult > Login ( UserLoginDto dto )
6667 {
67- var user = await _userManager . FindByNameAsync ( dto . Username ) ;
68- if ( user == null || ! await _userManager . CheckPasswordAsync ( user , dto . Password ) )
69- {
70- return new UnauthorizedResult ( ) ;
71- }
68+ var user = await _userManager . FindByNameAsync ( dto . Username ) ;
69+ if ( user == null )
70+ return new BadRequestObjectResult ( $ "User with username '{ dto . Username } ' does not exist.") ;
71+
72+ if ( ! await _userManager . CheckPasswordAsync ( user , dto . Password ) )
73+ return new BadRequestObjectResult ( $ "Wrong password.") ;
7274
73- var role = await _roleManager . FindByIdAsync ( user . AppRoleId . ToString ( ) ) ;
74- if ( role == null ) return new BadRequestObjectResult ( $ "Can't login. Role for this user { user . Id } is null ") ;
75+ var role = await _roleManager . FindByNameAsync ( "User" ) ;
76+ if ( role == null ) return new BadRequestObjectResult ( $ "Can't login. Role named 'User' is not found. ") ;
7577
7678 if ( string . IsNullOrEmpty ( user . UserName ) )
7779 {
@@ -92,7 +94,7 @@ public async Task<IActionResult> Login(UserLoginDto dto)
9294 } ;
9395
9496 var token = GetToken ( authClaims ) ;
95-
97+
9698 return new OkObjectResult ( new
9799 {
98100 token = new JwtSecurityTokenHandler ( ) . WriteToken ( token ) ,
@@ -144,9 +146,4 @@ private JwtSecurityToken GetToken(List<Claim> authClaims)
144146
145147 return token ;
146148 }
147-
148- public Task SeedRoles ( )
149- {
150- throw new NotImplementedException ( ) ;
151- }
152149}
0 commit comments