-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirect-handler.js
More file actions
39 lines (34 loc) · 1.41 KB
/
redirect-handler.js
File metadata and controls
39 lines (34 loc) · 1.41 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
import { useEffect } from 'react';
import { useAuth, useUser } from '@clerk/nextjs';
import { useRouter } from 'next/router';
import { Users} from "@/utils/schema";
import { db } from "@/utils/db";
export default function RedirectHandler() {
const { isSignedIn } = useAuth();
const { user } = useUser();
const router = useRouter();
useEffect(() => {
if (isSignedIn && user) {
// Save user data to your backend
const saveUserData = async () => {
try {
await db.insert(Users).values({
email : user?.primaryEmailAddress?.emailAddress, // Use the email from session metadata
plan: 'plan_free', // Save the plan
mockUsed: 0, // Start with 0 mocks used
mockLimit: 3, // Set mock limit based on the plan
createdAt: new Date().toISOString(),
endDate: new Date(new Date().setMonth(new Date().getMonth() - 1)).toISOString(), // Set created date if this is a new user
paymentStatus:"Null",
}).onConflictDoNothing(); // Avoid duplicate entries
// Redirect to the desired page
router.push('/dashboard'); // Change to your target route
} catch (error) {
console.error('Failed to save user data:', error);
}
};
saveUserData();
}
}, [isSignedIn, user, router]);
return <p>Redirecting...</p>;
}