| import React, { useState } from 'react'; |
| import { useNavigate } from 'react-router-dom'; |
| import { api } from '../services/api'; |
|
|
| |
| const PREDEFINED_USERS = { |
| 'mche0278@student.monash.edu': { name: 'Michelle Chen', email: 'mche0278@student.monash.edu' }, |
| 'zfan0011@student.monash.edu': { name: 'Fang Zhou', email: 'zfan0011@student.monash.edu' }, |
| 'yfuu0071@student.monash.edu': { name: 'Fu Yitong', email: 'yfuu0071@student.monash.edu' }, |
| 'hhan0022@student.monash.edu': { name: 'Han Heyang', email: 'hhan0022@student.monash.edu' }, |
| 'ylei0024@student.monash.edu': { name: 'Lei Yang', email: 'ylei0024@student.monash.edu' }, |
| 'wlii0217@student.monash.edu': { name: 'Li Wenhui', email: 'wlii0217@student.monash.edu' }, |
| 'zlia0091@student.monash.edu': { name: 'Liao Zhixin', email: 'zlia0091@student.monash.edu' }, |
| 'hmaa0054@student.monash.edu': { name: 'Ma Huachen', email: 'hmaa0054@student.monash.edu' }, |
| 'csun0059@student.monash.edu': { name: 'Sun Chongkai', email: 'csun0059@student.monash.edu' }, |
| 'pwon0030@student.monash.edu': { name: 'Wong Prisca Si-Heng', email: 'pwon0030@student.monash.edu' }, |
| 'zxia0017@student.monash.edu': { name: 'Xia Zechen', email: 'zxia0017@student.monash.edu' }, |
| 'lyou0021@student.monash.edu': { name: 'You Lianxiang', email: 'lyou0021@student.monash.edu' }, |
| 'wzhe0034@student.monash.edu': { name: 'Zheng Weijie', email: 'wzhe0034@student.monash.edu' }, |
| 'szhe0055@student.monash.edu': { name: 'Zheng Siyuan', email: 'szhe0055@student.monash.edu' }, |
| 'xzhe0055@student.monash.edu': { name: 'Zheng Xiang', email: 'xzhe0055@student.monash.edu' }, |
| 'hongchang.yu@monash.edu': { name: 'Tristan', email: 'hongchang.yu@monash.edu' } |
| }; |
|
|
| const Login: React.FC = () => { |
| const [email, setEmail] = useState(''); |
| const [isLoading, setIsLoading] = useState(false); |
| const [error, setError] = useState(''); |
| const navigate = useNavigate(); |
|
|
| const handleSubmit = async (e: React.FormEvent) => { |
| e.preventDefault(); |
| setIsLoading(true); |
| setError(''); |
|
|
| try { |
| const response = await api.post('/api/auth/login', { email }); |
| const { token, user } = response.data; |
|
|
| localStorage.setItem('token', token); |
| localStorage.setItem('user', JSON.stringify(user)); |
| api.defaults.headers.common['Authorization'] = `Bearer ${token}`; |
| |
| navigate('/dashboard'); |
| } catch (error: any) { |
| console.error('Login error:', error); |
| setError(error.response?.data?.error || 'Login failed'); |
| } finally { |
| setIsLoading(false); |
| } |
| }; |
|
|
| return ( |
| <div className="min-h-screen flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8"> |
| <div className="max-w-md w-full space-y-8"> |
| <div className="text-center"> |
| <h1 className="text-3xl font-bold text-gray-900 mb-2"> |
| Welcome to TransHub |
| </h1> |
| <p className="text-gray-600"> |
| Enter your student email address to continue |
| </p> |
| </div> |
| |
| {error && ( |
| <div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-md"> |
| {error} |
| </div> |
| )} |
| |
| <form className="mt-8 space-y-6" onSubmit={handleSubmit}> |
| <div> |
| <label htmlFor="email" className="block text-sm font-medium text-gray-700 mb-2"> |
| Student Email Address |
| </label> |
| <input |
| id="email" |
| name="email" |
| type="email" |
| autoComplete="email" |
| required |
| className="appearance-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm" |
| placeholder="your.email@student.monash.edu" |
| value={email} |
| onChange={(e) => { |
| setEmail(e.target.value); |
| if (error) setError(''); |
| }} |
| /> |
| </div> |
| |
| <div> |
| <button |
| type="submit" |
| disabled={isLoading} |
| className="group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 disabled:opacity-50 disabled:cursor-not-allowed" |
| > |
| {isLoading ? ( |
| <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white"></div> |
| ) : ( |
| 'Continue' |
| )} |
| </button> |
| </div> |
| </form> |
| |
| <div className="text-center"> |
| <p className="text-xs text-gray-500"> |
| If you're not a student, you can still enter any email address to access as a visitor. |
| </p> |
| </div> |
| </div> |
| </div> |
| ); |
| }; |
|
|
| export default Login; |