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

181
Views
How can i direct my user to the page i want according to his type?

I believe it's a simple logic, but as I'm still learning I'm having a bit of difficulty implementing it.

On the login page of my project, I get the data of who logged in after a post request. and in the user I have the field "type". There are 2 types of users only.

I want that, if the user has type A, he is redirected to the "home" page, if he is type B, he goes to the "main" page. If you don't have a type value defined yet, go to the "usertype" page.

I wrote this logic here but it's not working. can anybody help me?

heres my code

 async function onFinish(values) {
    let axiosConfig = {
      headers: {
        "Content-Type": "application/json;charset=UTF-8",
        "Access-Control-Allow-Origin": "*",
      },
    };

    Axios.post(
      `  /login`,
      {
        email: values.email,
        senha: values.password,
      },
      axiosConfig
    )
      .then((resp) => {
        var name = resp?.data[0]?.prinom;
        var userId = resp?.data[0]?.id; 
        var prodUser = resp?.data[0];
        localStorage.setItem("prodUser", JSON.stringify(prodUser)); 
        
        
        
        //here's the part i need help
        
        if (!resp?.data[0]?.tipo) history.push("/tipodeusuario");
        resp?.data[0]?.tipo === "pac" ? history.push("/homepac") : history.push("/home");
        
      })
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

After you find the user type (i.e., A or B) you can simply assign a new url to window.location

Here's some pseudo code

function getUserType(AorB){
  //Get user type
  return AorB;
}

//Store urls in a variable
let url = (getUserType('A') == 'A')? './UrlForUserTypeA':'./UrlForUserTypeB';

//Redirect
console.log('Redirecting...');
window.location = url;

Hope that's what you needed

about 4 years ago · Juan Pablo Isaza Report

0

First of all you need to store your response in a variable to have a readable code so :

let apiResp = respoone.data.tipo;

if (apiResp === A){
  history.push('/HomePage')
}if(apiResp === B) {
  history.push('/MainPAge')
}else {
  history.push('/userType')
}

You can also use "Switch Case" Approach.

Or Conditional (ternary) operator which I don't recommend because it makes your code less readable.

about 4 years ago · Juan Pablo Isaza Report

0

useHistory has changed in react-router v6. now we use useNavigate.

if (apiResp === A){
  navigate('/HomePage');
}if(apiResp === B) {
  navigate('/MainPage');
}else {
  navigate('/userType');
}

thanks @Amin Arshadinia :)

about 4 years ago · Juan Pablo Isaza Report
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!