Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | 2x 84x 84x 84x 84x 84x 84x 84x 84x 84x 84x 84x 84x 84x 11x 11x 6x 3x 3x 5x 2x 2x 84x 5x 5x 2x 84x 2x 84x 15x 3x 1x 69x 2x 8x 8x 3x 7x 7x 1x 1x | // src/pages/admin/components/AuthView.tsx
import React, { useState } from 'react';
import type { UserProfile } from '../../../types';
import { notifySuccess } from '../../../services/notificationService';
import { LoadingSpinner } from '../../../components/LoadingSpinner';
import { GoogleIcon } from '../../../components/icons/GoogleIcon';
import { GithubIcon } from '../../../components/icons/GithubIcon';
import { PasswordInput } from '../../../components/PasswordInput';
import {
useLoginMutation,
useRegisterMutation,
usePasswordResetRequestMutation,
} from '../../../hooks/mutations/useAuthMutations';
interface AuthViewProps {
onLoginSuccess: (user: UserProfile, token: string, rememberMe: boolean) => void;
onClose: () => void;
}
export const AuthView: React.FC<AuthViewProps> = ({ onLoginSuccess, onClose }) => {
const [isRegistering, setIsRegistering] = useState(false);
const [authEmail, setAuthEmail] = useState('');
const [authPassword, setAuthPassword] = useState('');
const [authFullName, setAuthFullName] = useState('');
const [isForgotPassword, setIsForgotPassword] = useState(false);
const [rememberMe, setRememberMe] = useState(false);
const loginMutation = useLoginMutation();
const registerMutation = useRegisterMutation();
const passwordResetMutation = usePasswordResetRequestMutation();
const loginLoading = loginMutation.isPending;
const registerLoading = registerMutation.isPending;
const passwordResetLoading = passwordResetMutation.isPending;
const handleAuthSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (isRegistering) {
registerMutation.mutate(
{ email: authEmail, password: authPassword, fullName: authFullName },
{
onSuccess: (authResult) => {
onLoginSuccess(authResult.userprofile, authResult.token, rememberMe);
onClose();
},
},
);
} else {
loginMutation.mutate(
{ email: authEmail, password: authPassword, rememberMe },
{
onSuccess: (authResult) => {
onLoginSuccess(authResult.userprofile, authResult.token, rememberMe);
onClose();
},
},
);
}
};
const handlePasswordResetRequest = async (e: React.FormEvent) => {
e.preventDefault();
passwordResetMutation.mutate(
{ email: authEmail },
{
onSuccess: (result) => {
notifySuccess(result.message);
},
},
);
};
const handleOAuthSignIn = (provider: 'google' | 'github') => {
window.location.href = '/api/auth/' + provider;
};
if (isForgotPassword) {
return (
<div className="p-8">
<h2 className="text-2xl font-bold text-gray-800 dark:text-white mb-1">Reset Password</h2>
<p className="text-sm text-gray-500 dark:text-gray-400 mb-6">
Enter your email to receive a password reset link.
</p>
<form
data-testid="reset-password-form"
onSubmit={handlePasswordResetRequest}
className="space-y-4"
>
<div>
<label
htmlFor="resetEmail"
className="block text-sm font-medium text-gray-700 dark:text-gray-300"
>
Email Address
</label>
<input
id="resetEmail"
type="email"
value={authEmail}
onChange={(e) => setAuthEmail(e.target.value)}
required
className="mt-1 block w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm"
disabled={passwordResetLoading}
/>
</div>
<div className="pt-2">
<button
type="submit"
disabled={passwordResetLoading}
className="w-full bg-brand-primary hover:bg-brand-dark disabled:bg-gray-400 text-white font-bold py-2.5 px-4 rounded-lg flex justify-center shadow-sm transition-colors"
>
{passwordResetLoading ? (
<div className="w-5 h-5">
<LoadingSpinner />
</div>
) : (
'Send Reset Link'
)}
</button>
</div>
</form>
<div className="text-center mt-4">
<button
onClick={() => {
setIsForgotPassword(false);
}}
className="text-sm font-medium text-brand-primary hover:text-brand-dark dark:hover:text-brand-light underline"
>
Back to Sign In
</button>
</div>
</div>
);
}
return (
<div className="p-8">
<h2 className="text-2xl font-bold text-gray-800 dark:text-white mb-1">
{isRegistering ? 'Create an Account' : 'Sign In'}
</h2>
<p className="text-sm text-gray-500 dark:text-gray-400 mb-6">
{isRegistering ? 'to get started.' : 'to access your account.'}
</p>
{isRegistering && (
<div className="space-y-4 mb-4">
<div>
<label
htmlFor="authFullName"
className="block text-sm font-medium text-gray-700 dark:text-gray-300"
>
Full Name
</label>
<input
id="authFullName"
type="text"
value={authFullName}
onChange={(e) => setAuthFullName(e.target.value)}
className="mt-1 block w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm"
placeholder="Optional"
/>
</div>
</div>
)}
<form data-testid="auth-form" onSubmit={handleAuthSubmit} className="space-y-4">
<div>
<label
htmlFor="authEmail"
className="block text-sm font-medium text-gray-700 dark:text-gray-300"
>
Email Address
</label>
<input
id="authEmail"
type="email"
value={authEmail}
onChange={(e) => setAuthEmail(e.target.value)}
required
className="mt-1 block w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-brand-primary focus:border-brand-primary sm:text-sm"
disabled={loginLoading || registerLoading}
/>
</div>
<div>
<label
htmlFor="authPassword"
className="block text-sm font-medium text-gray-700 dark:text-gray-300"
>
Password
</label>
<PasswordInput
id="authPassword"
value={authPassword}
onChange={(e) => setAuthPassword(e.target.value)}
required
className="mt-1"
disabled={loginLoading || registerLoading}
showStrength={isRegistering}
/>
</div>
{!isRegistering && (
<div className="flex items-center justify-between text-sm">
<div className="flex items-center">
<input
id="remember-me"
name="remember-me"
type="checkbox"
checked={rememberMe}
onChange={(e) => setRememberMe(e.target.checked)}
className="h-4 w-4 text-brand-primary border-gray-300 rounded focus:ring-brand-secondary"
/>
<label
htmlFor="remember-me"
className="ml-2 block text-sm text-gray-900 dark:text-gray-300"
>
Remember me
</label>
</div>
<button
type="button"
onClick={() => setIsForgotPassword(true)}
className="font-medium text-brand-primary hover:text-brand-dark dark:hover:text-brand-light underline"
>
Forgot password?
</button>
</div>
)}
<div className="pt-2">
<button
type="submit"
disabled={loginLoading || registerLoading}
className="w-full bg-brand-primary hover:bg-brand-dark disabled:bg-gray-400 text-white font-bold py-2.5 px-4 rounded-lg flex justify-center shadow-sm transition-colors"
>
{loginLoading || registerLoading ? (
<div className="w-5 h-5">
<LoadingSpinner />
</div>
) : isRegistering ? (
'Register'
) : (
'Sign In'
)}
</button>
</div>
</form>
<div className="mt-4">
<button
onClick={() => {
setIsRegistering(!isRegistering);
}}
className="w-full text-center px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm text-sm font-medium text-gray-700 dark:text-gray-200 bg-white dark:bg-gray-800 hover:bg-gray-50 dark:hover:bg-gray-700"
>
{isRegistering ? 'Already have an account? Sign In' : "Don't have an account? Register"}
</button>
</div>
<div className="relative my-6">
<div className="absolute inset-0 flex items-center" aria-hidden="true">
<div className="w-full border-t border-gray-200 dark:border-gray-700" />
</div>
<div className="relative flex justify-center">
<span className="bg-white dark:bg-gray-800 px-3 text-sm text-gray-500 dark:text-gray-400">
Or continue with
</span>
</div>
</div>
<div className="space-y-3">
<button
type="button"
onClick={() => handleOAuthSignIn('google')}
disabled={loginLoading || registerLoading}
className="w-full flex items-center justify-center px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm text-sm font-medium text-gray-700 dark:text-gray-200 bg-white dark:bg-gray-800 hover:bg-gray-50 dark:hover:bg-gray-700 disabled:opacity-50 disabled:cursor-not-allowed"
>
{loginLoading || registerLoading ? (
<div className="w-5 h-5 mr-3">
<LoadingSpinner />
</div>
) : (
<GoogleIcon className="w-5 h-5 mr-3" />
)}
Sign In with Google
</button>
<button
type="button"
onClick={() => handleOAuthSignIn('github')}
disabled={loginLoading || registerLoading}
className="w-full flex items-center justify-center px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm text-sm font-medium text-gray-700 dark:text-gray-200 bg-white dark:bg-gray-800 hover:bg-gray-50 dark:hover:bg-gray-700 disabled:opacity-50 disabled:cursor-not-allowed"
>
{loginLoading || registerLoading ? (
<div className="w-5 h-5 mr-3">
<LoadingSpinner />
</div>
) : (
<GithubIcon className="w-5 h-5 mr-3" />
)}
Sign In with GitHub
</button>
</div>
</div>
);
};
|