[frontend/session] Current should be a StoredUser
This commit is contained in:
parent
adb9267cd2
commit
1391451404
1 changed files with 57 additions and 55 deletions
|
@ -7,65 +7,67 @@ namespace Iceshrimp.Frontend.Core.Services;
|
|||
|
||||
internal class SessionService
|
||||
{
|
||||
[Inject] public ISyncLocalStorageService LocalStorage { get; }
|
||||
[Inject] public ApiService ApiService { get; }
|
||||
public Dictionary<string, StoredUser> Users { get; }
|
||||
public UserResponse? Current { get; private set; }
|
||||
[Inject] public ISyncLocalStorageService LocalStorage { get; }
|
||||
[Inject] public ApiService ApiService { get; }
|
||||
public Dictionary<string, StoredUser> Users { get; }
|
||||
public StoredUser? Current { get; private set; }
|
||||
|
||||
public SessionService(ApiService apiService, ISyncLocalStorageService localStorage)
|
||||
{
|
||||
ApiService = apiService;
|
||||
LocalStorage = localStorage;
|
||||
Users = LocalStorage.GetItem<Dictionary<string, StoredUser>>("Users") ?? [];
|
||||
var lastUser = LocalStorage.GetItem<string?>("last_user");
|
||||
if (lastUser != null)
|
||||
{
|
||||
SetSession(lastUser);
|
||||
}
|
||||
}
|
||||
public SessionService(ApiService apiService, ISyncLocalStorageService localStorage)
|
||||
{
|
||||
ApiService = apiService;
|
||||
LocalStorage = localStorage;
|
||||
Users = LocalStorage.GetItem<Dictionary<string, StoredUser>>("Users") ?? [];
|
||||
var lastUser = LocalStorage.GetItem<string?>("last_user");
|
||||
if (lastUser != null)
|
||||
{
|
||||
SetSession(lastUser);
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteUsers()
|
||||
{
|
||||
LocalStorage.SetItem("Users", Users);
|
||||
}
|
||||
private void WriteUsers()
|
||||
{
|
||||
LocalStorage.SetItem("Users", Users);
|
||||
}
|
||||
|
||||
public void AddUser(StoredUser user)
|
||||
{
|
||||
try
|
||||
{
|
||||
Users.Add(user.Id, user);
|
||||
}
|
||||
catch( ArgumentException ) // Update user if it already exists.
|
||||
{
|
||||
Users[user.Id] = user;
|
||||
}
|
||||
WriteUsers();
|
||||
}
|
||||
public void AddUser(StoredUser user)
|
||||
{
|
||||
try
|
||||
{
|
||||
Users.Add(user.Id, user);
|
||||
}
|
||||
catch (ArgumentException) // Update user if it already exists.
|
||||
{
|
||||
Users[user.Id] = user;
|
||||
}
|
||||
|
||||
public void DeleteUser(string id)
|
||||
{
|
||||
if (id == Current?.Id) throw new ArgumentException("Cannot remove current user.");
|
||||
Users.Remove(id);
|
||||
WriteUsers();
|
||||
}
|
||||
WriteUsers();
|
||||
}
|
||||
|
||||
private StoredUser? GetUserById(string id)
|
||||
{
|
||||
var user = Users[id];
|
||||
return user;
|
||||
}
|
||||
public void DeleteUser(string id)
|
||||
{
|
||||
if (id == Current?.Id) throw new ArgumentException("Cannot remove current user.");
|
||||
Users.Remove(id);
|
||||
WriteUsers();
|
||||
}
|
||||
|
||||
public void EndSession()
|
||||
{
|
||||
Current = null;
|
||||
LocalStorage.RemoveItem("last_user");
|
||||
}
|
||||
public void SetSession(string id)
|
||||
{
|
||||
var user = GetUserById(id);
|
||||
if (user == null) throw new Exception("Did not find User in Local Storage");
|
||||
ApiService.SetBearerToken(user.Token);
|
||||
Current = user;
|
||||
LocalStorage.SetItem("last_user", user.Id);
|
||||
}
|
||||
private StoredUser? GetUserById(string id)
|
||||
{
|
||||
var user = Users[id];
|
||||
return user;
|
||||
}
|
||||
|
||||
public void EndSession()
|
||||
{
|
||||
Current = null;
|
||||
LocalStorage.RemoveItem("last_user");
|
||||
}
|
||||
|
||||
public void SetSession(string id)
|
||||
{
|
||||
var user = GetUserById(id);
|
||||
if (user == null) throw new Exception("Did not find User in Local Storage");
|
||||
ApiService.SetBearerToken(user.Token);
|
||||
Current = user;
|
||||
LocalStorage.SetItem("last_user", user.Id);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue