feat: add userId field to the authentication response

This commit is contained in:
cuqmbr 2022-07-26 23:11:28 +03:00
parent b314e49442
commit f158bad756
5 changed files with 13 additions and 2 deletions

View File

@ -41,6 +41,7 @@ public class AuthenticationController : ControllerBase
return BadRequest("Username or password is incorrect."); return BadRequest("Username or password is incorrect.");
} }
return Ok(new AuthenticationResponse { Token = content } ); var userId = await _authenticationService.GetIdByUsername(request.Username);
return Ok(new AuthenticationResponse { UserId = userId, Token = content } );
} }
} }

View File

@ -14,7 +14,6 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
builder.Services.AddControllers().AddNewtonsoftJson(o => { builder.Services.AddControllers().AddNewtonsoftJson(o => {
o.SerializerSettings.ContractResolver = new DefaultContractResolver();
o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
}); });
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();

View File

@ -59,6 +59,12 @@ public class AuthenticationService
return (true, GenerateJwtToken(AssembleClaimsIdentity(user))); return (true, GenerateJwtToken(AssembleClaimsIdentity(user)));
} }
public async Task<int> GetIdByUsername(string username)
{
var dbUser = await _dbContext.Users.FirstAsync(u => u.Username == username);
return dbUser.Id;
}
private ClaimsIdentity AssembleClaimsIdentity(User user) private ClaimsIdentity AssembleClaimsIdentity(User user)
{ {
var subject = new ClaimsIdentity(new[] { var subject = new ClaimsIdentity(new[] {

View File

@ -2,5 +2,6 @@ namespace DatabaseModels.Responses;
public class AuthenticationResponse public class AuthenticationResponse
{ {
public int UserId { get; set; }
public string Token { get; set; } = null!; public string Token { get; set; } = null!;
} }

View File

@ -7,4 +7,8 @@
<RootNamespace>DatabaseModels</RootNamespace> <RootNamespace>DatabaseModels</RootNamespace>
</PropertyGroup> </PropertyGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="cp ./bin/Release/net6.0/SharedModels.dll /home/danil/Unity/Projects/untiteled-mobile-game/Assets/SharedModels.dll" />
</Target>
</Project> </Project>