Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.3 kB
4
Indexable
Never
const Login = () => {
  const [form] = Form.useForm();
  const navigate = useNavigate();

  const handleSubmit = (request) => {
    axios
      .post("http://localhost:8080/api/v1/login/authenticate", {
        email: request.email,
        password: request.password,
      })
      .then((response) => {
        if (response.status === HttpStatusCode.Ok) {
          if (response.data && response.data.id && response.data.email && response.data.role && response.data.token) {

            const { id, email, role, token } = response.data;

            localStorage.setItem('userId', id);
            localStorage.setItem('userEmail', email);
            localStorage.setItem('userRole', role);
            localStorage.setItem('userToken', token);
            axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;

            console.log(id);
            console.log(email);
            console.log(role);
            console.log(token);

            //TODO: go to correct page
            navigate("/admin/repository");

          }
        } else {
          alert("Invalid username or password!");
        }
      })
      .catch(() => {
          alert("An error occurred while processing the request.");
      });
  }