Skip to content

Commit

Permalink
feat(Login): add back token login
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanngab committed Mar 21, 2024
1 parent 2b56d38 commit 6fa9856
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
2 changes: 2 additions & 0 deletions ui/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import OrganismePage from "./organismes/OrganismePage.jsx";
import Layout from "./common/layout/Layout";
import DesignPage from "./DesignPage.jsx";
import OrganismesPage from "./organismes/OrganismesPage.jsx";
import LoginPage from "./LoginPage.jsx";
import ConnexionPage from "./ConnexionPage.jsx";
import TableauDeBordPage from "./tableau-de-bord/TableauDeBordPage.jsx";
import ValidationPage from "./tableau-de-bord/ValidationPage.jsx";
Expand Down Expand Up @@ -41,6 +42,7 @@ function App() {
<Routes>
<Route element={<Layout children={<Outlet />} />}>
<Route path="/" element={<AccueilPage />} />
<Route path="/login" element={<LoginPage />} />
<Route path="/connexion" element={<ConnexionPage />} />
<Route path="/organismes" element={<OrganismesPage />} />
<Route path="/organismes/:siret">
Expand Down
4 changes: 2 additions & 2 deletions ui/src/ConnexionPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default function Connexion() {
<div
className={usernameError ? "fr-input-group fr-input-group--error" : "fr-input-group"}
>
<label className="fr-label" for="username-1757">
<label className="fr-label" htmlFor="username-1757">
Adresse email
</label>
<input
Expand All @@ -101,7 +101,7 @@ export default function Connexion() {
<div className="fr-password" id="password-1758">
<label
className={passwordError ? "fr-label--error" : "fr-label"}
for="password-1758-input"
htmlFor="password-1758-input"
>
Mot de passe
</label>
Expand Down
29 changes: 29 additions & 0 deletions ui/src/LoginPage.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Navigate, useSearchParams } from "react-router-dom";
import { useContext, useEffect } from "react";
import jwt from "jsonwebtoken";
import { UserContext } from "./common/UserProvider.jsx";

export default function Login() {
const [searchParams] = useSearchParams();
const [_, setUserContext] = useContext(UserContext);

const token = searchParams.get("token");

useEffect(() => {
if (token) {
const decodedToken = jwt.decode(token);
setUserContext({
code: decodedToken.code,
email: decodedToken.email,
nom: decodedToken.nom,
type: decodedToken.type,
token: token,
loading: false,
isAnonymous: false,
});
sessionStorage.setItem("referentiel:token", token);
}
}, [token]);

return <Navigate to={token ? "/tableau-de-bord" : "/organismes"} replace={true} />;
}
4 changes: 2 additions & 2 deletions ui/src/common/ApiProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ export default function ApiProvider({ children }) {
headers: getHeaders(token),
}).then((res) => handleResponse(path, res));
},
_post(path, body) {
_post(path, body, token = null) {
return fetch(`${path}`, {
method: "POST",
headers: getHeaders(),
headers: getHeaders(token),
body: JSON.stringify(body),
}).then((res) => handleResponse(path, res));
},
Expand Down
11 changes: 8 additions & 3 deletions ui/src/common/UserProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ const UserProvider = (props) => {
const [user, setUser] = useState(anonymous);

const verifyUser = useCallback(async () => {
const tokenFromStorage = sessionStorage.getItem("referentiel:token");

const result = await httpClient._post(`/api/v1/users/refreshToken`);
if (result.success) {
const decodedToken = jwt.decode(result.token);

if (result.success || tokenFromStorage) {
const token = result.token || tokenFromStorage;
const decodedToken = jwt.decode(token);

setUser({
code: decodedToken.code,
email: decodedToken.email,
nom: decodedToken.nom,
type: decodedToken.type,
token: result.token,
token: token,
loading: false,
isAnonymous: false,
});
Expand Down

0 comments on commit 6fa9856

Please sign in to comment.