Spaces:
Sleeping
Sleeping
Refactor LoginPage to use Suspense for loading state and improve auth state handling in ExtensionBridge
Browse files
web/app/auth/login/page.tsx
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
"use client";
|
| 2 |
|
| 3 |
-
import { useState, useEffect } from "react";
|
| 4 |
import { createClient } from "@/lib/supabase/client";
|
| 5 |
import { getBaseUrl } from "@/lib/auth-url";
|
| 6 |
import Link from "next/link";
|
| 7 |
import { useSearchParams } from "next/navigation";
|
| 8 |
import { ArrowLeft, Mail, Loader2 } from "lucide-react";
|
| 9 |
|
| 10 |
-
|
| 11 |
const [email, setEmail] = useState("");
|
| 12 |
const [password, setPassword] = useState("");
|
| 13 |
const [error, setError] = useState("");
|
|
@@ -24,7 +24,7 @@ export default function LoginPage() {
|
|
| 24 |
if (user) { window.location.href = next; }
|
| 25 |
else { setChecking(false); }
|
| 26 |
});
|
| 27 |
-
}, []);
|
| 28 |
|
| 29 |
async function handleLogin(e: React.FormEvent) {
|
| 30 |
e.preventDefault(); setLoading(true); setError("");
|
|
@@ -119,3 +119,15 @@ export default function LoginPage() {
|
|
| 119 |
</div>
|
| 120 |
);
|
| 121 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"use client";
|
| 2 |
|
| 3 |
+
import { useState, useEffect, Suspense } from "react";
|
| 4 |
import { createClient } from "@/lib/supabase/client";
|
| 5 |
import { getBaseUrl } from "@/lib/auth-url";
|
| 6 |
import Link from "next/link";
|
| 7 |
import { useSearchParams } from "next/navigation";
|
| 8 |
import { ArrowLeft, Mail, Loader2 } from "lucide-react";
|
| 9 |
|
| 10 |
+
function LoginForm() {
|
| 11 |
const [email, setEmail] = useState("");
|
| 12 |
const [password, setPassword] = useState("");
|
| 13 |
const [error, setError] = useState("");
|
|
|
|
| 24 |
if (user) { window.location.href = next; }
|
| 25 |
else { setChecking(false); }
|
| 26 |
});
|
| 27 |
+
}, [next, supabase.auth]);
|
| 28 |
|
| 29 |
async function handleLogin(e: React.FormEvent) {
|
| 30 |
e.preventDefault(); setLoading(true); setError("");
|
|
|
|
| 119 |
</div>
|
| 120 |
);
|
| 121 |
}
|
| 122 |
+
|
| 123 |
+
export default function LoginPage() {
|
| 124 |
+
return (
|
| 125 |
+
<Suspense fallback={
|
| 126 |
+
<div className="min-h-screen flex items-center justify-center bg-white">
|
| 127 |
+
<Loader2 className="w-5 h-5 text-zinc-300 animate-spin" />
|
| 128 |
+
</div>
|
| 129 |
+
}>
|
| 130 |
+
<LoginForm />
|
| 131 |
+
</Suspense>
|
| 132 |
+
);
|
| 133 |
+
}
|
web/components/extension-bridge.tsx
CHANGED
|
@@ -57,20 +57,24 @@ export function ExtensionBridge() {
|
|
| 57 |
const { data: { subscription } } = supabase.auth.onAuthStateChange(async (event, session) => {
|
| 58 |
// Handle ALL events that mean "user is logged in"
|
| 59 |
if (session && (event === "SIGNED_IN" || event === "INITIAL_SESSION" || event === "TOKEN_REFRESHED")) {
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
sendAuthToExtension(
|
| 69 |
session.access_token,
|
| 70 |
session.user.email || "",
|
| 71 |
-
|
| 72 |
session.user.id,
|
| 73 |
-
|
| 74 |
);
|
| 75 |
}
|
| 76 |
|
|
|
|
| 57 |
const { data: { subscription } } = supabase.auth.onAuthStateChange(async (event, session) => {
|
| 58 |
// Handle ALL events that mean "user is logged in"
|
| 59 |
if (session && (event === "SIGNED_IN" || event === "INITIAL_SESSION" || event === "TOKEN_REFRESHED")) {
|
| 60 |
+
let profileData = null;
|
| 61 |
+
try {
|
| 62 |
+
const result = await supabase
|
| 63 |
+
.from("profiles")
|
| 64 |
+
.select("plan, full_name")
|
| 65 |
+
.eq("id", session.user.id)
|
| 66 |
+
.single();
|
| 67 |
+
profileData = result.data;
|
| 68 |
+
} catch {
|
| 69 |
+
// ignore
|
| 70 |
+
}
|
| 71 |
|
| 72 |
sendAuthToExtension(
|
| 73 |
session.access_token,
|
| 74 |
session.user.email || "",
|
| 75 |
+
profileData?.full_name || session.user.user_metadata?.full_name || "",
|
| 76 |
session.user.id,
|
| 77 |
+
profileData?.plan || "free",
|
| 78 |
);
|
| 79 |
}
|
| 80 |
|