Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions frontend/src/ts/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
} from "./firebase";
import { createSignalWithSetters } from "./hooks/createSignalWithSetters";
import { createEffectOn } from "./hooks/effects";
import { lastAuthenticationState } from "./legacy-states/last-authentication";
import * as Sentry from "./sentry";
import { getUserId, isAuthenticated, setUserId } from "./states/core";
import { hideLoaderBar, showLoaderBar } from "./states/loader-bar";
Expand Down Expand Up @@ -229,6 +230,15 @@ export async function onAuthStateChanged(
} else {
setUserId(null);
DB.setSnapshot(undefined);

const lastState = lastAuthenticationState.get();
if (lastState.isLoggedIn && Date.now() - lastState.timestamp > 5_000) {
showNoticeNotification("You got logged out.");
lastAuthenticationState.set({
isLoggedIn: false,
timestamp: Date.now(),
});
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion frontend/src/ts/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { tryCatch } from "@monkeytype/util/trycatch";
import { googleSignUpEvent } from "./events/google-sign-up";
import { addBanner } from "./states/banners";
import { setUserId, setUserVerified } from "./states/core";
import { lastAuthenticationState } from "./legacy-states/last-authentication";

let app: FirebaseApp | undefined;
let Auth: AuthType | undefined;
Expand Down Expand Up @@ -110,6 +111,10 @@ export function isAuthAvailable(): boolean {
export async function signOut(): Promise<void> {
console.log("auth signout");
await Auth?.signOut();
lastAuthenticationState.set({
isLoggedIn: false,
timestamp: Date.now(),
});
}

export async function signInWithEmailAndPassword(
Expand All @@ -130,7 +135,6 @@ export async function signInWithEmailAndPassword(
"Failed to sign in with email and password",
);
}

return result;
}

Expand All @@ -146,6 +150,10 @@ export function setUserState(
} else {
setUserId(options.uid);
setUserVerified(options.emailVerified);
lastAuthenticationState.set({
isLoggedIn: true,
timestamp: Date.now(),
});
}
}

Expand Down
16 changes: 16 additions & 0 deletions frontend/src/ts/legacy-states/last-authentication.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { z } from "zod";
import { LocalStorageWithSchema } from "../utils/local-storage-with-schema";

const LastAuthenticationStateSchema = z.object({
isLoggedIn: z.boolean(),
timestamp: z.number().safe().nonnegative(),
});
export type LastAuthenticationState = z.infer<
typeof LastAuthenticationStateSchema
>;

export const lastAuthenticationState = new LocalStorageWithSchema({
key: "lastAuthenticationState",
schema: LastAuthenticationStateSchema,
fallback: { isLoggedIn: false, timestamp: Date.now() },
});
Loading