Added endpoint for registering a user locally after successful logon from Auth0

This commit is contained in:
Carl Tibule
2023-01-25 22:47:05 -06:00
parent e0b38beff6
commit 406bd9a0d1
16 changed files with 267 additions and 47 deletions

View File

@ -0,0 +1,40 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;
using YABA.API.Extensions;
using YABA.API.ViewModels;
using YABA.Service.Interfaces;
namespace YABA.API.Controllers
{
[ApiVersion("1")]
[Authorize, Route("api/v{version:apiVersion}/[controller]")]
public class UsersController : ControllerBase
{
private readonly IUserService _userService;
public UsersController(IUserService userService)
{
_userService = userService;
}
[HttpPost("Register")]
[ProducesResponseType(typeof(UserResponse), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
[ProducesResponseType((int)HttpStatusCode.NoContent)]
public IActionResult Register()
{
var authProviderId = this.GetAuthProviderId();
if (string.IsNullOrEmpty(authProviderId)) return NotFound();
var isRegistered = _userService.IsUserRegistered(authProviderId);
if (isRegistered) return NoContent();
var registedUser = _userService.RegisterUser(authProviderId);
return Ok(new UserResponse(registedUser));
}
}
}

View File

@ -1,35 +0,0 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace YABA.API.Controllers
{
[ApiController]
[ApiVersion("1")]
[Authorize, Route("api/v{version:apiVersion}/[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}

View File

@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using YABA.Common.Extensions;
using YABA.Common.Lookups;
namespace YABA.API.Extensions
{
public static class ControllerExtensions
{
public static string GetAuthProviderId(this ControllerBase controller)
{
return GetCustomClaim(controller, ClaimsLookup.AuthProviderId);
}
public static int GetUserId(this ControllerBase controller)
{
var isValidUserId = int.TryParse(GetCustomClaim(controller, ClaimsLookup.UserId), out int userId);
return isValidUserId ? userId : 0;
}
public static string GetCustomClaim(this ControllerBase controller, ClaimsLookup claim)
{
var claimsIdentity = controller.User.Identity as ClaimsIdentity;
return claimsIdentity.FindFirst(claim.GetClaimName())?.Value.ToString();
}
public static string GetIpAddress(this ControllerBase controller)
{
if (controller.Request.Headers.ContainsKey("X-Forwarded-For"))
return controller.Request.Headers["X-Forwarded-For"];
return controller.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
}
}
}

View File

@ -0,0 +1,20 @@
using YABA.Service.DTO;
namespace YABA.API.ViewModels
{
public class UserResponse
{
public int Id { get; set; }
public bool IsDeleted { get; set; }
public DateTimeOffset CreatedOn { get; set; }
public DateTimeOffset LastModified { get; set; }
public UserResponse(UserDTO value)
{
Id = value.Id;
IsDeleted = value.IsDeleted;
CreatedOn = value.CreatedOn;
LastModified = value.LastModified;
}
}
}

View File

@ -23,6 +23,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\YABA.Common\YABA.Common.csproj" />
<ProjectReference Include="..\YABA.Data\YABA.Data.csproj" />
<ProjectReference Include="..\YABA.Service\YABA.Service.csproj" />
</ItemGroup>