mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-08-16 21:31:45 +02:00
.config
.run
.workdir
Agent
Common
Controller
Docker
Utils
Web
Phantom.Web
Phantom.Web.Bootstrap
Phantom.Web.Components
Phantom.Web.Services
Agents
Authentication
AuthenticationStateExtensions.cs
CustomAuthenticationStateProvider.cs
CustomClaimsPrincipal.cs
UserLoginManager.cs
UserSessionBrowserStorage.cs
Authorization
Events
Instances
Rpc
Users
ApplicationProperties.cs
Navigation.cs
Phantom.Web.Services.csproj
PhantomWebServices.cs
.dockerignore
.gitattributes
.gitignore
AddMigration.bat
AddMigration.sh
Directory.Build.props
Directory.Build.targets
Dockerfile
LICENSE
Packages.props
PhantomPanel.sln
README.md
global.json
58 lines
2.0 KiB
C#
58 lines
2.0 KiB
C#
using Phantom.Common.Data.Web.Users;
|
|
using Phantom.Common.Messages.Web.ToController;
|
|
using Phantom.Utils.Logging;
|
|
using Phantom.Web.Services.Rpc;
|
|
using ILogger = Serilog.ILogger;
|
|
|
|
namespace Phantom.Web.Services.Authentication;
|
|
|
|
public sealed class UserLoginManager {
|
|
private static readonly ILogger Logger = PhantomLogger.Create<UserLoginManager>();
|
|
|
|
private readonly Navigation navigation;
|
|
private readonly UserSessionBrowserStorage sessionBrowserStorage;
|
|
private readonly CustomAuthenticationStateProvider authenticationStateProvider;
|
|
private readonly ControllerConnection controllerConnection;
|
|
|
|
public UserLoginManager(Navigation navigation, UserSessionBrowserStorage sessionBrowserStorage, CustomAuthenticationStateProvider authenticationStateProvider, ControllerConnection controllerConnection) {
|
|
this.navigation = navigation;
|
|
this.sessionBrowserStorage = sessionBrowserStorage;
|
|
this.authenticationStateProvider = authenticationStateProvider;
|
|
this.controllerConnection = controllerConnection;
|
|
}
|
|
|
|
public async Task<bool> LogIn(string username, string password, string? returnUrl = null) {
|
|
LogInSuccess? success;
|
|
try {
|
|
success = await controllerConnection.Send<LogInMessage, LogInSuccess?>(new LogInMessage(username, password), TimeSpan.FromSeconds(30));
|
|
} catch (Exception e) {
|
|
Logger.Error(e, "Could not log in {Username}.", username);
|
|
return false;
|
|
}
|
|
|
|
if (success == null) {
|
|
return false;
|
|
}
|
|
|
|
Logger.Information("Successfully logged in {Username}.", username);
|
|
|
|
var userInfo = success.UserInfo;
|
|
|
|
await sessionBrowserStorage.Store(userInfo.Guid, success.Token);
|
|
authenticationStateProvider.SetLoadedSession(userInfo);
|
|
await navigation.NavigateTo(returnUrl ?? string.Empty);
|
|
|
|
return true;
|
|
}
|
|
|
|
public async Task LogOut() {
|
|
var stored = await sessionBrowserStorage.Delete();
|
|
if (stored != null) {
|
|
await controllerConnection.Send(new LogOutMessage(stored.UserGuid, stored.Token));
|
|
}
|
|
|
|
await navigation.NavigateTo(string.Empty);
|
|
authenticationStateProvider.SetUnloadedSession();
|
|
}
|
|
}
|