Signin and Register now give a token added ps1 script to start servers
This commit is contained in:
@@ -23,7 +23,7 @@ public static class AuthEndpoints
|
||||
});
|
||||
app.MapGet("/users", async (APIAuthService service) =>
|
||||
{
|
||||
return Results.Ok(service.GetUsersAsync());
|
||||
return Results.Ok(await service.GetUsersAsync());
|
||||
});
|
||||
app.MapPost("/user/register", async (AuthRegister request, APIAuthService service, HttpContext context) =>
|
||||
{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Newtonsoft.Json;
|
||||
using RelayCore.Endpoints;
|
||||
using RelayCore.Enums;
|
||||
using RelayCore.Models;
|
||||
using SurrealDb.Net;
|
||||
using SurrealDb.Net.Models;
|
||||
@@ -8,13 +9,18 @@ namespace RelayCore.Services;
|
||||
|
||||
public class APIAuthService(SurrealDbClient _db)
|
||||
{
|
||||
|
||||
public async Task<List<Users>> GetUsersAsync()
|
||||
{
|
||||
var users = await _db.Select<Users>("auth_users");
|
||||
return users.Where(x => x.Username is not null).OrderByDescending(x=>x.CreatedAt).ToList();
|
||||
}
|
||||
public async Task<string?> UserSigninAsync(AuthSignin request, string ip, string userAgent)
|
||||
{
|
||||
var hasher = new PasswordHasher();
|
||||
var users = await _db.Select<Users>("auth_users");
|
||||
var user = users.FirstOrDefault(x => (x.Username == request.UserName || x.Email == request.UserName) );
|
||||
// && hasher.VerifyPassword(request.Password, x.Password));
|
||||
var user = users.FirstOrDefault(x => (x.Username.ToLower() == request.UserName.ToLower() ||
|
||||
x.Email.ToLower() == request.UserName.ToLower()) &&
|
||||
hasher.VerifyPassword(x.Id + request.Password, x.Password));
|
||||
if (user == null)
|
||||
return null;
|
||||
var tokens = await _db.Select<Sessions>("auth_sessions");
|
||||
@@ -41,21 +47,34 @@ public class APIAuthService(SurrealDbClient _db)
|
||||
//TODO: Add invalidation to TOKENs
|
||||
return newToken;
|
||||
}
|
||||
|
||||
public async Task<List<Users>> GetUsersAsync()
|
||||
{
|
||||
var users = await _db.Select<Users>("auth_users");
|
||||
return users.Where(x => x.Username is not null).OrderByDescending(x=>x.CreatedAt).ToList();
|
||||
}
|
||||
|
||||
public async Task<string> UserRegisterAsync(AuthRegister request, string ip, string userAgent)
|
||||
public async Task<string?> UserRegisterAsync(AuthRegister request, string ip, string userAgent)
|
||||
{
|
||||
var hasher = new PasswordHasher();
|
||||
var users = await _db.Select<Users>("auth_users");
|
||||
var user = users.FirstOrDefault(x => x.Username == request.Username || x.Email == request.Username);
|
||||
Console.WriteLine($"Register User found in DB: {user.Username}");
|
||||
var user = users.FirstOrDefault(x => x.Username.ToLower() == request.Username.ToLower() || x.Email.ToLower() == request.Email.ToLower());
|
||||
if (user == null)
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
var created = await _db.Create("auth_users", new Users
|
||||
{
|
||||
Username = request.Username,
|
||||
Email = request.Email,
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now,
|
||||
LastLogin = now,
|
||||
TwoFactorEnabled = false,
|
||||
EmailVerified = false,
|
||||
AccountStatus = (int)AccountStatuses.Active,
|
||||
OnlineStatus = (int)OnlineStatuses.Online,
|
||||
|
||||
});
|
||||
var passwordHash = hasher.HashPassword(created.Id + request.Password);
|
||||
await _db.Merge<PasswordHash, Users>(new PasswordHash
|
||||
{
|
||||
Id = created.Id,
|
||||
Password = passwordHash
|
||||
});
|
||||
|
||||
return await UserSigninAsync(new AuthSignin{UserName=request.Username, Password = request.Password}, ip, userAgent);
|
||||
}
|
||||
|
||||
|
||||
63
start-servers.ps1
Normal file
63
start-servers.ps1
Normal file
@@ -0,0 +1,63 @@
|
||||
$root = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
Set-Location $root
|
||||
|
||||
$dockerExe = (Get-Command docker.exe).Source
|
||||
$dotnetExe = (Get-Command dotnet.exe).Source
|
||||
$ps = (Get-Command powershell.exe).Source
|
||||
|
||||
Write-Host "Building RelayCore..."
|
||||
& $dotnetExe build .\RelayCore\RelayCore.csproj
|
||||
if ($LASTEXITCODE -ne 0) { throw "RelayCore build failed." }
|
||||
|
||||
Write-Host "Building RelayServer..."
|
||||
& $dotnetExe build .\RelayServer\RelayServer.csproj
|
||||
if ($LASTEXITCODE -ne 0) { throw "RelayServer build failed." }
|
||||
|
||||
Write-Host "Building RelayClient (Windows only)..."
|
||||
& $dotnetExe build .\RelayClient\RelayClient.csproj -f net10.0-windows10.0.19041.0
|
||||
if ($LASTEXITCODE -ne 0) { throw "RelayClient build failed." }
|
||||
|
||||
$coreDll = Join-Path $root "RelayCore\bin\Debug\net9.0\RelayCore.dll"
|
||||
$serverDll = Join-Path $root "RelayServer\bin\Debug\net10.0\RelayServer.dll"
|
||||
|
||||
$tempDir = Join-Path $env:TEMP "RelayTabs"
|
||||
New-Item -ItemType Directory -Force -Path $tempDir | Out-Null
|
||||
|
||||
function New-TabScript {
|
||||
param(
|
||||
[string]$Name,
|
||||
[string]$Content
|
||||
)
|
||||
|
||||
$path = Join-Path $tempDir "$Name.ps1"
|
||||
Set-Content -Path $path -Value $Content -Encoding UTF8
|
||||
return $path
|
||||
}
|
||||
|
||||
$dockerScript = New-TabScript -Name "SurrealDB" -Content @"
|
||||
Set-Location '$root'
|
||||
& '$dockerExe' run --rm -p 8000:8000 -v /mydata:/mydata surrealdb/surrealdb:v2.2.1 start --user root --pass secret
|
||||
"@
|
||||
|
||||
$coreScript = New-TabScript -Name "RelayCore" -Content @"
|
||||
Set-Location '$root'
|
||||
Start-Sleep -Seconds 1
|
||||
& '$dotnetExe' '$coreDll'
|
||||
"@
|
||||
|
||||
$serverScript = New-TabScript -Name "RelayServer" -Content @"
|
||||
Set-Location '$root'
|
||||
Start-Sleep -Seconds 1
|
||||
& '$dotnetExe' '$serverDll'
|
||||
"@
|
||||
|
||||
$wtArgs = @(
|
||||
"new-tab --title `"SurrealDB`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$dockerScript`"",
|
||||
"new-tab --title `"RelayCore`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$coreScript`"",
|
||||
"new-tab --title `"RelayServer`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$serverScript`""
|
||||
) -join " ; "
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "Everything started."
|
||||
Write-Host "Close out terminal to end all applications."
|
||||
Start-Process wt.exe -ArgumentList $wtArgs
|
||||
Reference in New Issue
Block a user