Fix SuperTokens initialization timing issues
- Remove session check from home page (landing page doesn't need it) - Add delayed session check in auth page to redirect logged-in users - Handle SuperTokens not being initialized yet with proper error handling Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import dynamic from "next/dynamic";
|
import dynamic from "next/dynamic";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
|
||||||
// Dynamically import SuperTokens component (client-side only)
|
// Dynamically import SuperTokens component (client-side only)
|
||||||
const SuperTokensAuthComponent = dynamic(
|
const SuperTokensAuthComponent = dynamic(
|
||||||
@@ -10,11 +11,26 @@ const SuperTokensAuthComponent = dynamic(
|
|||||||
);
|
);
|
||||||
|
|
||||||
export default function AuthPage() {
|
export default function AuthPage() {
|
||||||
|
const router = useRouter();
|
||||||
const [mounted, setMounted] = useState(false);
|
const [mounted, setMounted] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setMounted(true);
|
setMounted(true);
|
||||||
}, []);
|
|
||||||
|
// Check if already logged in after a short delay
|
||||||
|
setTimeout(async () => {
|
||||||
|
try {
|
||||||
|
const { doesSessionExist } = await import("supertokens-web-js/recipe/session");
|
||||||
|
const exists = await doesSessionExist();
|
||||||
|
if (exists) {
|
||||||
|
router.push("/marks-account/projects");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// SuperTokens not initialized yet, continue to show auth page
|
||||||
|
console.log("Session check skipped");
|
||||||
|
}
|
||||||
|
}, 500);
|
||||||
|
}, [router]);
|
||||||
|
|
||||||
if (!mounted) {
|
if (!mounted) {
|
||||||
return (
|
return (
|
||||||
|
|||||||
33
app/page.tsx
33
app/page.tsx
@@ -1,43 +1,10 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
import { useRouter } from "next/navigation";
|
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||||
import { doesSessionExist } from "supertokens-web-js/recipe/session";
|
|
||||||
|
|
||||||
export default function HomePage() {
|
export default function HomePage() {
|
||||||
const router = useRouter();
|
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
|
||||||
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
// Check if user is already logged in
|
|
||||||
doesSessionExist().then((exists) => {
|
|
||||||
setIsLoggedIn(exists);
|
|
||||||
setIsLoading(false);
|
|
||||||
|
|
||||||
// If logged in, redirect to projects
|
|
||||||
if (exists) {
|
|
||||||
router.push("/marks-account/projects");
|
|
||||||
}
|
|
||||||
}).catch(() => {
|
|
||||||
setIsLoading(false);
|
|
||||||
});
|
|
||||||
}, [router]);
|
|
||||||
|
|
||||||
if (isLoading) {
|
|
||||||
return (
|
|
||||||
<div className="flex min-h-screen items-center justify-center bg-background">
|
|
||||||
<div className="text-center">
|
|
||||||
<div className="h-8 w-8 animate-spin rounded-full border-4 border-primary border-t-transparent mx-auto mb-4" />
|
|
||||||
<p className="text-muted-foreground">Loading...</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex min-h-screen items-center justify-center bg-background p-4">
|
<div className="flex min-h-screen items-center justify-center bg-background p-4">
|
||||||
<div className="w-full max-w-2xl space-y-6">
|
<div className="w-full max-w-2xl space-y-6">
|
||||||
|
|||||||
Reference in New Issue
Block a user