import React, { useEffect } from "react"; import { useForm } from "react-hook-form"; import * as fetch from 'node-fetch'; import Loading from "../Components/Loading"; import * as BS from "react-bootstrap"; import history from "../History.js"; import { FaLock } from "react-icons/fa"; import API_URL from "../Constants/constants.js"; const Home = ({isInvalid, setIsInvalid, isLoading, setIsLoading, isSubmit, setIsSubmit, }) => { const { register, handleSubmit } = useForm(); const onSubmit = (data) => { localStorage.setItem("token", data.pkToken); logIn(); }; function logIn() { setIsInvalid(false); setIsLoading(true); fetch(`${API_URL}s/`, { method: "GET", headers: { Authorization: JSON.stringify(localStorage.getItem("token")).slice( 1, -1 ), }, }) .then((res) => res.json()) .then((data) => { localStorage.setItem("user", JSON.stringify(data)); setIsSubmit(true); setIsLoading(false); history.push("/pk-webs/dash"); }) .catch((error) => { console.log(error); setIsInvalid(true); localStorage.removeItem("token"); localStorage.removeItem("user"); setIsLoading(false); }); } useEffect(() => { if (localStorage.getItem('token')) { checkLogIn(); } }, []); function checkLogIn() { setIsInvalid(false); setIsLoading(true); fetch(`${API_URL}s/`,{ method: 'GET', headers: { 'Authorization': JSON.stringify(localStorage.getItem("token")).slice(1, -1) }}).then ( res => res.json() ).then (data => { localStorage.setItem('user', JSON.stringify(data)); setIsSubmit(true); setIsLoading(false); }) .catch (error => { console.log(error); setIsInvalid(true); localStorage.removeItem('token'); localStorage.removeItem('user'); setIsLoading(false); }) }; return ( <> {isLoading ? ( ) : ( Login {isSubmit && !localStorage.getItem("user") ? ( Something went wrong, please try again. ) : ( "" )} {isInvalid ? ( Invalid token. ) : ( "" )} {localStorage.getItem("user") && localStorage.getItem("token") ? ( <>

You are logged in already, click here to continue to the dash.

history.push("/pk-webs/dash")} > Continue to dash ) : ( Enter your token here. You can get your token by using{" "} "pk;token". Submit )}
)} ); }; export default Home;