-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathlogin-dialog.tsx
More file actions
100 lines (93 loc) · 3.13 KB
/
login-dialog.tsx
File metadata and controls
100 lines (93 loc) · 3.13 KB
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
import {
Button,
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@repo/ui";
import * as React from "react";
import { AuthForm } from "./auth-form";
/**
* LoginDialog component - Renders login form in a dialog.
* Self-contained with trigger button. For programmatic control, use useLoginDialog hook.
*
* NOTE: This component doesn't handle post-auth navigation since it's typically used
* in contexts where the user should stay on the current page after signing in.
*/
export function LoginDialog() {
const [open, setOpen] = React.useState(false);
const handleSuccess = () => {
// Close modal on success - navigation not needed as user typically wants
// to continue on the same page that triggered the login requirement.
// React Query will automatically refetch protected data after auth state changes.
setOpen(false);
// TODO: Consider invalidating specific queries if immediate data refresh needed:
// queryClient.invalidateQueries({ queryKey: ["protected-data"] })
};
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="outline">Sign In</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-md">
<DialogHeader className="sr-only">
<DialogTitle>Sign in to your account</DialogTitle>
<DialogDescription>
Choose your preferred sign in method
</DialogDescription>
</DialogHeader>
<AuthForm
variant="modal"
mode="login"
onSuccess={handleSuccess}
showTerms={true} // Override default (false for modals) to show terms
/>
</DialogContent>
</Dialog>
);
}
/**
* Hook for programmatically controlling a login dialog.
* Returns dialog component and control functions.
*
* USAGE: Place LoginDialog component at app root, then call openLoginDialog()
* from any component that detects auth is required (e.g., after 401 response).
*
* WARNING: Only one instance should be mounted to avoid conflicts.
*/
export function useLoginDialog() {
const [open, setOpen] = React.useState(false);
const LoginDialog = React.useCallback(() => {
const handleSuccess = () => {
setOpen(false);
// Post-auth behavior handled by React Query's automatic refetching
// and the auth error boundary's session invalidation
};
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="sm:max-w-md">
<DialogHeader className="sr-only">
<DialogTitle>Sign in to your account</DialogTitle>
<DialogDescription>
Choose your preferred sign in method
</DialogDescription>
</DialogHeader>
<AuthForm
variant="modal"
mode="login"
onSuccess={handleSuccess}
// showTerms defaults to false for modals per AuthForm implementation
/>
</DialogContent>
</Dialog>
);
}, [open]);
return {
LoginDialog,
openLoginDialog: () => setOpen(true),
closeLoginDialog: () => setOpen(false),
isOpen: open,
};
}