Allowed email/username login
Browse filesX-Lovable-Edit-ID: edt-a609ed1b-d4b7-4306-8828-26eed7aa7494
- src/routes/auth.tsx +15 -4
- src/server/auth.functions.ts +41 -0
src/routes/auth.tsx
CHANGED
|
@@ -6,6 +6,8 @@ import { Input } from "@/components/ui/input";
|
|
| 6 |
import { Label } from "@/components/ui/label";
|
| 7 |
import { toast } from "sonner";
|
| 8 |
import { Radar, ShieldAlert } from "lucide-react";
|
|
|
|
|
|
|
| 9 |
|
| 10 |
export const Route = createFileRoute("/auth")({
|
| 11 |
component: AuthPage,
|
|
@@ -17,6 +19,7 @@ function AuthPage() {
|
|
| 17 |
const [username, setUsername] = useState("");
|
| 18 |
const [password, setPassword] = useState("");
|
| 19 |
const [loading, setLoading] = useState(false);
|
|
|
|
| 20 |
|
| 21 |
// Map username -> synthetic email for Supabase (which requires email auth)
|
| 22 |
const USERNAME_DOMAIN = "drivecore.local";
|
|
@@ -33,11 +36,18 @@ function AuthPage() {
|
|
| 33 |
setLoading(true);
|
| 34 |
try {
|
| 35 |
const cleanUser = username.trim().toLowerCase();
|
| 36 |
-
if (!
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
throw new Error("Username must be 3–32 chars: letters, numbers, underscore.");
|
| 38 |
}
|
| 39 |
-
const email = toEmail(cleanUser);
|
| 40 |
if (mode === "signup") {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
const { error } = await supabase.auth.signUp({
|
| 42 |
email,
|
| 43 |
password,
|
|
@@ -49,6 +59,7 @@ function AuthPage() {
|
|
| 49 |
if (error) throw error;
|
| 50 |
toast.success("Account created. Signing you in…");
|
| 51 |
} else {
|
|
|
|
| 52 |
const { error } = await supabase.auth.signInWithPassword({ email, password });
|
| 53 |
if (error) throw error;
|
| 54 |
}
|
|
@@ -99,7 +110,7 @@ function AuthPage() {
|
|
| 99 |
</div>
|
| 100 |
|
| 101 |
<div className="space-y-2">
|
| 102 |
-
<Label htmlFor="username">Username</Label>
|
| 103 |
<Input
|
| 104 |
id="username"
|
| 105 |
type="text"
|
|
@@ -107,7 +118,7 @@ function AuthPage() {
|
|
| 107 |
autoComplete="username"
|
| 108 |
value={username}
|
| 109 |
onChange={(e) => setUsername(e.target.value)}
|
| 110 |
-
placeholder="operator_01"
|
| 111 |
/>
|
| 112 |
</div>
|
| 113 |
<div className="space-y-2">
|
|
|
|
| 6 |
import { Label } from "@/components/ui/label";
|
| 7 |
import { toast } from "sonner";
|
| 8 |
import { Radar, ShieldAlert } from "lucide-react";
|
| 9 |
+
import { useServerFn } from "@tanstack/react-start";
|
| 10 |
+
import { resolveLoginIdentifier } from "@/server/auth.functions";
|
| 11 |
|
| 12 |
export const Route = createFileRoute("/auth")({
|
| 13 |
component: AuthPage,
|
|
|
|
| 19 |
const [username, setUsername] = useState("");
|
| 20 |
const [password, setPassword] = useState("");
|
| 21 |
const [loading, setLoading] = useState(false);
|
| 22 |
+
const resolveIdentifier = useServerFn(resolveLoginIdentifier);
|
| 23 |
|
| 24 |
// Map username -> synthetic email for Supabase (which requires email auth)
|
| 25 |
const USERNAME_DOMAIN = "drivecore.local";
|
|
|
|
| 36 |
setLoading(true);
|
| 37 |
try {
|
| 38 |
const cleanUser = username.trim().toLowerCase();
|
| 39 |
+
if (!cleanUser) {
|
| 40 |
+
throw new Error("Enter your username or email.");
|
| 41 |
+
}
|
| 42 |
+
const isEmailInput = cleanUser.includes("@");
|
| 43 |
+
if (!isEmailInput && !/^[a-z0-9_]{3,32}$/.test(cleanUser)) {
|
| 44 |
throw new Error("Username must be 3–32 chars: letters, numbers, underscore.");
|
| 45 |
}
|
|
|
|
| 46 |
if (mode === "signup") {
|
| 47 |
+
if (isEmailInput) {
|
| 48 |
+
throw new Error("Use a username when creating a new account.");
|
| 49 |
+
}
|
| 50 |
+
const email = toEmail(cleanUser);
|
| 51 |
const { error } = await supabase.auth.signUp({
|
| 52 |
email,
|
| 53 |
password,
|
|
|
|
| 59 |
if (error) throw error;
|
| 60 |
toast.success("Account created. Signing you in…");
|
| 61 |
} else {
|
| 62 |
+
const { email } = await resolveIdentifier({ data: { identifier: cleanUser } });
|
| 63 |
const { error } = await supabase.auth.signInWithPassword({ email, password });
|
| 64 |
if (error) throw error;
|
| 65 |
}
|
|
|
|
| 110 |
</div>
|
| 111 |
|
| 112 |
<div className="space-y-2">
|
| 113 |
+
<Label htmlFor="username">Username or email</Label>
|
| 114 |
<Input
|
| 115 |
id="username"
|
| 116 |
type="text"
|
|
|
|
| 118 |
autoComplete="username"
|
| 119 |
value={username}
|
| 120 |
onChange={(e) => setUsername(e.target.value)}
|
| 121 |
+
placeholder="operator_01 or you@example.com"
|
| 122 |
/>
|
| 123 |
</div>
|
| 124 |
<div className="space-y-2">
|
src/server/auth.functions.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { createServerFn } from "@tanstack/react-start";
|
| 2 |
+
import { z } from "zod";
|
| 3 |
+
import { supabaseAdmin } from "@/integrations/supabase/client.server";
|
| 4 |
+
|
| 5 |
+
const ResolveLoginIdentifierSchema = z.object({
|
| 6 |
+
identifier: z.string().trim().min(1).max(128),
|
| 7 |
+
});
|
| 8 |
+
|
| 9 |
+
const USERNAME_DOMAIN = "drivecore.local";
|
| 10 |
+
|
| 11 |
+
export const resolveLoginIdentifier = createServerFn({ method: "POST" })
|
| 12 |
+
.inputValidator((data) => ResolveLoginIdentifierSchema.parse(data))
|
| 13 |
+
.handler(async ({ data }) => {
|
| 14 |
+
const identifier = data.identifier.trim();
|
| 15 |
+
|
| 16 |
+
if (identifier.includes("@")) {
|
| 17 |
+
return { email: identifier.toLowerCase() };
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
const normalized = identifier.toLowerCase();
|
| 21 |
+
|
| 22 |
+
const { data: profile, error: profileError } = await supabaseAdmin
|
| 23 |
+
.from("profiles")
|
| 24 |
+
.select("id, display_name")
|
| 25 |
+
.ilike("display_name", normalized)
|
| 26 |
+
.limit(1)
|
| 27 |
+
.maybeSingle();
|
| 28 |
+
|
| 29 |
+
if (profileError) {
|
| 30 |
+
throw new Error("Could not verify account details.");
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
if (profile?.id) {
|
| 34 |
+
const { data: userResult, error: userError } = await supabaseAdmin.auth.admin.getUserById(profile.id);
|
| 35 |
+
if (!userError && userResult.user?.email) {
|
| 36 |
+
return { email: userResult.user.email.toLowerCase() };
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
return { email: `${normalized}@${USERNAME_DOMAIN}` };
|
| 41 |
+
});
|