feat: improve login

This commit is contained in:
monoid 2025-05-11 00:24:52 +09:00
parent 94cf46e7f8
commit 68b761fbd5
2 changed files with 115 additions and 39 deletions

View file

@ -5,43 +5,110 @@ import { Label } from "@/components/ui/label.tsx";
import { doLogin } from "@/state/user.ts"; import { doLogin } from "@/state/user.ts";
import { useState } from "react"; import { useState } from "react";
import { useLocation } from "wouter"; import { useLocation } from "wouter";
import { Eye, EyeOff } from "lucide-react";
import { LogInIcon, User, Loader2 } from "lucide-react";
export function LoginForm() { export function LoginForm() {
const [username, setUsername] = useState(""); const [username, setUsername] = useState("");
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [showPassword, setShowPassword] = useState(false);
const [, setLocation] = useLocation(); const [, setLocation] = useLocation();
const handleLogin = async () => {
if (!username || !password) {
alert("사용자 이름과 비밀번호를 입력해주세요.");
return;
}
setIsLoading(true);
try {
const result = await doLogin({
username,
password,
});
if (typeof result === "string") {
alert(result);
} else {
setLocation("/");
}
} catch (error) {
alert("로그인 중 오류가 발생했습니다.");
console.error(error);
} finally {
setIsLoading(false);
}
};
return ( return (
<Card className="w-full max-w-sm"> <Card className="w-full max-w-md shadow-lg rounded-xl">
<CardHeader> <CardHeader className="space-y-2">
<CardTitle className="text-2xl">Login</CardTitle> <CardTitle className="text-2xl inline-flex items-center gap-2">
<span className="inline-flex items-center justify-center w-12 h-12 rounded-full bg-primary/10">
<LogInIcon className="w-6 h-6 text-primary" />
</span>
Login
</CardTitle>
<CardDescription> <CardDescription>
Enter your email below to login to your account. Enter your email below to login to your account.
</CardDescription> </CardDescription>
</CardHeader> </CardHeader>
<CardContent className="grid gap-4"> <CardContent className="space-y-4">
<div className="grid gap-2"> <div className="space-y-2">
<Label htmlFor="username">Username</Label> <Label htmlFor="username" className="flex items-center gap-2">
<Input id="username" type="text" placeholder="username" required value={username} onChange={e=> setUsername(e.target.value)}/> <User className="w-4 h-4" />
Username
</Label>
<Input id="username"
type="text"
placeholder="username"
required
value={username}
onChange={e => setUsername(e.target.value)}
/>
</div> </div>
<div className="grid gap-2"> <div className="space-y-2">
<Label htmlFor="password">Password</Label> <Label htmlFor="password">Password</Label>
<Input id="password" type="password" required value={password} onChange={e=> setPassword(e.target.value)}/> <div className="relative">
<Input
id="password"
type={showPassword ? "text" : "password"}
required
value={password}
onChange={e => setPassword(e.target.value)}
/>
<button
type="button"
className="absolute right-2 top-1/2 transform -translate-y-1/2 text-gray-500 hover:text-gray-700"
onClick={() => setShowPassword(!showPassword)}
aria-label={showPassword ? "비밀번호 숨기기" : "비밀번호 표시하기"}
>
{showPassword ? (
<EyeOff className="h-4 w-4" />
) : (
<Eye className="h-4 w-4" />
)}
</button>
</div>
</div> </div>
</CardContent> </CardContent>
<CardFooter> <CardFooter>
<Button className="w-full" onClick={()=>{ <Button
doLogin({ className="w-full font-semibold transition-all"
username, size="lg"
password, disabled={isLoading}
}).then((r)=>{ onClick={handleLogin}
if (typeof r === "string") { >
alert(r); {isLoading ? (
} else { <>
setLocation("/"); <Loader2 className="w-4 h-4 mr-2 animate-spin" />
} Logging in...
}) </>
}}>Sign in</Button> ) : (
<>Sign In</>
)}
</Button>
</CardFooter> </CardFooter>
</Card> </Card>
) )
@ -49,9 +116,12 @@ export function LoginForm() {
export function LoginPage() { export function LoginPage() {
return ( return (
<div className="flex items-center justify-center h-screen"> <div className="flex items-center justify-center min-h-screen
p-4">
<div className="w-full max-w-md">
<LoginForm /> <LoginForm />
</div> </div>
</div>
) )
} }

View file

@ -97,6 +97,12 @@ export const doLogin = async (userLoginInfo: LoginRequest): Promise<string | Log
return b; return b;
}; };
Object.assign(window, {
doLogin,
doLogout,
refresh,
});
export const doResetPassword = async (username: string, oldpassword: string, newpassword: string) => { export const doResetPassword = async (username: string, oldpassword: string, newpassword: string) => {
const u = makeApiUrl("/api/user/reset"); const u = makeApiUrl("/api/user/reset");
const res = await fetch(u, { const res = await fetch(u, {