import { Mail, User } from "lucide-react"; import { useCallback, useState } from "react"; import { toast } from "sonner"; import { useAuthStore } from "@/models/auth"; import { Button } from "./ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "./ui/dialog"; import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel, } from "./ui/field"; import { Input } from "./ui/input"; export interface LoginModalProps extends Omit, "onOpenChange"> { open: boolean; onOpenChange: (open: boolean) => void; } export function LoginModal({ onOpenChange, ...props }: LoginModalProps) { const authStore = useAuthStore(); const [offlineUsername, setOfflineUsername] = useState(""); const [errorMessage, setErrorMessage] = useState(""); const [isLoggingIn, setIsLoggingIn] = useState(false); const handleMicrosoftLogin = useCallback(async () => { setIsLoggingIn(true); authStore.setLoginMode("microsoft"); try { await authStore.loginOnline(() => onOpenChange?.(false)); } catch (error) { const err = error as Error; console.error("Failed to login with Microsoft:", err); setErrorMessage(err.message); } finally { setIsLoggingIn(false); } }, [authStore.loginOnline, authStore.setLoginMode, onOpenChange]); const handleOfflineLogin = useCallback(async () => { setIsLoggingIn(true); try { await authStore.loginOffline(offlineUsername); toast.success("Logged in offline successfully"); onOpenChange?.(false); } catch (error) { const err = error as Error; console.error("Failed to login offline:", err); setErrorMessage(err.message); } finally { setIsLoggingIn(false); } }, [authStore, offlineUsername, onOpenChange]); return ( Login Login to your Minecraft account or play offline
{!authStore.loginMode && (
)} {authStore.loginMode === "microsoft" && (
To sign in, use a web browser to open the page{" "} {authStore.deviceCode?.verificationUri} {" "} and enter the code{" "} { if (authStore.deviceCode?.userCode) { navigator.clipboard?.writeText( authStore.deviceCode?.userCode, ); } }} onKeyDown={() => { if (authStore.deviceCode?.userCode) { navigator.clipboard?.writeText( authStore.deviceCode?.userCode, ); } }} > {authStore.deviceCode?.userCode} {" "} to authenticate, this code will be expired in{" "} {authStore.deviceCode?.expiresIn} seconds. {errorMessage}
)} {authStore.loginMode === "offline" && ( Username Enter a username to play offline { setOfflineUsername(e.target.value); setErrorMessage(""); }} aria-invalid={!!errorMessage} /> {errorMessage} )}
{authStore.statusMessage}
{authStore.loginMode === "offline" && ( )}
); }