Files
auth-mistox/src/Server/Controllers/MistoxControllerBase.cs
T
derek 0e65ba86a1
Docker Build and Release Upload / build (push) Failing after 1m4s
Handle sessions inside the JWT
2025-07-29 18:04:04 -07:00

76 lines
2.4 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Auth.Entities;
using Auth.Services.DatabaseService;
using System.Security.Claims;
using Auth.Services;
namespace Auth.Controllers {
public class MistoxControllerBase : ControllerBase {
public DatabaseService _databaseService;
public MistoxControllerBase(DatabaseService databaseService) {
_databaseService = databaseService;
}
public static void SignIn(HttpResponse Response, string jwt) {
// Stay logged in cookie
Response.Cookies.Append(AuthJWT.TokenName, jwt, new CookieOptions {
Secure = true,
HttpOnly = true,
SameSite = SameSiteMode.Strict,
Expires = DateTime.UtcNow.AddDays(7)
});
}
public static void SignOut(HttpResponse Response) {
Response.Cookies.Delete(AuthJWT.TokenName);
}
public bool isLoggedIn() {
if (User.Identity != null && User.Identity.IsAuthenticated) {
return true;
}
return false;
}
public int getLoggedInUserID() {
return Convert.ToInt32(User.FindFirstValue(ClaimTypes.NameIdentifier));
}
public async Task<Account> getLoggedInUser() {
try {
Account? test = await _databaseService.GetAccount(getLoggedInUserID());
if (test != null) {
return test;
}
return new Account();
} catch {
return new Account();
}
}
public string Substitue(string message, string subString, string Replacement) {
for (int i = 0; i < (message.Length - subString.Length); i++) {
if (message.Substring(i, subString.Length) == subString) {
string before = message.Substring(0, i);
string after = message.Substring(i + subString.Length);
return before + Replacement + after;
}
}
return message;
}
public bool contains(string outer, string inner) {
if (outer.Length >= inner.Length) {
for (int i = 0; i < outer.Length - inner.Length; i++) {
if (outer.Substring(i, inner.Length) == inner) {
return true;
}
}
}
return false;
}
}
}