backend / frontend /src /pages /Login.jsx
muhammadhasnain100's picture
add code
49f22ac
Raw History Blame Contribute Delete
5.71 kB
import { useEffect, useState } from 'react'
import { Link, useLocation, useNavigate } from 'react-router-dom'
import { login } from '../api/auth'
import { setSession } from '../auth/storage'
import AuthLayout from './AuthLayout'
function EnvelopeIcon({ className = '' }) {
return (
<svg className={className} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.75" aria-hidden>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M21.75 6.75v10.5a2.25 2.25 0 01-2.25 2.25h-15a2.25 2.25 0 01-2.25-2.25V6.75m19.5 0A2.25 2.25 0 0019.5 4.5h-15a2.25 2.25 0 00-2.25 2.25m19.5 0v.243a2.25 2.25 0 01-1.07 1.916l-7.5 4.615a2.25 2.25 0 01-2.36 0L3.32 8.91a2.25 2.25 0 01-1.07-1.916V6.75"
/>
</svg>
)
}
function LockIcon({ className = '' }) {
return (
<svg className={className} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.75" aria-hidden>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M16.5 10.5V6.75a4.5 4.5 0 10-9 0v3.75m-.75 11.25h10.5a2.25 2.25 0 002.25-2.25v-6.75a2.25 2.25 0 00-2.25-2.25H6.75a2.25 2.25 0 00-2.25 2.25v6.75a2.25 2.25 0 002.25 2.25z"
/>
</svg>
)
}
export default function Login() {
const navigate = useNavigate()
const location = useLocation()
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const [error, setError] = useState('')
const [loading, setLoading] = useState(false)
useEffect(() => {
if (location.state?.email) setEmail(location.state.email)
}, [location.state])
async function handleSubmit(e) {
e.preventDefault()
setError('')
setLoading(true)
try {
const data = await login(email.trim(), password)
setSession({
access_token: data.access_token,
refresh_token: data.refresh_token,
user: data.user,
})
navigate('/chat', { replace: true })
} catch (err) {
setError(err.message || 'Login failed')
} finally {
setLoading(false)
}
}
return (
<AuthLayout
brandTo="/login"
title="Sign in"
subtitle="Use your CUI Connect account."
>
<form onSubmit={handleSubmit} className="flex flex-col gap-5 sm:gap-6">
{location.state?.registered ? (
<div
role="status"
className="rounded-lg border border-emerald-200 bg-emerald-50 px-3 py-2 text-sm text-emerald-900"
>
Account created. You can sign in now.
</div>
) : null}
{error ? (
<div
role="alert"
className="rounded-lg border border-red-200 bg-red-50 px-3 py-2 text-sm text-red-800"
>
{error}
</div>
) : null}
<div className="flex flex-col gap-1.5">
<label htmlFor="login-email" className="text-sm font-medium text-slate-700">
Email
</label>
<div className="relative">
<EnvelopeIcon className="pointer-events-none absolute left-3.5 top-1/2 h-5 w-5 -translate-y-1/2 text-slate-400" />
<input
id="login-email"
name="email"
type="email"
autoComplete="email"
required
value={email}
onChange={(e) => setEmail(e.target.value)}
className="min-h-[48px] w-full rounded-xl border border-slate-300 bg-white py-3 pl-11 pr-4 text-base text-slate-900 shadow-sm outline-none transition-colors focus:border-comsats-blue focus:ring-2 focus:ring-comsats-blue/20 sm:text-sm"
/>
</div>
</div>
<div className="flex flex-col gap-1.5">
<label htmlFor="login-password" className="text-sm font-medium text-slate-700">
Password
</label>
<div className="relative">
<LockIcon className="pointer-events-none absolute left-3.5 top-1/2 h-5 w-5 -translate-y-1/2 text-slate-400" />
<input
id="login-password"
name="password"
type="password"
autoComplete="current-password"
required
value={password}
onChange={(e) => setPassword(e.target.value)}
className="min-h-[48px] w-full rounded-xl border border-slate-300 bg-white py-3 pl-11 pr-4 text-base text-slate-900 shadow-sm outline-none transition-colors focus:border-comsats-blue focus:ring-2 focus:ring-comsats-blue/20 sm:text-sm"
/>
</div>
</div>
<button
type="submit"
disabled={loading}
className="mt-1 flex min-h-[48px] w-full items-center justify-center rounded-xl bg-gradient-to-r from-comsats-purple to-[#3d2469] px-4 py-3 text-sm font-semibold text-white shadow-md shadow-comsats-purple/25 transition-all hover:brightness-105 focus:outline-none focus-visible:ring-2 focus-visible:ring-comsats-blue focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-60"
>
{loading ? 'Signing in…' : 'Sign in'}
</button>
<p className="text-center text-sm text-slate-600">
No account?{' '}
<Link
to="/signup"
className="font-semibold text-comsats-blue underline-offset-2 hover:underline"
>
Create one
</Link>
<span className="mx-2 text-slate-300">·</span>
<Link
to="/admin/login"
className="font-semibold text-slate-600 underline-offset-2 hover:text-comsats-purple hover:underline"
>
Administrator sign-in
</Link>
</p>
</form>
</AuthLayout>
)
}