[frontend] Set cookie on session change

This commit is contained in:
Lilian 2024-04-24 03:52:39 +02:00
parent cc5252ce04
commit 35de6a472c
No known key found for this signature in database
GPG key ID: 007CA12D692829E1
2 changed files with 6 additions and 2 deletions

View file

@ -1,6 +1,7 @@
using Blazored.LocalStorage; using Blazored.LocalStorage;
using Iceshrimp.Frontend.Core.Schemas; using Iceshrimp.Frontend.Core.Schemas;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace Iceshrimp.Frontend.Core.Services; namespace Iceshrimp.Frontend.Core.Services;
@ -8,13 +9,15 @@ internal class SessionService
{ {
[Inject] public ISyncLocalStorageService LocalStorage { get; } [Inject] public ISyncLocalStorageService LocalStorage { get; }
[Inject] public ApiService ApiService { get; } [Inject] public ApiService ApiService { get; }
[Inject] public IJSRuntime Js { get; }
public Dictionary<string, StoredUser> Users { get; } public Dictionary<string, StoredUser> Users { get; }
public StoredUser? Current { get; private set; } public StoredUser? Current { get; private set; }
public SessionService(ApiService apiService, ISyncLocalStorageService localStorage) public SessionService(ApiService apiService, ISyncLocalStorageService localStorage, IJSRuntime js)
{ {
ApiService = apiService; ApiService = apiService;
LocalStorage = localStorage; LocalStorage = localStorage;
Js = js;
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)
@ -68,5 +71,7 @@ internal class SessionService
ApiService.SetBearerToken(user.Token); ApiService.SetBearerToken(user.Token);
Current = user; Current = user;
LocalStorage.SetItem("last_user", user.Id); LocalStorage.SetItem("last_user", user.Id);
((IJSInProcessRuntime)Js).InvokeVoid("eval",
$"document.cookie = \"session={user.Id}; expires=Fri, 31 Dec 9999 23:59:59 GMT; SameSite=Strict\"");
} }
} }

View file

@ -3,7 +3,6 @@
@using Iceshrimp.Frontend.Core.Schemas @using Iceshrimp.Frontend.Core.Schemas
@using Iceshrimp.Frontend.Core.Services @using Iceshrimp.Frontend.Core.Services
@using Iceshrimp.Shared.Schemas @using Iceshrimp.Shared.Schemas
@using Iceshrimp.Frontend.Components
@inject ApiService Api @inject ApiService Api
@inject SessionService SessionService @inject SessionService SessionService
@inject NavigationManager Navigation @inject NavigationManager Navigation