import PageForm from "./Form"; import Pages from "./List"; import Prompt from "./Prompt"; import * as React from 'react'; import {Alert, AlertTitle, Button, Divider} from "@mui/material"; import {EncryptStorage} from "storage-encryption"; import { Switch, Route, Link, Redirect, withRouter, useHistory } from "react-router-dom"; interface List { id: string; date: string; } const app = document.getElementById('app'); const word = "shikiryu"; // FIXME should be in db and ≠ between users const sessionPassphrase = sessionStorage.getItem("key"); let pages: List[] = []; let getPageContentUrl, postUrl, removeUrl, checkword, csrf = ""; if (app) { getPageContentUrl = "" + app.getAttribute('data-url'); pages = JSON.parse("" + app.getAttribute('data-list')); postUrl = "" + app.getAttribute('data-post'); removeUrl = "" + app.getAttribute('data-remove'); csrf = "" + app.getAttribute('data-csrf'); checkword = "" + app.getAttribute('data-checkword'); } function App() { const [listPages, setListPages] = React.useState(pages); const [passphrase, setPassphrase] = React.useState(sessionPassphrase); const history = useHistory(); const [user, setUser] = React.useState(checkPassphrase()); const [error, isError] = React.useState(!checkPassphrase()); const signin = cb => { const isAuthenticated = checkPassphrase(); setUser(isAuthenticated); if (isAuthenticated) { isError(false); cb(); } else { isError(true); } }; const signout = cb => { sessionStorage.removeItem("key"); setUser(false); cb(); }; function checkPassphrase() { if (checkword === "" || checkword === null || checkword === "null") { console.error("checkword is empty !"); return false; } localStorage.setItem("checkword", checkword); const key = ""+sessionStorage.getItem("key"); if (key === "" || key === null || key === "null") { console.error("key is empty 🤔 !"); return false; } const encryptStorage = new EncryptStorage(key); const decrypted_word = encryptStorage.decrypt("checkword"); return decrypted_word === word; } const Error = function() { if (error) { return ( Erreur La phrase de passe ne correspond pas. ); } return (
); } const AuthButton = function() { const history = useHistory(); return user ? (

Bienvenue !{" "}

) : (

Votre carnet est fermé.{" "} Voir vos pages

); } function updatePassphrase(newPassphrase) { setPassphrase(newPassphrase); if (checkPassphrase()) { isError(false); setUser(true); signin(() => { history.push({ pathname: "/diary/public/pages" }); }) } else { isError(true); } } function PrivateRoute({ children, ...rest }) { return ( user ? ( children ) : ( ) } /> ); } function PromptPage() { return (); } function ListPage() { return (
); } return (
); } export default withRouter(App);