React redirects to the login page after entering username and password instead of redirecting to the required page

huangapple go评论72阅读模式
英文:

React redirects to the login page after entering username and password instead of redirecting to the required page

问题

I'm creating a simple react application with 3 user roles. On the login page localhost:3000/login I have fields for username and password. Once those details are entered and the login button is clicked, the data is sent to the backend (running node js) and that data is used to query a MySql database. If the entered data matches a user in the database, the userId, name, password, and role is sent to the backend. This data is then sent to the front end. I can read the retrieved data from the front end and up to this point it works fine. However, when I'm trying to redirect a user according to the role, say the role is doctor and I want to redirect the user to localhost:3000/doctor, it goes to localhost:3000/doctor momentarily and switches to localhost:3000/login?. Shown below is the code for the login component.

import { useState } from "react";
import Axios from 'axios';
import { useNavigate } from 'react-router-dom';
import './login.css';

const Login = () => {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');

  let navigate = useNavigate();

  const handleLogin = () => {
    Axios.post("http://localhost:3001/login",
      {
        email: email,
        password: password,
      },
      {
        headers: {
          'Content-Type': 'application/json'
        }
      }
    )
      .then((response) => {
        console.log('response 1', response.data[0]['role']);
        if (response.data[0]['role'] === 'doctor') {
          navigate('/doctor');
        }
      });
  };

  return (
    <div>
      <form>
        <h3>Electronic Prescription System</h3>
        <h3>Login</h3>

        <label>Email Address</label>
        <input
          className="inputs"
          type="text"
          placeholder="email"
          onChange={(e) => {
            setEmail(e.target.value)
          }}
        />
        <label>Password</label>
        <input
          className="inputs"
          type="password"
          placeholder="password"
          onChange={(e) => setPassword(e.target.value)}
        />
        <button onClick={handleLogin}>Log in</button>
      </form>
    </div>
  )
};

export default Login;

If I remove all the code inside the handleLogin function and just have navigate('/doctor'); it redirects properly.

The routes are inside the Main component as shown below.

import React from 'react';
import { Routes, Route } from 'react-router-dom';

import Login from "./pages/Login/Login";
import Doctor from "./pages/Doctor/Doctor";
import Patient from "./pages/Patient/Patient";
import Pharmacy from "./pages/Pharmacy/Pharmacy";

const Main = () => {
  return (
    <Routes>
      <Route path="login" element={<Login />} />
      <Route path="doctor" element={<Doctor />} />
      <Route path="patient" element={<Patient />} />
      <Route path="pharmacy" element={<Pharmacy />} />
    </Routes>
  );
}

export default Main;

The Doctor Component:

import { HeaderPanel } from '../../components/headerPanel/headerPanel';
import { PrescribePanel } from '../../components/prescribePanel/prescribePanel';
import { PrescriptionsList } from '../../components/prescriptionsList/prescriptionsList';
import './styles.css';

export const Doctor = () => {
  return (
    <>
      <HeaderPanel />
      <div className='wrapper'>
        <PrescribePanel />
        <PrescriptionsList />
      </div>
    </> 
  );
}

export default Doctor;

I'm using react-router-dom version 6.6.1 and the react version is 18.2.0.

Tried using a useEffect hook to capture the role changing and redirecting, but it did not work either.

英文:

I'm creating a simple react application with 3 user roles. On the login page localhost:3000/login I have fields for username and password. Once those details are entered and the login button is clicked, the data is sent to the backend (running node js) and that data is used to query a MySql database. If the entered data matches a user in the database, the userId, name, password, and role is sent to the backend. This data is then sent to the front end. I can read the retrieved data from the front end and up to this point it works fine. However, when I'm trying to redirect a user according to the role, say the role is doctor and I want to redirect the user to localhost:3000/doctor , it goes to localhost:3000/doctor momentarily and switches to localhost:3000/login?. Shown below is the code for the login component.

