Add auto-registration mechanism
- Add User registration mechanism when adding auth provider id to claims, if no User Id corresponds to the auth provider Id - Swallow unique constraint violation for unique auth provider id on user table, in case of duplicate requests - Add Serilog logging - Add no bookmarks and no tags message when none is found on Bookmark List page
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
using YABA.API.Settings;
|
||||
using YABA.API.ViewModels;
|
||||
using YABA.Service.Interfaces;
|
||||
|
||||
@ -24,7 +25,7 @@ namespace YABA.API.Controllers
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Obsolete]
|
||||
[DevOnly]
|
||||
[Route("GetWebsiteMetaData")]
|
||||
[ProducesResponseType(typeof(GetWebsiteMetaDataResponse), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
||||
|
||||
42
YABA.API/Controllers/TestController.cs
Normal file
42
YABA.API/Controllers/TestController.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
using YABA.API.Settings;
|
||||
|
||||
namespace YABA.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[ApiVersion("1")]
|
||||
[DevOnly]
|
||||
[Route("api/v{version:apiVersion}/[controller]")]
|
||||
public class TestController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<TestController> _logger;
|
||||
|
||||
public TestController(ILogger<TestController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet("TestLog")]
|
||||
[ProducesResponseType(typeof(string), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
||||
public IActionResult TestLog()
|
||||
{
|
||||
var testObject = new { id = 1, name = "Test Message" };
|
||||
_logger.LogDebug("Testing debug: {@TestObject}", testObject);
|
||||
return Ok(testObject);
|
||||
}
|
||||
|
||||
[HttpGet("TestLogError")]
|
||||
[ProducesResponseType(typeof(string), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
||||
public IActionResult TestLogError()
|
||||
{
|
||||
var testObject = new { id = 1, name = "Test Message" };
|
||||
throw new Exception();
|
||||
return Ok(testObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -24,6 +24,13 @@ namespace YABA.API.Middlewares
|
||||
if (!string.IsNullOrEmpty(userAuthProviderId))
|
||||
{
|
||||
var userId = userService.GetUserId(userAuthProviderId);
|
||||
|
||||
if(userId <= 0)
|
||||
{
|
||||
var registedUser = await userService.RegisterUser(userAuthProviderId);
|
||||
userId = registedUser.Id;
|
||||
}
|
||||
|
||||
httpContext.User.Identities.FirstOrDefault().AddClaim(new Claim(ClaimsLookup.UserId.GetClaimName(), userId.ToString()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,6 +12,7 @@ using YABA.API.Settings.Swashbuckle;
|
||||
using YABA.Data.Configuration;
|
||||
using YABA.Data.Context;
|
||||
using YABA.Service.Configuration;
|
||||
using Serilog;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
var configuration = builder.Configuration;
|
||||
@ -77,6 +78,10 @@ builder.Services.AddSwaggerGen(
|
||||
}
|
||||
);
|
||||
|
||||
// Add Serilog
|
||||
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).CreateLogger();
|
||||
builder.Host.UseSerilog();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Run database migrations
|
||||
|
||||
34
YABA.API/Settings/DevOnlyAttribute.cs
Normal file
34
YABA.API/Settings/DevOnlyAttribute.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
namespace YABA.API.Settings
|
||||
{
|
||||
// Source: https://stackoverflow.com/questions/56495475/asp-net-core-its-possible-to-configure-an-action-in-controller-only-in-developm
|
||||
public class DevOnlyAttribute : Attribute, IFilterFactory
|
||||
{
|
||||
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
|
||||
{
|
||||
return new DevOnlyAttributeImpl(serviceProvider.GetRequiredService<IWebHostEnvironment>());
|
||||
}
|
||||
|
||||
public bool IsReusable => true;
|
||||
|
||||
private class DevOnlyAttributeImpl : Attribute, IAuthorizationFilter
|
||||
{
|
||||
public DevOnlyAttributeImpl(IWebHostEnvironment hostingEnv)
|
||||
{
|
||||
HostingEnv = hostingEnv;
|
||||
}
|
||||
|
||||
private IWebHostEnvironment HostingEnv { get; }
|
||||
|
||||
public void OnAuthorization(AuthorizationFilterContext context)
|
||||
{
|
||||
if (!HostingEnv.IsDevelopment())
|
||||
{
|
||||
context.Result = new NotFoundResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -18,6 +18,10 @@
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.15.1" />
|
||||
<PackageReference Include="Serilog" Version="2.12.0" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.5.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.5.0" />
|
||||
|
||||
@ -6,6 +6,18 @@
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"Serilog": {
|
||||
"MinimumLevel": "Information",
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logs/log-.txt",
|
||||
"rollingInterval": "Day"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"WebClient": {
|
||||
"Url": "https://localhost:3000"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user