[8ca35dc] | 1 | import React, { useState } from 'react';
|
---|
| 2 | import axios from 'axios';
|
---|
| 3 | import { useNavigate } from 'react-router-dom';
|
---|
| 4 | import 'bootstrap/dist/css/bootstrap.min.css';
|
---|
| 5 | import classNames from 'classnames';
|
---|
| 6 | import MembershipsEnum from './MembershipsEnum';
|
---|
| 7 |
|
---|
| 8 | const AuthForm = () => {
|
---|
| 9 | const navigate = useNavigate();
|
---|
| 10 | const [activeTab, setActiveTab] = useState('login');
|
---|
| 11 | const [credentials, setCredentials] = useState({
|
---|
| 12 | firstName: '',
|
---|
| 13 | lastName: '',
|
---|
| 14 | email: '',
|
---|
| 15 | password: '',
|
---|
| 16 | phone: '',
|
---|
| 17 | address: '',
|
---|
| 18 | membershipLevel: ''
|
---|
| 19 | });
|
---|
| 20 | const [error, setError] = useState('');
|
---|
| 21 |
|
---|
| 22 | const handleChange = (e) => {
|
---|
| 23 | const { name, value } = e.target;
|
---|
| 24 | setCredentials({ ...credentials, [name]: value });
|
---|
| 25 | };
|
---|
| 26 |
|
---|
| 27 | const handleLogin = async (e) => {
|
---|
| 28 | e.preventDefault();
|
---|
| 29 | try {
|
---|
| 30 | const response = await axios.post('http://localhost:8081/api/login', {
|
---|
| 31 | email: credentials.email,
|
---|
| 32 | password: credentials.password
|
---|
| 33 | });
|
---|
| 34 |
|
---|
| 35 | const { token } = response.data;
|
---|
| 36 | localStorage.setItem('token', token);
|
---|
| 37 | navigate('/');
|
---|
| 38 | } catch (err) {
|
---|
| 39 | setError('Login failed. Please check your credentials.');
|
---|
| 40 | }
|
---|
| 41 | };
|
---|
| 42 |
|
---|
| 43 | const handleRegister = async (e) => {
|
---|
| 44 | e.preventDefault();
|
---|
| 45 | try {
|
---|
| 46 | await axios.post('http://localhost:8081/api/register', {
|
---|
| 47 | firstName: credentials.firstName,
|
---|
| 48 | lastName: credentials.lastName,
|
---|
| 49 | email: credentials.email,
|
---|
| 50 | password: credentials.password,
|
---|
| 51 | phone: credentials.phone,
|
---|
| 52 | address: credentials.address,
|
---|
| 53 | membershipLevel: credentials.membershipLevel
|
---|
| 54 | });
|
---|
| 55 | setActiveTab('login');
|
---|
| 56 | alert('Registration successful. Please log in.');
|
---|
| 57 | } catch (err) {
|
---|
| 58 | setError('Registration failed. Please try again.');
|
---|
| 59 | }
|
---|
| 60 | };
|
---|
| 61 |
|
---|
| 62 | return (
|
---|
| 63 | <div className="container mt-5">
|
---|
| 64 | <ul className="nav nav-pills nav-justified mb-3">
|
---|
| 65 | <li className="nav-item">
|
---|
| 66 | <button
|
---|
| 67 | className={classNames('nav-link', { active: activeTab === 'login' })}
|
---|
| 68 | onClick={() => setActiveTab('login')}
|
---|
| 69 | >
|
---|
| 70 | Login
|
---|
| 71 | </button>
|
---|
| 72 | </li>
|
---|
| 73 | <li className="nav-item">
|
---|
| 74 | <button
|
---|
| 75 | className={classNames('nav-link', { active: activeTab === 'register' })}
|
---|
| 76 | onClick={() => setActiveTab('register')}
|
---|
| 77 | >
|
---|
| 78 | Register
|
---|
| 79 | </button>
|
---|
| 80 | </li>
|
---|
| 81 | </ul>
|
---|
| 82 |
|
---|
| 83 | {activeTab === 'login' && (
|
---|
| 84 | <form onSubmit={handleLogin}>
|
---|
| 85 | <div className="form-outline mb-4">
|
---|
| 86 | <input
|
---|
| 87 | type="email"
|
---|
| 88 | name="email"
|
---|
| 89 | className="form-control"
|
---|
| 90 | placeholder="Email"
|
---|
| 91 | value={credentials.email}
|
---|
| 92 | onChange={handleChange}
|
---|
| 93 | required
|
---|
| 94 | />
|
---|
| 95 | </div>
|
---|
| 96 | <div className="form-outline mb-4">
|
---|
| 97 | <input
|
---|
| 98 | type="password"
|
---|
| 99 | name="password"
|
---|
| 100 | className="form-control"
|
---|
| 101 | placeholder="Password"
|
---|
| 102 | value={credentials.password}
|
---|
| 103 | onChange={handleChange}
|
---|
| 104 | required
|
---|
| 105 | />
|
---|
| 106 | </div>
|
---|
| 107 | {error && <div className="text-danger mb-3">{error}</div>}
|
---|
| 108 | <button type="submit" className="btn btn-primary w-100">Login</button>
|
---|
| 109 | </form>
|
---|
| 110 | )}
|
---|
| 111 |
|
---|
| 112 | {activeTab === 'register' && (
|
---|
| 113 | <form onSubmit={handleRegister}>
|
---|
| 114 | <div className="form-outline mb-4">
|
---|
| 115 | <input
|
---|
| 116 | type="text"
|
---|
| 117 | name="firstName"
|
---|
| 118 | className="form-control"
|
---|
| 119 | placeholder="First Name"
|
---|
| 120 | value={credentials.firstName}
|
---|
| 121 | onChange={handleChange}
|
---|
| 122 | required
|
---|
| 123 | />
|
---|
| 124 | </div>
|
---|
| 125 | <div className="form-outline mb-4">
|
---|
| 126 | <input
|
---|
| 127 | type="text"
|
---|
| 128 | name="lastName"
|
---|
| 129 | className="form-control"
|
---|
| 130 | placeholder="Last Name"
|
---|
| 131 | value={credentials.lastName}
|
---|
| 132 | onChange={handleChange}
|
---|
| 133 | required
|
---|
| 134 | />
|
---|
| 135 | </div>
|
---|
| 136 | <div className="form-outline mb-4">
|
---|
| 137 | <input
|
---|
| 138 | type="email"
|
---|
| 139 | name="email"
|
---|
| 140 | className="form-control"
|
---|
| 141 | placeholder="Email"
|
---|
| 142 | value={credentials.email}
|
---|
| 143 | onChange={handleChange}
|
---|
| 144 | required
|
---|
| 145 | />
|
---|
| 146 | </div>
|
---|
| 147 | <div className="form-outline mb-4">
|
---|
| 148 | <input
|
---|
| 149 | type="password"
|
---|
| 150 | name="password"
|
---|
| 151 | className="form-control"
|
---|
| 152 | placeholder="Password"
|
---|
| 153 | value={credentials.password}
|
---|
| 154 | onChange={handleChange}
|
---|
| 155 | required
|
---|
| 156 | />
|
---|
| 157 | </div>
|
---|
| 158 | <div className="form-outline mb-4">
|
---|
| 159 | <input
|
---|
| 160 | type="text"
|
---|
| 161 | name="phone"
|
---|
| 162 | className="form-control"
|
---|
| 163 | placeholder="Phone"
|
---|
| 164 | value={credentials.phone}
|
---|
| 165 | onChange={handleChange}
|
---|
| 166 | required
|
---|
| 167 | />
|
---|
| 168 | </div>
|
---|
| 169 | <div className="form-outline mb-4">
|
---|
| 170 | <input
|
---|
| 171 | type="text"
|
---|
| 172 | name="address"
|
---|
| 173 | className="form-control"
|
---|
| 174 | placeholder="Address"
|
---|
| 175 | value={credentials.address}
|
---|
| 176 | onChange={handleChange}
|
---|
| 177 | required
|
---|
| 178 | />
|
---|
| 179 | </div>
|
---|
| 180 | <div className="form-outline mb-4">
|
---|
| 181 | <select
|
---|
| 182 | name="membershipLevel"
|
---|
| 183 | className="form-control"
|
---|
| 184 | value={credentials.membershipLevel}
|
---|
| 185 | onChange={handleChange}
|
---|
| 186 | required
|
---|
| 187 | >
|
---|
| 188 | <option value="">Select Membership Type</option>
|
---|
| 189 | {MembershipsEnum().map((membership, index) => (
|
---|
| 190 | <option key={index} value={membership}>
|
---|
| 191 | {membership}
|
---|
| 192 | </option>
|
---|
| 193 | ))}
|
---|
| 194 | </select>
|
---|
| 195 | </div>
|
---|
| 196 | {error && <div className="text-danger mb-3">{error}</div>}
|
---|
| 197 | <button type="submit" className="btn btn-success w-100">Register</button>
|
---|
| 198 | </form>
|
---|
| 199 | )}
|
---|
| 200 | </div>
|
---|
| 201 | );
|
---|
| 202 | };
|
---|
| 203 |
|
---|
| 204 | export default AuthForm;
|
---|