Skip to content

Commit

Permalink
Register new 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 7a15bf4 commit 57b7ebc
Show file tree
Hide file tree
Showing 10 changed files with 906 additions and 4 deletions.
43 changes: 43 additions & 0 deletions MoviesApi/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace MoviesApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AccountController : ControllerBase
{
private readonly UserManager<AppUser> _userManager;
public AccountController(UserManager<AppUser> userManager)
{
_userManager = userManager;
}
[HttpPost]
public async Task<IActionResult> RegisterNewUser(dtoNewUser dtouser)
{
if (ModelState.IsValid)
{
//map dto with appuser
AppUser appUser = new()
{
UserName = dtouser.userName,
Email = dtouser.email,
};
IdentityResult result = await _userManager.CreateAsync(appUser, dtouser.password);
if (result.Succeeded)
{
return Ok("Success");
}
else
{
foreach (var item in result.Errors)
{
ModelState.AddModelError("", item.Description);
}
}
}
return BadRequest(ModelState);
}
}
}
16 changes: 16 additions & 0 deletions MoviesApi/Dtos/dtoNewUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace MoviesApi.Dtos
{
public class dtoNewUser
{
[Required]
public string userName { get; set; } = string.Empty;

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

Check warning on line 9 in MoviesApi/Dtos/dtoNewUser.cs

View workflow job for this annotation

GitHub Actions / build

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

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

Check warning on line 12 in MoviesApi/Dtos/dtoNewUser.cs

View workflow job for this annotation

GitHub Actions / build

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

public string? phoneNumber { get; set; }
}
}
Loading

0 comments on commit 57b7ebc

Please sign in to comment.