2026-02-14 17:15:46 +07:00
|
|
|
import { useState, useEffect } from 'react';
|
|
|
|
|
import { GithubOutlined } from '@ant-design/icons';
|
|
|
|
|
import { useNavigate, Link } from 'react-router';
|
|
|
|
|
import { toast } from 'sonner';
|
|
|
|
|
import { Icon } from '@iconify/react';
|
|
|
|
|
import { useAuthStore } from '../../features/auth/store/auth.store';
|
|
|
|
|
|
|
|
|
|
export default function LoginPage() {
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const login = useAuthStore((state) => state.login);
|
|
|
|
|
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
|
|
|
|
|
|
|
|
|
|
const [isGithubLoading, setIsGithubLoading] = useState(false);
|
|
|
|
|
const [email, setEmail] = useState('');
|
|
|
|
|
const [password, setPassword] = useState('');
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (isAuthenticated) {
|
|
|
|
|
navigate('/');
|
|
|
|
|
}
|
|
|
|
|
}, [isAuthenticated, navigate]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-04-10 14:18:04 +07:00
|
|
|
const hashParams = new URLSearchParams(globalThis.location.hash.substring(1));
|
2026-02-14 17:15:46 +07:00
|
|
|
const urlParams = new URLSearchParams(globalThis.location.search);
|
2026-04-10 14:18:04 +07:00
|
|
|
const accessToken = hashParams.get('access_token') || urlParams.get('access_token');
|
2026-02-14 17:15:46 +07:00
|
|
|
const type = hashParams.get('type') || urlParams.get('type');
|
|
|
|
|
if (accessToken) {
|
|
|
|
|
if (type === 'recovery' || type === 'magiclink' || !type) {
|
|
|
|
|
toast.info('Redirecting to password reset...');
|
|
|
|
|
navigate('/auth/reset-password?access_token=' + accessToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [navigate]);
|
|
|
|
|
|
|
|
|
|
const handleEmailLogin = async (e: React.FormEvent) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
setError(null);
|
|
|
|
|
if (!email || !password) {
|
|
|
|
|
setError('Please enter both email and password');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setIsSubmitting(true);
|
|
|
|
|
try {
|
|
|
|
|
const success = await login(email, password);
|
|
|
|
|
if (success) {
|
|
|
|
|
toast.success('Login successful!');
|
|
|
|
|
navigate('/');
|
|
|
|
|
} else {
|
|
|
|
|
setError('Login failed. Please check your credentials.');
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setError('Login failed. Please try again.');
|
|
|
|
|
} finally {
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleGithubLogin = async () => {
|
|
|
|
|
toast.info('GitHub login coming soon');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2026-04-10 14:18:04 +07:00
|
|
|
<div className="flex justify-center items-center min-h-screen bg-gray-50 dark:bg-gray-950 p-4">
|
|
|
|
|
<div className="bg-white dark:bg-gray-900 w-full max-w-md p-8 rounded-2xl shadow-lg border border-gray-200 dark:border-gray-700">
|
2026-02-14 17:15:46 +07:00
|
|
|
<div className="text-center mb-8">
|
2026-04-10 14:18:04 +07:00
|
|
|
<h2 className="text-3xl font-bold text-gray-900 dark:text-white mb-2">
|
2026-02-14 17:15:46 +07:00
|
|
|
Welcome Back
|
|
|
|
|
</h2>
|
2026-04-10 14:18:04 +07:00
|
|
|
<p className="text-gray-600 dark:text-gray-400 font-sans">
|
|
|
|
|
Sign in to QR Campaign Manager
|
|
|
|
|
</p>
|
2026-02-14 17:15:46 +07:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{error && (
|
2026-04-10 14:18:04 +07:00
|
|
|
<div className="mb-6 p-3 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg">
|
|
|
|
|
<p className="text-red-600 dark:text-red-400 text-sm">{error}</p>
|
2026-02-14 17:15:46 +07:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2026-04-10 14:18:04 +07:00
|
|
|
<form onSubmit={handleEmailLogin} className="space-y-4">
|
2026-02-14 17:15:46 +07:00
|
|
|
<div>
|
2026-04-10 14:18:04 +07:00
|
|
|
<label htmlFor="email" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
|
2026-02-14 17:15:46 +07:00
|
|
|
Email
|
|
|
|
|
</label>
|
|
|
|
|
<input
|
|
|
|
|
id="email"
|
|
|
|
|
type="email"
|
|
|
|
|
value={email}
|
|
|
|
|
onChange={(e) => setEmail(e.target.value)}
|
2026-04-10 14:18:04 +07:00
|
|
|
placeholder="your@email.com"
|
2026-02-14 17:15:46 +07:00
|
|
|
disabled={isSubmitting}
|
2026-04-10 14:18:04 +07:00
|
|
|
className="w-full px-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-transparent bg-white dark:bg-gray-800 text-gray-900 dark:text-white placeholder:text-gray-400 dark:placeholder:text-gray-500 disabled:bg-gray-100 dark:disabled:bg-gray-700 disabled:cursor-not-allowed"
|
2026-02-14 17:15:46 +07:00
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
<div className="flex items-center justify-between mb-1">
|
2026-04-10 14:18:04 +07:00
|
|
|
<label htmlFor="password" className="block text-sm font-medium text-gray-700 dark:text-gray-300">
|
2026-02-14 17:15:46 +07:00
|
|
|
Password
|
|
|
|
|
</label>
|
2026-04-10 14:18:04 +07:00
|
|
|
<Link to="/auth/forgot-password" className="text-sm text-primary-600 dark:text-primary-400 hover:text-primary-700 dark:hover:text-primary-300">
|
2026-02-14 17:15:46 +07:00
|
|
|
Forgot password?
|
|
|
|
|
</Link>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="relative">
|
|
|
|
|
<input
|
|
|
|
|
id="password"
|
|
|
|
|
type={showPassword ? 'text' : 'password'}
|
|
|
|
|
value={password}
|
|
|
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
|
|
|
placeholder="••••••••"
|
|
|
|
|
disabled={isSubmitting}
|
2026-04-10 14:18:04 +07:00
|
|
|
className="w-full px-4 py-2.5 pr-12 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-transparent bg-white dark:bg-gray-800 text-gray-900 dark:text-white placeholder:text-gray-400 dark:placeholder:text-gray-500 disabled:bg-gray-100 dark:disabled:bg-gray-700 disabled:cursor-not-allowed"
|
2026-02-14 17:15:46 +07:00
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setShowPassword(!showPassword)}
|
2026-04-10 14:18:04 +07:00
|
|
|
className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-300"
|
2026-02-14 17:15:46 +07:00
|
|
|
>
|
2026-04-10 14:18:04 +07:00
|
|
|
<Icon icon={showPassword ? 'mdi:eye-off' : 'mdi:eye'} className="text-xl" />
|
2026-02-14 17:15:46 +07:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<button
|
|
|
|
|
type="submit"
|
|
|
|
|
disabled={isSubmitting}
|
2026-04-10 14:18:04 +07:00
|
|
|
className="w-full py-3 bg-primary-600 text-white rounded-lg font-semibold hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-primary-500 focus:ring-offset-2 dark:focus:ring-offset-gray-900 disabled:bg-gray-400 dark:disabled:bg-gray-600 disabled:cursor-not-allowed transition-colors cursor-pointer"
|
2026-02-14 17:15:46 +07:00
|
|
|
>
|
2026-04-10 14:18:04 +07:00
|
|
|
{isSubmitting ? 'Signing in...' : 'Sign in with Email'}
|
2026-02-14 17:15:46 +07:00
|
|
|
</button>
|
|
|
|
|
</form>
|
|
|
|
|
|
2026-04-10 14:18:04 +07:00
|
|
|
<div className="my-6 flex items-center">
|
|
|
|
|
<div className="flex-1 border-t border-gray-300 dark:border-gray-600"></div>
|
|
|
|
|
<span className="px-4 text-sm text-gray-500 dark:text-gray-400">OR</span>
|
|
|
|
|
<div className="flex-1 border-t border-gray-300 dark:border-gray-600"></div>
|
2026-02-14 17:15:46 +07:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<button
|
|
|
|
|
onClick={handleGithubLogin}
|
|
|
|
|
disabled={isGithubLoading}
|
|
|
|
|
type="button"
|
2026-04-10 14:18:04 +07:00
|
|
|
className="w-full py-3 flex items-center justify-center gap-2 bg-gray-100 dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg font-semibold text-gray-900 dark:text-white hover:bg-gray-200 dark:hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-gray-400 focus:ring-offset-2 dark:focus:ring-offset-gray-900 disabled:bg-gray-100 dark:disabled:bg-gray-800 disabled:cursor-not-allowed transition-colors cursor-pointer"
|
2026-02-14 17:15:46 +07:00
|
|
|
>
|
|
|
|
|
<GithubOutlined className="text-xl" />
|
2026-04-10 14:18:04 +07:00
|
|
|
<span>{isGithubLoading ? 'Connecting...' : 'Sign in with GitHub'}</span>
|
2026-02-14 17:15:46 +07:00
|
|
|
</button>
|
|
|
|
|
|
2026-04-10 14:18:04 +07:00
|
|
|
<p className="mt-3 text-xs text-center text-gray-500 dark:text-gray-500 font-sans">
|
2026-02-14 17:15:46 +07:00
|
|
|
Make sure your GitHub email is{' '}
|
2026-04-10 14:18:04 +07:00
|
|
|
<a href="https://github.com/settings/emails" target="_blank" rel="noopener noreferrer" className="text-primary-600 dark:text-primary-400 hover:underline">
|
2026-02-14 17:15:46 +07:00
|
|
|
set to public
|
|
|
|
|
</a>{' '}
|
|
|
|
|
for GitHub sign in to work.
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<div className="mt-6 text-center">
|
2026-04-10 14:18:04 +07:00
|
|
|
<p className="text-gray-600 dark:text-gray-400 text-sm">
|
2026-02-14 17:15:46 +07:00
|
|
|
Don't have an account?{' '}
|
2026-04-10 14:18:04 +07:00
|
|
|
<Link to="/auth/signup" className="text-primary-600 dark:text-primary-400 hover:text-primary-700 dark:hover:text-primary-300 font-semibold">
|
2026-02-14 17:15:46 +07:00
|
|
|
Sign up
|
|
|
|
|
</Link>
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="mt-6 text-center">
|
2026-04-10 14:18:04 +07:00
|
|
|
<p className="text-gray-500 dark:text-gray-500 text-xs">
|
2026-02-14 17:15:46 +07:00
|
|
|
By signing in, you agree to our Terms of Service and Privacy Policy
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|