Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
react-client-session Login loop

I'm trying to implement Login Functionality to my app but whenever I do a FRESH Reload and try to redirect to my home page it enters an infinite loop, How could I go about resolving this?.

** Note **

I have refactored this in so many ways with and Without react state in place and many ways of redirecting but zero to no luck, It just loops infinitely in different ways. My current method doesn't generate an error in the console its just the logs of 'mounting' and 'redi' you will read in my code infinitely looping till my CPU dies. Code for reference

// Other Imports. lke axios, useEffect and components from bootstrap.
import { ReactSession } from 'react-client-session';
import { Redirect } from "react-router-dom";

const Login = () => {
  const { handleSubmit, register } = useForm();
  const BASE_URL = process.env.REACT_APP_BASE_URL
  let  [error, setError] = useState(false)
  let  [errorMessage, setErrorMessage] = useState('')
  let [loginToken, setToken] = useState('')


  const login = async (data, e ) => {
   try { let loginData = {
      email: data.email,
      password: data.password,
    }
   let res =  await axios.post(BASE_URL + '/users/admin/login', loginData)
    if (res.status === 200) {
      ReactSession.set('token', res.data.token)
    //  return <Redirect to="/admin/index" />
      setToken(res.data.token)
    }} catch (error) {
      setError(error)
      setErrorMessage("There's been a problem while you were loggin in")
    }
  }

  useEffect(() => {
    const token = ReactSession.get("token");
    setToken(token)
    console.log('mounting')
  }, [loginToken])
  
  if (loginToken){
    console.log('redi')
    return <Redirect to="/admin/meals" />
  }
  return (
// Login Form components.

A copy of my router for reference

// More Imports
import AdminLayout from "layouts/Admin.js";
import AuthLayout from "layouts/Auth.js";



ReactSession.setStoreType("localStorage");
const token = ReactSession.get("token");

ReactDOM.render(
  <BrowserRouter>  
    <Switch>
      <Route path="/auth" render={(props) => <AuthLayout {...props} />} />
      <Route path="/admin" render={(props) => 
        token
          ? <AdminLayout {...props} /> 
          : <Redirect from="*" to="/auth/login" />
        }
      />
      {(token) 
        ?  <Redirect from="/" to="/admin/index" />
        :  <Redirect from="*" to="/auth/login" />
      }
    </Switch>
  </BrowserRouter>,
  document.getElementById("root")
);

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!