Skip to content

Commit ffe5ffc

Browse files
committed
store logged in state in local storage
1 parent f56dc23 commit ffe5ffc

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

frontend/src/utils/AuthService.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,12 @@ export default class AuthService {
182182
const authState =
183183
AuthService.getCurrentTabAuth() || (await this.getSharedSessionAuth());
184184

185-
if (!this.authState && authState) {
186-
this.saveLogin(authState);
185+
if (authState) {
186+
if (!this.authState) {
187+
this.saveLogin(authState);
188+
}
189+
} else {
190+
AppStateService.updateAuth(null);
187191
}
188192

189193
return authState;
@@ -260,20 +264,15 @@ export default class AuthService {
260264
{ signal },
261265
);
262266

263-
/**
264-
* Assume another tab has logged out if `orgSlug` preference is removed
265-
* See FIXME note in state.ts
266-
*/
267+
// Check for storage event so that the auth token can be removed from
268+
// session storage if any other other tab is logged out; otherwise,
269+
// the token will be refreshed until the original expiration time
267270
window.addEventListener(
268271
"storage",
269272
(e: StorageEvent) => {
270-
if (!this.authState) return;
273+
if (e.key !== `${STORAGE_KEY_PREFIX}.loggedIn`) return;
271274

272-
if (
273-
e.key === `${STORAGE_KEY_PREFIX}.orgSlug` &&
274-
e.oldValue &&
275-
!e.newValue
276-
) {
275+
if (this.authState && e.oldValue === "true") {
277276
this.revoke();
278277
}
279278
},

frontend/src/utils/state.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe("state", () => {
3030
const mockAuth = null;
3131
AppStateService.updateAuth(mockAuth);
3232
expect(AppStateService.appState.auth).to.equal(mockAuth);
33+
expect(AppStateService.appState.loggedIn).to.equal(false);
3334
});
3435

3536
it("updates valid auth", () => {
@@ -40,6 +41,7 @@ describe("state", () => {
4041
};
4142
AppStateService.updateAuth(mockAuth);
4243
expect(AppStateService.appState.auth).to.equal(mockAuth);
44+
expect(AppStateService.appState.loggedIn).to.equal(true);
4345
});
4446

4547
it("does not update invalid auth", () => {

frontend/src/utils/state.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,16 @@ export function makeAppStateService() {
3535
@options(persist(window.localStorage))
3636
userPreferences: UserPreferences | null = null;
3737

38-
// TODO persist here
38+
// TODO persist here instead of in AuthService
3939
auth: Auth | null = null;
4040

41+
// Store logged-in flag in local storage so that tabs know whether to
42+
// check auth again when focused
43+
@options(persist(window.localStorage))
44+
loggedIn: boolean | null = null;
45+
4146
// Store user's org slug preference in local storage in order to redirect
4247
// to the most recently visited org on next log in.
43-
//
44-
// FIXME Since the org slug preference is removed on log out, AuthService
45-
// currently checks whether `orgSlug` is being removed in a `storage`
46-
// event to determine whether another tab has logged out.
47-
// It's not the cleanest solution to use `orgSlug` as a cross-tab logout
48-
// event, so we may want to refactor this in the future.
49-
//
5048
// TODO move to `userPreferences`
5149
@options(persist(window.localStorage))
5250
orgSlug: string | null = null;
@@ -103,11 +101,13 @@ export function makeAppStateService() {
103101
appState.settings = settings;
104102
}
105103

104+
@transaction()
106105
@unlock()
107106
updateAuth(authState: AppState["auth"]) {
108107
authSchema.nullable().parse(authState);
109108

110109
appState.auth = authState;
110+
appState.loggedIn = Boolean(authState);
111111
}
112112

113113
@transaction()
@@ -188,6 +188,7 @@ export function makeAppStateService() {
188188

189189
private _resetUser() {
190190
appState.auth = null;
191+
appState.loggedIn = null;
191192
appState.userInfo = null;
192193
appState.userPreferences = null;
193194
appState.orgSlug = null;

0 commit comments

Comments
 (0)