[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
|
internal class SessionService
|
||||||
{
|
{
|
||||||
[Inject] public ISyncLocalStorageService LocalStorage { get; }
|
[Inject] public ISyncLocalStorageService LocalStorage { get; }
|
||||||
[Inject] public ApiService ApiService { get; }
|
[Inject] public ApiService ApiService { get; }
|
||||||
public Dictionary<string, StoredUser> Users { get; }
|
public Dictionary<string, StoredUser> Users { get; }
|
||||||
public UserResponse? Current { get; private set; }
|
public StoredUser? Current { get; private set; }
|
||||||
|
|
||||||
public SessionService(ApiService apiService, ISyncLocalStorageService localStorage)
|
public SessionService(ApiService apiService, ISyncLocalStorageService localStorage)
|
||||||
{
|
{
|
||||||
ApiService = apiService;
|
ApiService = apiService;
|
||||||
LocalStorage = localStorage;
|
LocalStorage = localStorage;
|
||||||
Users = LocalStorage.GetItem<Dictionary<string, StoredUser>>("Users") ?? [];
|
Users = LocalStorage.GetItem<Dictionary<string, StoredUser>>("Users") ?? [];
|
||||||
var lastUser = LocalStorage.GetItem<string?>("last_user");
|
var lastUser = LocalStorage.GetItem<string?>("last_user");
|
||||||
if (lastUser != null)
|
if (lastUser != null)
|
||||||
{
|
{
|
||||||
SetSession(lastUser);
|
SetSession(lastUser);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WriteUsers()
|
private void WriteUsers()
|
||||||
{
|
{
|
||||||
LocalStorage.SetItem("Users", Users);
|
LocalStorage.SetItem("Users", Users);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddUser(StoredUser user)
|
public void AddUser(StoredUser user)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Users.Add(user.Id, user);
|
Users.Add(user.Id, user);
|
||||||
}
|
}
|
||||||
catch( ArgumentException ) // Update user if it already exists.
|
catch (ArgumentException) // Update user if it already exists.
|
||||||
{
|
{
|
||||||
Users[user.Id] = user;
|
Users[user.Id] = user;
|
||||||
}
|
}
|
||||||
WriteUsers();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void DeleteUser(string id)
|
WriteUsers();
|
||||||
{
|
}
|
||||||
if (id == Current?.Id) throw new ArgumentException("Cannot remove current user.");
|
|
||||||
Users.Remove(id);
|
|
||||||
WriteUsers();
|
|
||||||
}
|
|
||||||
|
|
||||||
private StoredUser? GetUserById(string id)
|
public void DeleteUser(string id)
|
||||||
{
|
{
|
||||||
var user = Users[id];
|
if (id == Current?.Id) throw new ArgumentException("Cannot remove current user.");
|
||||||
return user;
|
Users.Remove(id);
|
||||||
}
|
WriteUsers();
|
||||||
|
}
|
||||||
|
|
||||||
public void EndSession()
|
private StoredUser? GetUserById(string id)
|
||||||
{
|
{
|
||||||
Current = null;
|
var user = Users[id];
|
||||||
LocalStorage.RemoveItem("last_user");
|
return user;
|
||||||
}
|
}
|
||||||
public void SetSession(string id)
|
|
||||||
{
|
public void EndSession()
|
||||||
var user = GetUserById(id);
|
{
|
||||||
if (user == null) throw new Exception("Did not find User in Local Storage");
|
Current = null;
|
||||||
ApiService.SetBearerToken(user.Token);
|
LocalStorage.RemoveItem("last_user");
|
||||||
Current = user;
|
}
|
||||||
LocalStorage.SetItem("last_user", user.Id);
|
|
||||||
}
|
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