Advertisement
Dodo67

RegitserSetup

Feb 2nd, 2022
169
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/auth.svg';
  3. import { ToastContainer, toast } from 'react-toastify';
  4. import axios from 'axios';
  5. import { authenticate, isAuth } from '../helpers/auth';
  6. import { Link, Navigate } from 'react-router-dom';
  7.  
  8. const Register = () => {
  9.   const [formData, setFormData] = useState({
  10.     name: '',
  11.     email: '',
  12.     password1: '',
  13.     password2: '',
  14.     textChange: 'Sign Up'
  15.   });
  16.  
  17.   const { name, email, password1, password2, textChange } = formData;
  18.   const handleChange = text => e => {
  19.     setFormData({ ...formData, [text]: e.target.value });
  20.   };
  21.   const handleSubmit = e => {
  22.     e.preventDefault();
  23.     if (name && email && password1) {
  24.       if (password1 === password2) {
  25.         setFormData({ ...formData, textChange: 'Submitting' });
  26.         axios
  27.           .post(`${process.env.REACT_APP_API_URL}/register`, {
  28.             name,
  29.             email,
  30.             password: password1
  31.           })
  32.           .then(res => {
  33.             setFormData({
  34.               ...formData,
  35.               name: '',
  36.               email: '',
  37.               password1: '',
  38.               password2: '',
  39.               textChange: 'Submitted'
  40.             });
  41.  
  42.             toast.success(res.data.message);
  43.           })
  44.           .catch(err => {
  45.             setFormData({
  46.               ...formData,
  47.               name: '',
  48.               email: '',
  49.               password1: '',
  50.               password2: '',
  51.               textChange: 'Sign Up'
  52.             });
  53.             console.log(err.response);
  54.             toast.error(err.response.data.errors);
  55.           });
  56.       } else {
  57.         toast.error("Passwords don't matches");
  58.       }
  59.     } else {
  60.       toast.error('Please fill all fields');
  61.     }
  62.   };
  63.  
  64.   return (
  65.     <div className='min-h-screen bg-gray-100 text-gray-900 flex justify-center'>
  66.       {isAuth() ? <Navigate to='/' /> : null}
  67.       <ToastContainer />
  68.       <div className='max-w-screen-xl m-0 sm:m-20 bg-white shadow sm:rounded-lg flex justify-center flex-1'>
  69.         <div className='lg:w-1/2 xl:w-5/12 p-6 sm:p-12'>
  70.           <div className='mt-12 flex flex-col items-center'>
  71.             <h1 className='text-2xl xl:text-3xl font-extrabold'>
  72.               Sign Up for Congar
  73.             </h1>
  74.  
  75.             <form
  76.               className='w-full flex-1 mt-8 text-indigo-500'
  77.               onSubmit={handleSubmit}
  78.             >
  79.               <div className='mx-auto max-w-xs relative '>
  80.                 <input
  81.                   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'
  82.                   type='text'
  83.                   placeholder='Name'
  84.                   onChange={handleChange('name')}
  85.                   value={name}
  86.                 />
  87.                 <input
  88.                   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'
  89.                   type='email'
  90.                   placeholder='Email'
  91.                   onChange={handleChange('email')}
  92.                   value={email}
  93.                 />
  94.                 <input
  95.                   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'
  96.                   type='password'
  97.                   placeholder='Password'
  98.                   onChange={handleChange('password1')}
  99.                   value={password1}
  100.                 />
  101.                 <input
  102.                   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'
  103.                   type='password'
  104.                   placeholder='Confirm Password'
  105.                   onChange={handleChange('password2')}
  106.                   value={password2}
  107.                 />
  108.                 <button
  109.                   type='submit'
  110.                   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'
  111.                 >
  112.                   <i className='fas fa-user-plus fa 1x w-6  -ml-2' />
  113.                   <span className='ml-3'>{textChange}</span>
  114.                 </button>
  115.               </div>
  116.               <div className='my-12 border-b text-center'>
  117.                 <div className='leading-none px-2 inline-block text-sm text-gray-600 tracking-wide font-medium bg-white transform translate-y-1/2'>
  118.                   Or sign with email or social login
  119.                 </div>
  120.               </div>
  121.               <div className='flex flex-col items-center'>
  122.                 <a
  123.                   className='w-full max-w-xs font-bold shadow-sm rounded-lg py-3
  124.           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'
  125.                   href='/login'
  126.                   target='_self'
  127.                 >
  128.                   <i className='fas fa-sign-in-alt fa 1x w-6  -ml-2 text-indigo-500' />
  129.                   <span className='ml-4'>Sign In</span>
  130.                 </a>
  131.               </div>
  132.             </form>
  133.           </div>
  134.         </div>
  135.         <div className='flex-1 bg-indigo-100 text-center hidden lg:flex'>
  136.           <div
  137.             className='m-12 xl:m-16 w-full bg-contain bg-center bg-no-repeat'
  138.             style={{ backgroundImage: `url(${authSvg})` }}
  139.           ></div>
  140.         </div>
  141.       </div>
  142.       ;
  143.     </div>
  144.   );
  145. };
  146.  
  147. export default Register;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement