Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions NorthwindCRUD/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,71 @@ public ActionResult<string> Register(RegisterDto userModel)
return StatusCode(500);
}
}

[AllowAnonymous]
[HttpPost("LoginObject")]
public ActionResult<string> LoginObject(LoginDto userModel)
{
try
{
if (ModelState.IsValid)
{
if (this.authService.IsAuthenticated(userModel.Email, userModel.Password))
{
var token = this.authService.GenerateJwtToken(userModel.Email);

return Ok(new { token });
}

return BadRequest("Email or password are not correct!");
}

return BadRequest(ModelState);
}
catch (Exception error)
{
logger.LogError(error.Message);
return StatusCode(500);
}
}

[AllowAnonymous]
[HttpPost("RegisterObject")]
public ActionResult<string> RegisterObject(RegisterDto userModel)
{
try
{
if (ModelState.IsValid)
{
if (userModel.Password != userModel.ConfirmedPassword)
{
return BadRequest("Passwords does not match!");
}

if (this.authService.DoesUserExists(userModel.Email))
{
return BadRequest("User does not exists!");
}

var mappedModel = this.mapper.Map<RegisterDto, UserDb>(userModel);
var user = this.authService.RegisterUser(mappedModel);

if (user != null)
{
var token = this.authService.GenerateJwtToken(user.Email);
return Ok(new { token });
}

return BadRequest("Email or password are not correct!");
}

return BadRequest(ModelState);
}
catch (Exception error)
{
logger.LogError(error.Message);
return StatusCode(500);
}
}
}
}
Loading