Dodo67

Login

Mar 28th, 2022
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState } from 'react';
  2. import authSvg from '../assests/login.svg';
  3. import { ToastContainer, toast } from 'react-toastify';
  4. import axios from 'axios';
  5. import { authenticate, isAuth } from '../helpers/auth';
  6. import { Link, useNavigate } from 'react-router-dom';
  7. import { GoogleLogin } from 'react-google-login';
  8. import FacebookLogin from 'react-facebook-login/dist/facebook-login-render-props';
  9.  
  10. const Login = () => {
  11.     const navigate = useNavigate();
  12.     const [formData, setFormData] = useState({
  13.         email: '',
  14.         password1: '',
  15.         textChange: 'Sign In',
  16.     });
  17.     const { email, password1, textChange } = formData;
  18.     const handleChange = text => e => {
  19.         setFormData({ ...formData, [text]: e.target.value });
  20.     };
  21.  
  22.     const sendGoogleToken = tokenId => {
  23.         axios
  24.             .post(`${process.env.REACT_APP_API_URL}/googlelogin`, {
  25.                 idToken: tokenId,
  26.             })
  27.             .then(res => {
  28.                 console.log(res.data);
  29.                 informParent(res);
  30.             })
  31.             .catch(error => {
  32.                 console.log('GOOGLE SIGNIN ERROR', error.response);
  33.             });
  34.     };
  35.     const informParent = response => {
  36.         authenticate(response, () => {
  37.             isAuth() && isAuth().role === 'admin'
  38.                 ? navigate('/admin')
  39.                 : navigate('/');
  40.         });
  41.     };
  42.  
  43.     const sendFacebookToken = (userID, accessToken) => {
  44.         axios
  45.             .post(`${process.env.REACT_APP_API_URL}/facebooklogin`, {
  46.                 userID,
  47.                 accessToken,
  48.             })
  49.             .then(res => {
  50.                 console.log(res.data);
  51.                 informParent(res);
  52.             })
  53.             .catch(error => {
  54.                 console.log('GOOGLE SIGNIN ERROR', error.response);
  55.             });
  56.     };
  57.     const responseGoogle = response => {
  58.         console.log('helllo from response google');
  59.         console.log(response);
  60.         sendGoogleToken(response.tokenId);
  61.     };
  62.  
  63.     const responseFacebook = response => {
  64.         console.log(response);
  65.         sendFacebookToken(response.userID, response.accessToken);
  66.     };
  67.  
  68.     const handleSubmit = e => {
  69.         console.log(process.env.REACT_APP_API_URL);
  70.         e.preventDefault();
  71.         if (email && password1) {
  72.             setFormData({ ...formData, textChange: 'Submitting' });
  73.             axios
  74.                 .post(`${process.env.REACT_APP_API_URL}/login`, {
  75.                     email,
  76.                     password: password1,
  77.                 })
  78.                 .then(res => {
  79.                     authenticate(res, () => {
  80.                         setFormData({
  81.                             ...formData,
  82.                             email: '',
  83.                             password1: '',
  84.                             textChange: 'Submitted',
  85.                         });
  86.                         isAuth() && isAuth().role === 'admin'
  87.                             ? navigate('/admin')
  88.                             : navigate('/');
  89.                         toast.success(
  90.                             `Hey ${res.data.user.name}, Welcome back!`
  91.                         );
  92.                     });
  93.                 })
  94.                 .catch(err => {
  95.                     setFormData({
  96.                         ...formData,
  97.                         email: '',
  98.                         password1: '',
  99.                         textChange: 'Sign In',
  100.                     });
  101.                     console.log(err.response);
  102.                     toast.error(err.response.data.errors);
  103.                 });
  104.         } else {
  105.             toast.error('Please fill all fields');
  106.         }
  107.     };
  108.     return (
  109.         <div className='min-h-screen bg-gray-100 text-gray-900 flex justify-center'>
  110.             {isAuth() ? navigate('/') : null}
  111.             <ToastContainer />
  112.             <div className='max-w-screen-xl m-0 sm:m-20 bg-white shadow sm:rounded-lg flex justify-center flex-1'>
  113.                 <div className='lg:w-1/2 xl:w-5/12 p-6 sm:p-12'>
  114.                     <div className='mt-12 flex flex-col items-center'>
  115.                         <h1 className='text-2xl xl:text-3xl font-extrabold'>
  116.                             Sign In
  117.                         </h1>
  118.                         <div className='w-full flex-1 mt-8 text-indigo-500'>
  119.                             <div className='flex flex-col items-center'>
  120.                                 <GoogleLogin
  121.                                     clientId={`${process.env.REACT_APP_GOOGLE_CLIENT}`}
  122.                                     onSuccess={responseGoogle}
  123.                                     onFailure={responseGoogle}
  124.                                     cookiePolicy={'single_host_origin'}
  125.                                     render={renderProps => (
  126.                                         <button
  127.                                             onClick={renderProps.onClick}
  128.                                             disabled={renderProps.disabled}
  129.                                             className='w-full max-w-xs font-bold shadow-sm rounded-lg py-3 bg-indigo-100 text-gray-800 flex items-center justify-center transition-all duration-300 ease-in-out focus:outline-none hover:shadow focus:shadow-sm focus:shadow-outline'>
  130.                                             <div className=' p-2 rounded-full '>
  131.                                                 <i className='fab fa-google ' />
  132.                                             </div>
  133.                                             <span className='ml-4'>
  134.                                                 Sign In with Google
  135.                                             </span>
  136.                                         </button>
  137.                                     )}></GoogleLogin>
  138.                                 <FacebookLogin
  139.                                     appId={`${process.env.REACT_APP_FACEBOOK_CLIENT}`}
  140.                                     autoLoad={false}
  141.                                     callback={responseFacebook}
  142.                                     render={renderProps => (
  143.                                         <button
  144.                                             onClick={renderProps.onClick}
  145.                                             className='w-full max-w-xs font-bold shadow-sm rounded-lg py-3 bg-indigo-100 text-gray-800 flex items-center justify-center transition-all duration-300 ease-in-out focus:outline-none hover:shadow focus:shadow-sm focus:shadow-outline mt-5'>
  146.                                             <div className=' p-2 rounded-full '>
  147.                                                 <i className='fab fa-facebook' />
  148.                                             </div>
  149.                                             <span className='ml-4'>
  150.                                                 Sign In with Facebook
  151.                                             </span>
  152.                                         </button>
  153.                                     )}
  154.                                 />
  155.  
  156.                                 <a
  157.                                     className='w-full max-w-xs font-bold shadow-sm rounded-lg py-3
  158.           bg-indigo-100 text-gray-800 flex items-center justify-center transition-all duration-300 ease-in-out focus:outline-none hover:shadow focus:shadow-sm focus:shadow-outline mt-5'
  159.                                     href='/register'
  160.                                     target='_self'>
  161.                                     <i className='fas fa-user-plus fa 1x w-6  -ml-2 text-indigo-500' />
  162.                                     <span className='ml-4'>Sign Up</span>
  163.                                 </a>
  164.                             </div>
  165.                             <div className='my-12 border-b text-center'>
  166.                                 <div className='leading-none px-2 inline-block text-sm text-gray-600 tracking-wide font-medium bg-white transform translate-y-1/2'>
  167.                                     Or sign In with e-mail
  168.                                 </div>
  169.                             </div>
  170.                             <form
  171.                                 className='mx-auto max-w-xs relative '
  172.                                 onSubmit={handleSubmit}>
  173.                                 <input
  174.                                     className='w-full px-8 py-4 rounded-lg font-medium bg-gray-100 border border-gray-200 placeholder-gray-500 text-sm focus:outline-none focus:border-gray-400 focus:bg-white'
  175.                                     type='email'
  176.                                     placeholder='Email'
  177.                                     onChange={handleChange('email')}
  178.                                     value={email}
  179.                                 />
  180.                                 <input
  181.                                     className='w-full px-8 py-4 rounded-lg font-medium bg-gray-100 border border-gray-200 placeholder-gray-500 text-sm focus:outline-none focus:border-gray-400 focus:bg-white mt-5'
  182.                                     type='password'
  183.                                     placeholder='Password'
  184.                                     onChange={handleChange('password1')}
  185.                                     value={password1}
  186.                                 />
  187.                                 <button
  188.                                     type='submit'
  189.                                     className='mt-5 tracking-wide font-semibold bg-indigo-500 text-gray-100 w-full py-4 rounded-lg hover:bg-indigo-700 transition-all duration-300 ease-in-out flex items-center justify-center focus:shadow-outline focus:outline-none'>
  190.                                     <i className='fas fa-sign-in-alt  w-6  -ml-2' />
  191.                                     <span className='ml-3'>Sign In</span>
  192.                                 </button>
  193.                                 <Link
  194.                                     to='/users/password/forget'
  195.                                     className='no-underline hover:underline text-indigo-500 text-md text-right absolute right-0  mt-2'>
  196.                                     Forget password?
  197.                                 </Link>
  198.                             </form>
  199.                         </div>
  200.                     </div>
  201.                 </div>
  202.                 <div className='flex-1 bg-indigo-100 text-center hidden lg:flex'>
  203.                     <div
  204.                         className='m-12 xl:m-16 w-full bg-contain bg-center bg-no-repeat'
  205.                         style={{ backgroundImage: `url(${authSvg})` }}></div>
  206.                 </div>
  207.             </div>
  208.         </div>
  209.     );
  210. };
  211.  
  212. export default Login;
Advertisement
Add Comment
Please, Sign In to add comment