frontend
This commit is contained in:
@ -43,7 +43,7 @@ namespace Yuna.Website.Server.API
|
||||
var hashedPassword = Encrypter.HashPassword(request.RawPassword, request.UserName);
|
||||
if (!hashedPassword.Equals(userFromDb.HashedPassword)) return Results.Unauthorized();
|
||||
await SetAccessToken(context, tokenService, userFromDb);
|
||||
return Results.Ok();
|
||||
return Results.Ok("");
|
||||
}
|
||||
|
||||
|
||||
@ -54,6 +54,11 @@ namespace Yuna.Website.Server.API
|
||||
await context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
|
||||
}
|
||||
|
||||
private static void SetFrontendCookie(User user, HttpContext context)
|
||||
{
|
||||
context.SaveToCookies("user", new CookieUserModel(user));
|
||||
}
|
||||
|
||||
public class RegisterUserRequest
|
||||
{
|
||||
public string username { get; set; } = null!;
|
||||
|
@ -4,6 +4,8 @@ using System.Text.Json.Serialization;
|
||||
using Yuna.Website.Server.Services.DeviceSkillService;
|
||||
using Yuna.Website.Server.Services.DeviceService;
|
||||
using Yuna.Website.Server.Model;
|
||||
using System.Security.Claims;
|
||||
using Yuna.Website.Server.Infrastructure;
|
||||
|
||||
namespace Yuna.Website.Server.API
|
||||
{
|
||||
@ -47,13 +49,18 @@ namespace Yuna.Website.Server.API
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
public async Task<IResult> CreateDevice([FromBody] CreateDeviceResult request, IDeviceService deviceService)
|
||||
public async Task<IResult> CreateDevice([FromBody] CreateDeviceResult request, IDeviceService deviceService, HttpContext context)
|
||||
{
|
||||
|
||||
var userId = context.GetUserIdFromCookie();
|
||||
if (userId is null) return Results.Unauthorized();
|
||||
|
||||
var device = new Device()
|
||||
{
|
||||
Description = request.Description,
|
||||
DeviceUrl = request.DeviceUrl,
|
||||
Name = request.Name
|
||||
Name = request.Name,
|
||||
UserId = (long)userId
|
||||
};
|
||||
|
||||
var result = await deviceService.Create(device);
|
||||
@ -74,9 +81,20 @@ namespace Yuna.Website.Server.API
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
public async Task<IResult> GetAll(IDeviceService deviceService)
|
||||
public async Task<IResult> GetAll(IDeviceService deviceService, HttpContext context)
|
||||
{
|
||||
var result = await deviceService.GetList();
|
||||
|
||||
var isAdmin = context.GetRoleFromCookie();
|
||||
if(isAdmin)
|
||||
{
|
||||
var adminResult = await deviceService.GetList();
|
||||
return Results.Ok(adminResult);
|
||||
}
|
||||
|
||||
var userId = context.GetUserIdFromCookie();
|
||||
if (userId is null) return Results.Unauthorized();
|
||||
|
||||
var result = await deviceService.GetList((long)userId);
|
||||
return Results.Ok(result);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user