O
O
Orion Orion2022-03-27 16:55:55
typescript
Orion Orion, 2022-03-27 16:55:55

How to handle an error?

import { useState } from 'react';
import AuthService from '../http/auth';

export const useLogin = () => {
  const [email, setEmail] = useState<string>('');
  const [password, setPassword] = useState<string>('');
  const [userAcc, setUserAcc] = useState<any>();

  const userLogin = async () => {
    try {
      const response = await AuthService.login(email, password);
      console.log(response);
      // if (response.status === 404) {
      //   return alert('Не правльный логин или пароль!');
      // }
      localStorage.setItem('token', JSON.stringify(response.data.accessToken));
      setUserAcc(response.data.user);
      const dt = await AuthService.login(email, password);
    } catch (error) {
      console.log(error);
    }
  };
  return { email, password, setEmail, setPassword, userLogin, userAcc };
};

if you enter an incorrect email or password on the server, it gives an error, so is it possible to catch this error on the client and display a message in alertor somewhere else as in the above commented code!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question