import { useState } from &quot;react&quot;;
import Axios from &#39;axios&#39;;
import { useNavigate } from &#39;react-router-dom&#39;
import &#39;./login.css&#39;;
const Login = () =&gt; {
const [email, setEmail] = useState(&#39;&#39;);
const [password, setPassword] = useState(&#39;&#39;);
let navigate = useNavigate()
const handleLogin = () =&gt; {
Axios.post(&quot;http://localhost:3001/login&quot;, 
{
email: email,
password: password,
},
{
headers: {
&#39;Content-Type&#39;: &#39;application/json&#39;
}
}
)
.then((response) =&gt; {
console.log(&#39;response 1&#39;, response.data[0][&#39;role&#39;])
if (response.data[0][&#39;role&#39;] === &#39;doctor&#39;) {
navigate(&#39;/doctor&#39;);
}
});
};
return (
&lt;div&gt;
&lt;form&gt;
&lt;h3&gt;Electronic Prescription System&lt;/h3&gt;
&lt;h3&gt;Login&lt;/h3&gt;
&lt;label&gt;Email Address&lt;/label&gt;
&lt;input
className=&quot;inputs&quot;
type=&quot;text&quot;
placeholder=&quot;email&quot;
onChange={(e) =&gt; {
setEmail(e.target.value)
}}
/&gt;
&lt;label&gt;Password&lt;/label&gt;
&lt;input
className=&quot;inputs&quot;
type=&quot;password&quot;
placeholder=&quot;password&quot;
onChange={(e) =&gt; setPassword(e.target.value)}
/&gt;
&lt;button onClick={handleLogin}&gt;Log in&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;
)
};
export default Login;

If I remove all the code inside the handleLogin function and just have navigate(&#39;/doctor&#39;); it redirects properly.

The routes are inside the Main component as shown below.

import React from &#39;react&#39;;
import { Routes, Route } from &#39;react-router-dom&#39;;
import Login from &quot;./pages/Login/Login&quot;;
import Doctor from &quot;./pages/Doctor/Doctor&quot;;
import Patient from &quot;./pages/Patient/Patient&quot;;
import Pharmacy from &quot;./pages/Pharmacy/Pharmacy&quot;;
const Main = () =&gt; {
return (
&lt;Routes&gt;
&lt;Route path=&quot;login&quot; element={&lt;Login /&gt;} /&gt;
&lt;Route path=&quot;doctor&quot; element={&lt;Doctor /&gt;} /&gt;
&lt;Route path=&quot;patient&quot; element={&lt;Patient /&gt;} /&gt;
&lt;Route path=&quot;pharmacy&quot; element={&lt;Pharmacy /&gt;} /&gt;
&lt;/Routes&gt;
);
}
export default Main;

The Doctor Component:

import { HeaderPanel } from &#39;../../components/headerPanel/headerPanel&#39;
import { PrescribePanel } from &#39;../../components/prescribePanel/prescribePanel&#39;
import { PrescriptionsList } from &#39;../../components/prescriptionsList/prescriptionsList&#39;
import &#39;./styles.css&#39;;

export const Doctor = () =&gt; {
  return (
    &lt;&gt;
      &lt;HeaderPanel /&gt;
      &lt;div className=&#39;wrapper&#39;&gt;
        &lt;PrescribePanel /&gt;
        &lt;PrescriptionsList /&gt;
      &lt;/div&gt;
    &lt;/&gt; 
  );
}

export default Doctor

I'm using react-router-dom version 6.6.1 and the react version is 18.2.0.

Tried using a useEffect hook to capture the role changing and redirecting, but id did not work either.

答案1

得分: 1

我怀疑的情况是,“登录”按钮正在提交表单,该表单采用默认的表单操作并重新加载页面,当前路由路径为“/login”。默认情况下,button 元素的 type="submit" 属性值。

要解决这个问题,我建议将 handleLogin 附加到 form 元素的 onSubmit 事件处理程序,并在 onSubmit 事件对象上调用 preventDefault 以防止提交表单和重新加载页面。这应该允许身份验证逻辑按预期完成。

请养成具体指定 button 元素的 type 属性的习惯。

const Login = () => {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');

  const navigate = useNavigate();

  const handleLogin = (e) => { // <-- onSubmit 事件对象
    e.preventDefault();        // <-- 不执行表单操作

    Axios.post(
      "http://localhost:3001/login", 
      { email, password },
      {
        headers: {
          'Content-Type': 'application/json'
        }
      }
    )
      .then((response) => {
        if (response.data[0]['role'] === 'doctor') {
          navigate('/doctor');
        }
      });
  };

  return (
    <div>
      <form onSubmit={handleLogin}> // <-- form onSubmit 事件处理程序
        <h3>电子处方系统</h3>
        <h3>登录</h3>

        <label>
          电子邮件地址
          <input
            className="inputs"
            type="text"
            placeholder="email"
            onChange={(e) => setEmail(e.target.value)}
          />
        </label>
        <label>
          密码
          <input
            className="inputs"
            type="password"
            placeholder="password"
            onChange={(e) => setPassword(e.target.value)}
          />
        </label>

        <button type="submit"> // <-- 具体指定按钮类型
          登录
        </button>
      </form>
    </div>
  );
};

export default Login;

注意:以上是你要翻译的内容。

英文:

What I suspect is happening here is that the "log in" button is submitting the form which takes the default form action and reloads the page, the current route path being &quot;/login&quot;. button elements have a type=&quot;submit&quot; attribute value by default.

To resolve I'd suggest attaching handleLogin to the form element's onSubmit event handler and calling preventDefault on the onSubmit event object to prevent submitting the form and prevent reloading the page. This should allow the authentication logic to complete as expected.

Try to get yourself in the habit of being specific with the button element's type attribute.

const Login = () =&gt; {
const [email, setEmail] = useState(&#39;&#39;);
const [password, setPassword] = useState(&#39;&#39;);
const navigate = useNavigate();
const handleLogin = (e) =&gt; { // &lt;-- onSubmit event object
e.preventDefault();        // &lt;-- don&#39;t take form action
Axios.post(
&quot;http://localhost:3001/login&quot;, 
{ email, password },
{
headers: {
&#39;Content-Type&#39;: &#39;application/json&#39;
}
}
)
.then((response) =&gt; {
if (response.data[0][&#39;role&#39;] === &#39;doctor&#39;) {
navigate(&#39;/doctor&#39;);
}
});
};
return (
&lt;div&gt;
&lt;form onSubmit={handleLogin}&gt; // &lt;-- form onSubmit event handler
&lt;h3&gt;Electronic Prescription System&lt;/h3&gt;
&lt;h3&gt;Login&lt;/h3&gt;
&lt;label&gt;
Email Address
&lt;input
className=&quot;inputs&quot;
type=&quot;text&quot;
placeholder=&quot;email&quot;
onChange={(e) =&gt; setEmail(e.target.value)}
/&gt;
&lt;/label&gt;
&lt;label&gt;
Password
&lt;input
className=&quot;inputs&quot;
type=&quot;password&quot;
placeholder=&quot;password&quot;
onChange={(e) =&gt; setPassword(e.target.value)}
/&gt;
&lt;/label&gt;
&lt;button type=&quot;submit&quot;&gt; // &lt;-- be specific with button type
Log in
&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;
);
};
export default Login;

huangapple
  • 本文由 发表于 2023年1月9日 01:39:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定