Skip to content

Commit

Permalink
feat: auto logout if wrong version
Browse files Browse the repository at this point in the history
  • Loading branch information
ledouxm committed Dec 7, 2024
1 parent 111119e commit 219d3ec
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/frontend/src/components/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const LoginForm = () => {

const login = async (values: LoginFormProps) => {
const response = await mutation.mutateAsync(values);
localStorage.setItem("crvif/version", "1");
setAuthData({ ...authData, ...response });
};

Expand Down
12 changes: 10 additions & 2 deletions packages/frontend/src/contexts/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { type PropsWithChildren, createContext, useContext, useState } from "react";
import { type PropsWithChildren, createContext, useContext, useEffect, useState } from "react";
import { safeParseLocalStorage } from "../utils";
import { useQuery } from "@tanstack/react-query";
import { electric } from "../db";
import { api, setToken, type RouterOutputs } from "../api";

const initialAuth = safeParseLocalStorage("crvif/auth");
Expand All @@ -17,6 +16,15 @@ const AuthContext = createContext<AuthContextProps>({
export const AuthProvider = ({ children }: PropsWithChildren) => {
const [data, setData] = useState<Omit<AuthContextProps, "setData">>(initialAuth);

const logout = useLogout();

useEffect(() => {
const version = localStorage.getItem("crvif/version");
if (!version) {
return logout();
}
}, []);

const setDataAndSaveInStorage = (data: Omit<AuthContextProps, "setData" | "electricStatus">) => {
setData((d) => ({ ...d, ...data }));
setToken(data?.token);
Expand Down
1 change: 0 additions & 1 deletion packages/frontend/src/features/menu/MenuActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Center, Divider, Stack, styled } from "#styled-system/jsx";
import { css } from "#styled-system/css";
import Button from "@codegouvfr/react-dsfr/Button";
import { useLogout } from "../../contexts/AuthContext";
import { electric } from "../../db";
import { NestedMenu } from "./MenuButton";
import { menuStore } from "./menuStore";
import { Fragment } from "react/jsx-runtime";
Expand Down
1 change: 0 additions & 1 deletion tests/user.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { test, expect, type Route } from "@playwright/test";
import type { RouterOutputs } from "../packages/frontend/src/api";
import { db } from "../packages/backend/src/db/db";

test.beforeEach(async ({ page }) => {
Expand Down

0 comments on commit 219d3ec

Please sign in to comment.