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
|
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<React.ComponentPropsWithoutRef<typeof Dialog>, "onOpenChange"> {
open: boolean;
onOpenChange: (open: boolean) => void;
}
export function LoginModal({ onOpenChange, ...props }: LoginModalProps) {
const authStore = useAuthStore();
const [offlineUsername, setOfflineUsername] = useState<string>("");
const [errorMessage, setErrorMessage] = useState<string>("");
const [isLoggingIn, setIsLoggingIn] = useState<boolean>(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 (
<Dialog onOpenChange={onOpenChange} {...props}>
<DialogContent className="md:max-w-md">
<DialogHeader>
<DialogTitle>Login</DialogTitle>
<DialogDescription>
Login to your Minecraft account or play offline
</DialogDescription>
</DialogHeader>
<div className="p-4 w-full overflow-hidden">
{!authStore.loginMode && (
<div className="flex flex-col space-y-4">
<Button size="lg" onClick={handleMicrosoftLogin}>
<Mail />
Login with Microsoft
</Button>
<Button
variant="secondary"
onClick={() => authStore.setLoginMode("offline")}
size="lg"
>
<User />
Login Offline
</Button>
</div>
)}
{authStore.loginMode === "microsoft" && (
<div className="flex flex-col space-y-4">
<button
type="button"
className="text-4xl font-bold text-center bg-accent p-4 cursor-pointer"
onClick={() => {
if (authStore.deviceCode?.userCode) {
navigator.clipboard?.writeText(
authStore.deviceCode?.userCode,
);
toast.success("Copied to clipboard");
}
}}
>
{authStore.deviceCode?.userCode}
</button>
<span className="text-muted-foreground w-full overflow-hidden text-ellipsis">
To sign in, use a web browser to open the page{" "}
<a href={authStore.deviceCode?.verificationUri}>
{authStore.deviceCode?.verificationUri}
</a>{" "}
and enter the code{" "}
<code
className="font-semibold cursor-pointer"
onClick={() => {
if (authStore.deviceCode?.userCode) {
navigator.clipboard?.writeText(
authStore.deviceCode?.userCode,
);
}
}}
onKeyDown={() => {
if (authStore.deviceCode?.userCode) {
navigator.clipboard?.writeText(
authStore.deviceCode?.userCode,
);
}
}}
>
{authStore.deviceCode?.userCode}
</code>{" "}
to authenticate, this code will be expired in{" "}
{authStore.deviceCode?.expiresIn} seconds.
</span>
<FieldError>{errorMessage}</FieldError>
</div>
)}
{authStore.loginMode === "offline" && (
<FieldGroup>
<Field>
<FieldLabel>Username</FieldLabel>
<FieldDescription>
Enter a username to play offline
</FieldDescription>
<Input
value={offlineUsername}
onChange={(e) => {
setOfflineUsername(e.target.value);
setErrorMessage("");
}}
aria-invalid={!!errorMessage}
/>
<FieldError>{errorMessage}</FieldError>
</Field>
</FieldGroup>
)}
</div>
<DialogFooter>
<div className="flex flex-col justify-center items-center">
<span className="text-xs text-muted-foreground ">
{authStore.statusMessage}
</span>
</div>
<Button
variant="outline"
onClick={() => {
if (authStore.loginMode) {
if (authStore.loginMode === "microsoft") {
authStore.cancelLoginOnline();
}
authStore.setLoginMode(null);
} else {
onOpenChange?.(false);
}
}}
>
Cancel
</Button>
{authStore.loginMode === "offline" && (
<Button onClick={handleOfflineLogin} disabled={isLoggingIn}>
Login
</Button>
)}
</DialogFooter>
</DialogContent>
</Dialog>
);
}
|