Skip to content

Commit

Permalink
Login user with UserManager
Browse files Browse the repository at this point in the history
  • Loading branch information
YaraGh22-engs committed Jun 10, 2024
1 parent 57b7ebc commit a905879
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
27 changes: 26 additions & 1 deletion MoviesApi/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public AccountController(UserManager<AppUser> userManager)
{
_userManager = userManager;
}
[HttpPost]
[HttpPost("Register")]
public async Task<IActionResult> RegisterNewUser(dtoNewUser dtouser)
{
if (ModelState.IsValid)
Expand All @@ -39,5 +39,30 @@ public async Task<IActionResult> RegisterNewUser(dtoNewUser dtouser)
}
return BadRequest(ModelState);
}

[HttpPost]
public async Task<IActionResult> LogIn(dtoLogin dtolog)
{
if (ModelState.IsValid)
{
AppUser? user = await _userManager.FindByNameAsync(dtolog.userName);
if (user != null)
{
if (await _userManager.CheckPasswordAsync(user, dtolog.password))
{
return Ok("Token");
}
else
{
return Unauthorized();
}
}
else
{
ModelState.AddModelError("", "User Name is invalid");
}
}
return BadRequest(ModelState);
}
}
}
11 changes: 11 additions & 0 deletions MoviesApi/Dtos/dtoLogin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MoviesApi.Dtos
{
public class dtoLogin
{
[Required]
public string userName { get; set; }

Check warning on line 6 in MoviesApi/Dtos/dtoLogin.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'userName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[Required]
public string password { get; set; }
}
}

0 comments on commit a905879

Please sign in to comment.