Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda