Auth endpoints are now functional
This commit is contained in:
@ -17,16 +17,30 @@ using System.ComponentModel.DataAnnotations;
|
||||
using IO.Swagger.Attributes;
|
||||
using IO.Swagger.Security;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using IO.Swagger.Models;
|
||||
using IO.Swagger.Models.dto;
|
||||
using IO.Swagger.Repositories;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq;
|
||||
using IO.Swagger.Services;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace IO.Swagger.Controllers
|
||||
{
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
public class AuthApiController : ControllerBase
|
||||
{
|
||||
{
|
||||
private readonly IUserRepository repository;
|
||||
private readonly JwtService jwt;
|
||||
|
||||
public AuthApiController(IUserRepository repository, JwtService jwt)
|
||||
{
|
||||
this.repository = repository ?? throw new ArgumentNullException(nameof(repository));
|
||||
this.jwt = jwt ?? throw new ArgumentNullException(nameof(jwt));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get user details
|
||||
/// </summary>
|
||||
@ -37,15 +51,13 @@ namespace IO.Swagger.Controllers
|
||||
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
|
||||
[ValidateModelState]
|
||||
[SwaggerOperation("GetUserDetails")]
|
||||
public virtual IActionResult GetUserDetails()
|
||||
{
|
||||
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
|
||||
// return StatusCode(200);
|
||||
|
||||
//TODO: Uncomment the next line to return response 401 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
|
||||
// return StatusCode(401);
|
||||
|
||||
throw new NotImplementedException();
|
||||
public virtual async Task<IActionResult> GetUserDetails()
|
||||
{
|
||||
var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
|
||||
if (!int.TryParse(userIdString, out int userId))
|
||||
return Unauthorized();
|
||||
var user = await repository.RetrieveUser(userId);
|
||||
return user == null ? NoContent() : Ok(user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -58,40 +70,39 @@ namespace IO.Swagger.Controllers
|
||||
[Route("/v1/api/auth/login")]
|
||||
[ValidateModelState]
|
||||
[SwaggerOperation("LoginUser")]
|
||||
public virtual IActionResult LoginUser([FromBody]AuthLoginBody body)
|
||||
{
|
||||
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
|
||||
// return StatusCode(200);
|
||||
|
||||
//TODO: Uncomment the next line to return response 401 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
|
||||
// return StatusCode(401);
|
||||
|
||||
throw new NotImplementedException();
|
||||
public virtual async Task<IActionResult> LoginUser([FromBody]AuthLoginBody body)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
var errors = ModelState.Values.SelectMany(v => v.Errors.Select(e => e.ErrorMessage));
|
||||
return BadRequest(errors);
|
||||
}
|
||||
var user = await repository.LoginUser(body);
|
||||
return user == null ? Unauthorized() : Ok(new { token = jwt.GenerateJwt(user.Id) });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register a new user
|
||||
/// </summary>
|
||||
/// <param name="body"></param>
|
||||
/// <response code="201">User registered successfully</response>
|
||||
/// <response code="200">User registered successfully</response>
|
||||
/// <response code="400">Bad Request</response>
|
||||
/// <response code="409">Conflict (user with provided email already exists)</response>
|
||||
[HttpPost]
|
||||
[Route("/v1/api/auth/register")]
|
||||
[ValidateModelState]
|
||||
|
||||
[SwaggerOperation("RegisterUser")]
|
||||
public virtual IActionResult RegisterUser([FromBody]AuthRegisterBody body)
|
||||
{
|
||||
//TODO: Uncomment the next line to return response 201 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
|
||||
// return StatusCode(201);
|
||||
public async Task<IActionResult> RegisterUser([FromBody]AuthRegisterBody body)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
var errors = ModelState.Values.SelectMany(v => v.Errors.Select(e => e.ErrorMessage));
|
||||
return BadRequest(errors);
|
||||
}
|
||||
|
||||
//TODO: Uncomment the next line to return response 400 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
|
||||
// return StatusCode(400);
|
||||
|
||||
//TODO: Uncomment the next line to return response 409 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
|
||||
// return StatusCode(409);
|
||||
|
||||
throw new NotImplementedException();
|
||||
var user = await repository.RegisterUser(body);
|
||||
return user == null ? StatusCode(409) : Ok(new { token = jwt.GenerateJwt(user.Id) });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,10 +17,10 @@ using System.ComponentModel.DataAnnotations;
|
||||
using IO.Swagger.Attributes;
|
||||
using IO.Swagger.Security;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using IO.Swagger.Models;
|
||||
using IO.Swagger.Models.dto;
|
||||
|
||||
namespace IO.Swagger.Controllers
|
||||
{
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
||||
@ -17,10 +17,10 @@ using System.ComponentModel.DataAnnotations;
|
||||
using IO.Swagger.Attributes;
|
||||
using IO.Swagger.Security;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using IO.Swagger.Models;
|
||||
using IO.Swagger.Models.dto;
|
||||
|
||||
namespace IO.Swagger.Controllers
|
||||
{
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user