mirror of
https://git.lewd.wtf/PGR/ascnet
synced 2025-12-12 19:24:36 +01:00
api for sdk
This commit is contained in:
@@ -1,10 +1,80 @@
|
||||
namespace AscNet.SDKServer.Controllers
|
||||
using System.Text;
|
||||
using AscNet.Common.Database;
|
||||
using AscNet.SDKServer.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace AscNet.SDKServer.Controllers
|
||||
{
|
||||
public class AccountController : IRegisterable
|
||||
{
|
||||
public static void Register(WebApplication app)
|
||||
{
|
||||
app.MapPost("/api/AscNet/register", (HttpContext ctx) =>
|
||||
{
|
||||
AuthRequest? req = JsonConvert.DeserializeObject<AuthRequest>(Encoding.UTF8.GetString(ctx.Request.BodyReader.ReadAsync().Result.Buffer));
|
||||
|
||||
if (req is null)
|
||||
{
|
||||
return JsonConvert.SerializeObject(new
|
||||
{
|
||||
code = -1,
|
||||
msg = "Invalid request"
|
||||
});
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Account account = Account.Create(req.Username, req.Password);
|
||||
|
||||
return JsonConvert.SerializeObject(new
|
||||
{
|
||||
code = 0,
|
||||
msg = "OK",
|
||||
account
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return JsonConvert.SerializeObject(new
|
||||
{
|
||||
code = -1,
|
||||
msg = ex.Message
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
app.MapPost("/api/AscNet/login", (HttpContext ctx) =>
|
||||
{
|
||||
AuthRequest? req = JsonConvert.DeserializeObject<AuthRequest>(Encoding.UTF8.GetString(ctx.Request.BodyReader.ReadAsync().Result.Buffer));
|
||||
|
||||
if (req is null)
|
||||
{
|
||||
return JsonConvert.SerializeObject(new
|
||||
{
|
||||
code = -1,
|
||||
msg = "Invalid request"
|
||||
});
|
||||
}
|
||||
|
||||
Account? account = Account.FromUsername(req.Username, req.Password);
|
||||
|
||||
if (account == null)
|
||||
{
|
||||
return JsonConvert.SerializeObject(new
|
||||
{
|
||||
code = -1,
|
||||
msg = "Invalid credentials!"
|
||||
});
|
||||
}
|
||||
|
||||
return JsonConvert.SerializeObject(new
|
||||
{
|
||||
code = 0,
|
||||
msg = "OK",
|
||||
account
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using AscNet.Common.Util;
|
||||
using AscNet.SDKServer.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
|
||||
14
AscNet.SDKServer/Models/AscNetAuth.cs
Normal file
14
AscNet.SDKServer/Models/AscNetAuth.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
namespace AscNet.SDKServer.Models
|
||||
{
|
||||
public class AuthRequest
|
||||
{
|
||||
[JsonProperty("username", NullValueHandling = NullValueHandling.Ignore)]
|
||||
public string Username { get; set; }
|
||||
|
||||
[JsonProperty("password", NullValueHandling = NullValueHandling.Ignore)]
|
||||
public string Password { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user