fix(auth): clear login credentials and form state after logout
This commit is contained in:
parent
7d941687f6
commit
7be59fe18e
18
src/App.tsx
18
src/App.tsx
|
|
@ -59,9 +59,6 @@ export default function App() {
|
|||
);
|
||||
|
||||
// Admin Login State
|
||||
const [adminEmail, setAdminEmail] = useState('');
|
||||
const [adminPassword, setAdminPassword] = useState('');
|
||||
const [showAdminPassword, setShowAdminPassword] = useState(false);
|
||||
const [adminAuthLoading, setAdminAuthLoading] = useState(false);
|
||||
const [adminAuthError, setAdminAuthError] = useState<string | null>(null);
|
||||
|
||||
|
|
@ -274,14 +271,13 @@ export default function App() {
|
|||
};
|
||||
|
||||
// Auth Handlers
|
||||
const handleAdminLogin = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
const handleAdminLogin = async (credentials: { email: string; password: string }) => {
|
||||
setAdminAuthLoading(true);
|
||||
setAdminAuthError(null);
|
||||
try {
|
||||
const res = await axios.post(`${API_BASE}/api/auth/login`, {
|
||||
email: adminEmail,
|
||||
password: adminPassword,
|
||||
email: credentials.email,
|
||||
password: credentials.password,
|
||||
});
|
||||
const token = res.data.access_token;
|
||||
localStorage.setItem('access_token', token);
|
||||
|
|
@ -298,6 +294,7 @@ export default function App() {
|
|||
localStorage.removeItem('access_token');
|
||||
setUserToken(null);
|
||||
setLoggedInUser(null);
|
||||
setAdminAuthError(null);
|
||||
};
|
||||
|
||||
// Force Sync Gateway Handler
|
||||
|
|
@ -936,17 +933,12 @@ export default function App() {
|
|||
if (!loggedInUser || !isAdmin) {
|
||||
return (
|
||||
<AdminLoginForm
|
||||
email={adminEmail}
|
||||
password={adminPassword}
|
||||
showPassword={showAdminPassword}
|
||||
loading={adminAuthLoading}
|
||||
error={adminAuthError}
|
||||
loggedInUser={loggedInUser}
|
||||
isAdmin={isAdmin}
|
||||
onEmailChange={setAdminEmail}
|
||||
onPasswordChange={setAdminPassword}
|
||||
onToggleShowPassword={() => setShowAdminPassword(!showAdminPassword)}
|
||||
onSubmit={handleAdminLogin}
|
||||
onLogout={handleLogout}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,33 +1,32 @@
|
|||
import React from 'react';
|
||||
import React, { useState } from 'react';
|
||||
import { Sparkles, AlertCircle, Shield, Eye, EyeOff } from 'lucide-react';
|
||||
|
||||
interface AdminLoginFormProps {
|
||||
email: string;
|
||||
password: string;
|
||||
showPassword: boolean;
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
loggedInUser: any;
|
||||
isAdmin: boolean;
|
||||
onEmailChange: (v: string) => void;
|
||||
onPasswordChange: (v: string) => void;
|
||||
onToggleShowPassword: () => void;
|
||||
onSubmit: (e: React.FormEvent) => void;
|
||||
onSubmit: (credentials: { email: string; password: string }) => void;
|
||||
onLogout?: () => void;
|
||||
}
|
||||
|
||||
export const AdminLoginForm: React.FC<AdminLoginFormProps> = ({
|
||||
email,
|
||||
password,
|
||||
showPassword,
|
||||
loading,
|
||||
error,
|
||||
loggedInUser,
|
||||
isAdmin,
|
||||
onEmailChange,
|
||||
onPasswordChange,
|
||||
onToggleShowPassword,
|
||||
onSubmit,
|
||||
onLogout,
|
||||
}) => {
|
||||
const [email, setEmail] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [showPassword, setShowPassword] = useState(false);
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
onSubmit({ email, password });
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-[#08080C] text-slate-100 flex items-center justify-center p-4 selection:bg-indigo-500 selection:text-white">
|
||||
<div className="w-full max-w-md bg-slate-900/90 border border-slate-800 rounded-3xl p-8 shadow-2xl backdrop-blur-xl space-y-6">
|
||||
|
|
@ -47,21 +46,33 @@ export const AdminLoginForm: React.FC<AdminLoginFormProps> = ({
|
|||
)}
|
||||
|
||||
{loggedInUser && !isAdmin && (
|
||||
<div className="p-3 bg-amber-500/10 border border-amber-500/30 text-amber-400 text-xs rounded-xl font-bold flex items-center gap-2">
|
||||
<Shield className="h-4 w-4 shrink-0" />
|
||||
<span>Account {loggedInUser.email} lacks administrative privileges.</span>
|
||||
<div className="p-3 bg-amber-500/10 border border-amber-500/30 text-amber-400 text-xs rounded-xl font-bold flex flex-col gap-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<Shield className="h-4 w-4 shrink-0" />
|
||||
<span>Account {loggedInUser.email} lacks administrative privileges.</span>
|
||||
</div>
|
||||
{onLogout && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={onLogout}
|
||||
className="text-[11px] text-amber-300 hover:text-white underline text-left cursor-pointer"
|
||||
>
|
||||
Log out
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={onSubmit} className="space-y-4">
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div className="space-y-1">
|
||||
<label className="text-[10px] uppercase font-black tracking-wider text-slate-400">Admin Email</label>
|
||||
<input
|
||||
type="email"
|
||||
required
|
||||
value={email}
|
||||
onChange={(e) => onEmailChange(e.target.value)}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
placeholder="admin@acupedia.in"
|
||||
autoComplete="username"
|
||||
className="w-full px-4 py-3 rounded-xl bg-slate-800/80 border border-slate-700 text-white text-xs font-semibold focus:outline-none focus:border-indigo-500"
|
||||
/>
|
||||
</div>
|
||||
|
|
@ -73,13 +84,14 @@ export const AdminLoginForm: React.FC<AdminLoginFormProps> = ({
|
|||
type={showPassword ? 'text' : 'password'}
|
||||
required
|
||||
value={password}
|
||||
onChange={(e) => onPasswordChange(e.target.value)}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
placeholder="••••••••"
|
||||
autoComplete="current-password"
|
||||
className="w-full px-4 py-3 rounded-xl bg-slate-800/80 border border-slate-700 text-white text-xs font-semibold focus:outline-none focus:border-indigo-500"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={onToggleShowPassword}
|
||||
onClick={() => setShowPassword(!showPassword)}
|
||||
className="absolute right-3 top-3 text-slate-400 hover:text-white cursor-pointer"
|
||||
>
|
||||
{showPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
|
||||
|
|
|
|||
Loading…
Reference in New Issue