189 lines
6.0 KiB
JavaScript
189 lines
6.0 KiB
JavaScript
import React, { useState } from 'react';
|
|
import { Link, useNavigate, useLocation } from 'react-router-dom';
|
|
import { useForm } from 'react-hook-form';
|
|
import { EyeIcon, EyeSlashIcon } from '@heroicons/react/24/outline';
|
|
import { useAuth } from '../../hooks/useAuth';
|
|
import LoadingSpinner from '../../components/UI/LoadingSpinner';
|
|
|
|
const Login = () => {
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
const { login, loading } = useAuth();
|
|
const navigate = useNavigate();
|
|
const location = useLocation();
|
|
|
|
const from = location.state?.from?.pathname || '/dashboard';
|
|
|
|
const {
|
|
register,
|
|
handleSubmit,
|
|
formState: { errors },
|
|
setError,
|
|
} = useForm();
|
|
|
|
const onSubmit = async (data) => {
|
|
const result = await login(data);
|
|
if (result.success) {
|
|
navigate(from, { replace: true });
|
|
} else {
|
|
setError('root', {
|
|
type: 'manual',
|
|
message: result.error
|
|
});
|
|
}
|
|
};
|
|
|
|
const handleAuthentikLogin = () => {
|
|
// Redirect to Authentik OAuth endpoint
|
|
window.location.href = `${process.env.REACT_APP_API_URL || 'http://localhost:5000'}/api/auth/authentik`;
|
|
};
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<div>
|
|
<h2 className="text-2xl font-bold text-gray-900">Sign in to your account</h2>
|
|
<p className="mt-2 text-sm text-gray-600">
|
|
Or{' '}
|
|
<Link
|
|
to="/register"
|
|
className="font-medium text-primary-600 hover:text-primary-500"
|
|
>
|
|
create a new account
|
|
</Link>
|
|
</p>
|
|
</div>
|
|
|
|
<form className="space-y-6" onSubmit={handleSubmit(onSubmit)}>
|
|
{errors.root && (
|
|
<div className="rounded-md bg-red-50 p-4">
|
|
<div className="flex">
|
|
<div className="ml-3">
|
|
<h3 className="text-sm font-medium text-red-800">
|
|
{errors.root.message}
|
|
</h3>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<div>
|
|
<label htmlFor="email" className="label">
|
|
Email address
|
|
</label>
|
|
<div className="mt-1">
|
|
<input
|
|
id="email"
|
|
type="email"
|
|
autoComplete="email"
|
|
className={errors.email ? 'input-error' : 'input'}
|
|
{...register('email', {
|
|
required: 'Email is required',
|
|
pattern: {
|
|
value: /^\S+@\S+$/i,
|
|
message: 'Invalid email address',
|
|
},
|
|
})}
|
|
/>
|
|
{errors.email && (
|
|
<p className="mt-2 text-sm text-red-600">{errors.email.message}</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="password" className="label">
|
|
Password
|
|
</label>
|
|
<div className="mt-1 relative">
|
|
<input
|
|
id="password"
|
|
type={showPassword ? 'text' : 'password'}
|
|
autoComplete="current-password"
|
|
className={errors.password ? 'input-error pr-10' : 'input pr-10'}
|
|
{...register('password', {
|
|
required: 'Password is required',
|
|
})}
|
|
/>
|
|
<button
|
|
type="button"
|
|
className="absolute inset-y-0 right-0 pr-3 flex items-center"
|
|
onClick={() => setShowPassword(!showPassword)}
|
|
>
|
|
{showPassword ? (
|
|
<EyeSlashIcon className="h-5 w-5 text-gray-400" />
|
|
) : (
|
|
<EyeIcon className="h-5 w-5 text-gray-400" />
|
|
)}
|
|
</button>
|
|
{errors.password && (
|
|
<p className="mt-2 text-sm text-red-600">{errors.password.message}</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center">
|
|
<input
|
|
id="remember-me"
|
|
name="remember-me"
|
|
type="checkbox"
|
|
className="h-4 w-4 text-primary-600 focus:ring-primary-500 border-gray-300 rounded"
|
|
/>
|
|
<label htmlFor="remember-me" className="ml-2 block text-sm text-gray-900">
|
|
Remember me
|
|
</label>
|
|
</div>
|
|
|
|
<div className="text-sm">
|
|
<Link
|
|
to="/forgot-password"
|
|
className="font-medium text-primary-600 hover:text-primary-500"
|
|
>
|
|
Forgot your password?
|
|
</Link>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<button
|
|
type="submit"
|
|
disabled={loading}
|
|
className="btn-primary w-full justify-center"
|
|
>
|
|
{loading ? (
|
|
<LoadingSpinner size="sm" color="white" />
|
|
) : (
|
|
'Sign in'
|
|
)}
|
|
</button>
|
|
</div>
|
|
|
|
<div className="mt-6">
|
|
<div className="relative">
|
|
<div className="absolute inset-0 flex items-center">
|
|
<div className="w-full border-t border-gray-300" />
|
|
</div>
|
|
<div className="relative flex justify-center text-sm">
|
|
<span className="px-2 bg-white text-gray-500">Or continue with</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="mt-6">
|
|
<button
|
|
type="button"
|
|
onClick={handleAuthentikLogin}
|
|
className="w-full inline-flex justify-center py-2 px-4 border border-gray-300 rounded-lg shadow-sm bg-white text-sm font-medium text-gray-500 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500 transition-colors duration-200"
|
|
>
|
|
<svg className="w-5 h-5" viewBox="0 0 24 24" fill="currentColor">
|
|
<path d="M12 2L2 7v10c0 5.55 3.84 9.74 9 11 5.16-1.26 9-5.45 9-11V7l-10-5z"/>
|
|
<path d="M10 17l-4-4 1.41-1.41L10 14.17l6.59-6.59L18 9l-8 8z" fill="white"/>
|
|
</svg>
|
|
<span className="ml-2">Sign in with Authentik</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default Login; |