-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathloginForm.tsx
More file actions
114 lines (106 loc) · 4.63 KB
/
loginForm.tsx
File metadata and controls
114 lines (106 loc) · 4.63 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use client';
import { useMemo } from "react";
import { Card } from "@/components/ui/card";
import { SourcebotLogo } from "@/app/components/sourcebotLogo";
import { AuthMethodSelector } from "@/app/components/authMethodSelector";
import useCaptureEvent from "@/hooks/useCaptureEvent";
import Link from "next/link";
import type { IdentityProviderMetadata } from "@/lib/identityProviders";
interface LoginFormProps {
callbackUrl?: string;
error?: string;
providers: IdentityProviderMetadata[];
context: "login" | "signup";
isAnonymousAccessEnabled?: boolean;
hideSecurityNotice?: boolean;
}
export const LoginForm = ({ callbackUrl, error, providers, context, isAnonymousAccessEnabled = false, hideSecurityNotice = false }: LoginFormProps) => {
const captureEvent = useCaptureEvent();
const safeCallbackUrl = useMemo(() => {
if (!callbackUrl) return "/";
// Allow only relative paths that start with "/" but not "//" (protocol-relative URLs)
if (callbackUrl.startsWith("/") && !callbackUrl.startsWith("//")) {
return callbackUrl;
}
return "/";
}, [callbackUrl]);
const errorMessage = useMemo(() => {
if (!error) {
return "";
}
switch (error) {
case "CredentialsSignin":
return "Invalid email or password. Please try again.";
case "OAuthAccountNotLinked":
return "This email is already associated with a different sign-in method.";
default:
return "An error occurred during authentication. Please try again.";
}
}, [error]);
// Helper function to get the correct analytics event name
const getLoginEventName = (providerId: string) => {
switch (providerId) {
case "github":
return "wa_login_with_github" as const;
case "google":
return "wa_login_with_google" as const;
case "gitlab":
return "wa_login_with_gitlab" as const;
case "okta":
return "wa_login_with_okta" as const;
case "keycloak":
return "wa_login_with_keycloak" as const;
case "microsoft-entra-id":
return "wa_login_with_microsoft_entra_id" as const;
default:
return "wa_login_with_github" as const; // fallback
}
};
// Analytics callback for provider clicks
const handleProviderClick = (providerId: string) => {
captureEvent(getLoginEventName(providerId), {});
};
return (
<div className="flex flex-col items-center justify-center w-full">
<div className="mb-6 flex flex-col items-center">
<SourcebotLogo
className="h-12 sm:h-16 mb-3"
/>
<h2 className="text-lg font-medium text-center">
{context === "login" ? "Sign in to your account" : "Create a new account"}
</h2>
</div>
<Card className="flex flex-col items-center border p-6 sm:p-12 rounded-lg gap-4 sm:gap-6 w-full sm:w-[500px] max-w-[500px] bg-background">
{error && (
<div className="text-sm text-destructive text-center text-wrap border p-2 rounded-md border-destructive">
{errorMessage}
</div>
)}
<AuthMethodSelector
providers={providers}
callbackUrl={callbackUrl}
context={context}
onProviderClick={handleProviderClick}
securityNoticeClosable={true}
hideSecurityNotice={hideSecurityNotice}
/>
<p className="text-sm text-muted-foreground mt-8">
{context === "login" ?
<>
Don't have an account? <Link className="underline" href={callbackUrl ? `/signup?callbackUrl=${encodeURIComponent(callbackUrl)}` : "/signup"}>Sign up</Link>
</>
:
<>
Already have an account? <Link className="underline" href={callbackUrl ? `/login?callbackUrl=${encodeURIComponent(callbackUrl)}` : "/login"}>Sign in</Link>
</>
}
</p>
{isAnonymousAccessEnabled && (
<p className="text-sm text-muted-foreground">
<Link className="underline" href={safeCallbackUrl}>Continue as guest</Link>
</p>
)}
</Card>
</div>
)
}