Add JWT auth, token service & Neutrino endpoint
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
using API.Contracts.Profiles.Responses;
|
||||
using API.Contracts.Profiles.Responses;
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using RecNet.Application.Profiles;
|
||||
using RecNet.Domain.Repositories;
|
||||
using RecNet.Domain.Services.Tokens;
|
||||
using LoginRequest = API.Contracts.Profiles.Requests.LoginRequest;
|
||||
using Profile = RecNet.Domain.Entities.Profiles.Profile;
|
||||
|
||||
@@ -10,9 +12,11 @@ namespace API.Controllers.Profiles.V1;
|
||||
|
||||
[Route("api/[controller]/v1")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class ProfilesController(
|
||||
IProfileRepository profileRepository,
|
||||
IMapper mapper) : ControllerBase
|
||||
IProfileRepository profileRepository,
|
||||
IMapper mapper,
|
||||
ITokenService tokenService) : ControllerBase
|
||||
{
|
||||
[HttpGet("{id:guid}")]
|
||||
public async Task<ActionResult<ProfileDTO>> GetProfile(
|
||||
@@ -22,7 +26,7 @@ public class ProfilesController(
|
||||
var profile = await profileRepository.GetByIdAsync(id, ct);
|
||||
if (profile == null)
|
||||
return NotFound();
|
||||
|
||||
|
||||
return mapper.Map<ProfileDTO>(profile);
|
||||
}
|
||||
|
||||
@@ -32,33 +36,44 @@ public class ProfilesController(
|
||||
CancellationToken ct)
|
||||
{
|
||||
var profiles = await profileRepository.GetByIdsAsync(ids, ct);
|
||||
|
||||
|
||||
return mapper.Map<List<ProfileDTO>>(profiles);
|
||||
}
|
||||
|
||||
// TODO: Implement
|
||||
[AllowAnonymous]
|
||||
[HttpPost("login")]
|
||||
public async Task<ActionResult<LoginResponse>> Login(
|
||||
[FromBody] LoginRequest request,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var name = GenerateRandomName();
|
||||
|
||||
var profile = await profileRepository.GetByPlatform(request.PlatformType, request.PlatformId, ct);
|
||||
if (profile is null)
|
||||
{
|
||||
profile = Profile.Create(request.Username, request.PlatformType, request.PlatformId);
|
||||
profile = Profile.Create(name, request.PlatformType, request.PlatformId);
|
||||
await profileRepository.AddAsync(profile, ct);
|
||||
}
|
||||
|
||||
|
||||
profile.AddDeviceId(request.DeviceId);
|
||||
|
||||
if (request.Username != profile.Name)
|
||||
profile.SetName(request.Username);
|
||||
|
||||
|
||||
await profileRepository.SaveChangesAsync(ct);
|
||||
|
||||
if (profile.IsBanned)
|
||||
return BadRequest();
|
||||
|
||||
var token = tokenService.GenerateProfileToken(profile);
|
||||
|
||||
return new LoginResponse
|
||||
{
|
||||
Profile = mapper.Map<ProfileDTO>(profile)
|
||||
Profile = mapper.Map<ProfileDTO>(profile),
|
||||
AccessToken = token.AccessToken,
|
||||
ExpiresIn = token.ExpiresIn
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: REPLACE WITH BETTER GENERATOR, THIS IS TEMP.
|
||||
private static string GenerateRandomName()
|
||||
=> string.Concat("rr", Guid.NewGuid().ToString("N").AsSpan(0, 18));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user