Hen_B_S

Login Form React TypeScript

Feb 2nd, 2023
1,098
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 3.77 KB | Source Code | 0 0
  1. import axios, { AxiosRequestConfig } from "axios";
  2. import { connect } from 'react-redux';
  3. import { useNavigate } from "react-router-dom";
  4. import { authenticate, authFailure, authSuccess } from "service/authActions";
  5. import { BASE_URL } from "utils/requests";
  6.  
  7.  
  8. function LoginForm({ ...props }: any) {
  9.  
  10.     const navigate = useNavigate();
  11.     const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
  12.         event.preventDefault();
  13.         const userName = (event.target as any).userName.value;
  14.         const password = (event.target as any).password.value;
  15.  
  16.         const config: AxiosRequestConfig = {
  17.             baseURL: BASE_URL,
  18.             method: "POST",
  19.             url: "/auth/login",
  20.             data: {
  21.                 userName: userName,
  22.                 password: password
  23.             },
  24.         };
  25.         axios(config).then((response) => {
  26.             if (response.status === 200) {
  27.                 props.setUser(response.data);
  28.                 navigate("/home");
  29.             } else {
  30.                 props.loginFailure("Algo de errado ocorreu! Tente novamente");
  31.             }
  32.         }).catch((err) => {
  33.             if (err && err.response) {
  34.                 switch (err.response.status) {
  35.                     case 401:
  36.                         console.log("401 status");
  37.                         props.loginFailure("As Credenciais Falharam na Autenticação");
  38.                         break;
  39.                     default:
  40.                         props.loginFailure("Algo de errado ocorreu! Tente novamente");
  41.                 }
  42.             } else {
  43.                 props.loginFailure("Algo de errado ocorreu! Tente novamente");
  44.             }
  45.         });
  46.     }
  47.  
  48.     return (
  49.         <div className="form-container">
  50.             <div className="form-card-container">
  51.                 <h3>Introduza os dados da sua conta</h3>
  52.                 <form className="gerencg-form" onSubmit={handleSubmit} noValidate={false}>
  53.                     <div className="form-group gerencg-form-group">
  54.                         <label htmlFor="userName">Nome de Usuário: </label>
  55.                         <input type="text" className="form-control" id="userName" required />
  56.                     </div>
  57.  
  58.                     <div className="form-group gerencg-form-group">
  59.                         <label htmlFor="password">Senha</label>
  60.                         <input type="password" className="form-control" id="password" required />
  61.                         <a href="forget.html" className="float-right form-links">
  62.                             Esqueceu a Senha ?
  63.                         </a>
  64.                     </div>
  65.  
  66.                     <div className="form-group">
  67.                         <div className="custom-control custom-checkbox">
  68.                             <input type="checkbox" className="custom-control-input" id="customCheck1" />
  69.                             <label className="custom-control-label msg-container" htmlFor="customCheck1">
  70.                                 Mantenha-me Conectado
  71.                             </label>
  72.                         </div>
  73.                     </div>
  74.  
  75.                     <div className="form-btn-container">
  76.                         <button type="submit" className="gerencg-btn" >
  77.                             Entrar
  78.                         </button>
  79.                     </div>
  80.                 </form>
  81.             </div>
  82.         </div>
  83.     );
  84. }
  85.  
  86. const mapDispatchToProps = (dispatch: (arg0: { type: any; payload?: any; }) => any) => {
  87.     return {
  88.         authenticate: () => dispatch(authenticate()),
  89.         setUser: (data: { token: string; }) => dispatch(authSuccess(data)),
  90.         loginFailure: (message: string) => dispatch(authFailure(message))
  91.     }
  92. }
  93.  
  94.  
  95. export default connect(mapDispatchToProps)(LoginForm);
Advertisement
Add Comment
Please, Sign In to add comment