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:
@ -1,4 +1,8 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using YABA.Common.DTOs;
|
||||
@ -13,12 +17,14 @@ namespace YABA.Service
|
||||
private readonly YABAReadOnlyContext _roContext;
|
||||
private readonly YABAReadWriteContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly ILogger<UserService> _logger;
|
||||
|
||||
public UserService (YABAReadOnlyContext roContext, YABAReadWriteContext context, IMapper mapper)
|
||||
public UserService (YABAReadOnlyContext roContext, YABAReadWriteContext context, IMapper mapper, ILogger<UserService> logger)
|
||||
{
|
||||
_roContext = roContext;
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public bool IsUserRegistered(string authProviderId)
|
||||
@ -28,19 +34,40 @@ namespace YABA.Service
|
||||
|
||||
public async Task<UserDTO> RegisterUser(string authProviderId)
|
||||
{
|
||||
if(IsUserRegistered(authProviderId))
|
||||
try
|
||||
{
|
||||
var user = _roContext.Users.FirstOrDefault(x => x.Auth0Id == authProviderId);
|
||||
return _mapper.Map<UserDTO>(user);
|
||||
if (!IsUserRegistered(authProviderId))
|
||||
{
|
||||
var userToRegister = new User
|
||||
{
|
||||
Auth0Id = authProviderId
|
||||
};
|
||||
|
||||
var registedUser = _context.Users.Add(userToRegister);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if(ex.InnerException is PostgresException &&
|
||||
((PostgresException)ex.InnerException).Code == "23505")
|
||||
{
|
||||
var postgresException = (PostgresException)ex.InnerException;
|
||||
_logger.LogWarning("Swallowing constraint violation: {@ConstraintName} for {@AuthProviderId}", postgresException.ConstraintName, authProviderId);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
var userToRegister = new User
|
||||
{
|
||||
Auth0Id = authProviderId
|
||||
};
|
||||
return await Get(authProviderId);
|
||||
}
|
||||
|
||||
var registedUser = _context.Users.Add(userToRegister);
|
||||
return await _context.SaveChangesAsync() > 0 ? _mapper.Map<UserDTO>(registedUser.Entity) : null;
|
||||
public async Task<UserDTO> Get(string authProviderId)
|
||||
{
|
||||
var user = await _roContext.Users.FirstOrDefaultAsync(x => x.Auth0Id == authProviderId);
|
||||
return _mapper.Map<UserDTO>(user);
|
||||
}
|
||||
|
||||
public int GetUserId(string authProviderId)
|
||||
|
||||
Reference in New Issue
Block a user