import prisma from "@/app/lib/db";
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";
import { NextResponse } from "next/server";
export async function POST(request: Request) {
try {
const { getUser } = getKindeServerSession();
const user = await getUser();
console.log("User session details:", user);
if (!user || !user.id) {
console.error("Account Identification Failed: User or User ID is missing");
throw new Error("Account Identification Failed");
}
const { roleName } = await request.json();
console.log(`Role name received: ${roleName}`);
if (!roleName || (roleName !== 'Freelancer' && roleName !== 'Client')) {
console.error("Invalid Role");
return NextResponse.json({ error: 'Invalid Role' }, { status: 400 });
}
const role = await prisma.role.findFirst({
where: {
roleName,
},
});
if (!role) {
console.error("Role not found in database");
return NextResponse.json({ error: 'Role not found' }, { status: 404 });
}
const updatedUser = await prisma.user.update({
where: {
id: user.id,
},
data: {
roleId: role.id,
},
});
console.log(`User ${updatedUser.id} role updated to ${roleName}`);
const redirectUrl = roleName === "Freelancer"
? "http://localhost:3000/dashboard/freelancer"
: "http://localhost:3000/dashboard/client";
return NextResponse.redirect(redirectUrl);
} catch (error) {
if (error instanceof Error) {
console.error("Error in role selection POST route:", error.message);
return NextResponse.json({ error: error.message }, { status: 500 });
} else {
console.error("Unexpected error in role selection POST route:", error);
return NextResponse.json({ error: "Unexpected error occurred" }, { status: 500 });
}
}
}
'use client';
import React, { useState } from 'react';
import { useRouter } from 'next/navigation';
import { FaUserTie, FaBriefcase } from 'react-icons/fa';
const RoleSelection = () => {
const [loading, setLoading] = useState(false);
const router = useRouter();
const handleRoleSelection = async (role: string) => {
setLoading(true);
console.log(`Role selected: ${role}`);
try {
const response = await fetch('/api/roleselection', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ roleName: role }),
});
console.log('Fetch response:', response);
if (response.ok) {
const redirectUrl = role === 'Freelancer'
? '/dashboard/freelancer'
: '/dashboard/client';
console.log(`Redirecting to ${redirectUrl}`);
router.push(redirectUrl);
} else {
const errorData = await response.json();
console.error('Failed to select role', errorData);
}
} catch (error) {
console.error('Error selecting role:', error);
} finally {
setLoading(false);
}
};
return (
<div className="flex flex-col items-center justify-center min-h-screen bg-gray-50">
<h1 className="text-4xl font-bold text-yellow-500 mb-12">Register As a</h1>
<div className="grid grid-cols-2 gap-4 w-full max-w-md mx-auto">
<div
onClick={() => handleRoleSelection('Freelancer')}
className="cursor-pointer aspect-square flex flex-col items-center justify-center text-center text-gray-800 bg-white border-2 border-gray-800 rounded-lg shadow-lg hover:bg-yellow-500 hover:text-white transition-all duration-300 ease-in-out transform hover:scale-105"
>
<FaUserTie className="text-6xl mb-2" />
<span className="text-xl font-semibold">Freelancer</span>
</div>
<div
onClick={() => handleRoleSelection('Client')}
className="cursor-pointer aspect-square flex flex-col items-center justify-center text-center text-gray-800 bg-white border-2 border-gray-800 rounded-lg shadow-lg hover:bg-yellow-500 hover:text-white transition-all duration-300 ease-in-out transform hover:scale-105"
>
<FaBriefcase className="text-6xl mb-2" />
<span className="text-xl font-semibold">Client</span>
</div>
</div>
{loading && <p className="text-yellow-500 mt-4">Loading...</p>}
</div>
);
};
export default RoleSelection;
I made Role selection page and when user select the role they need to redirect to the dashbord page according to their selected role. I added the Roleselection page tsx file and route.ts file.
I use nextjs,prisma,supabase and Kinde auth.
when i click the role Freelancer or Client it not redirect to the dashbord. just loading and still in the same page. And when i try POST Request with postman it shows
500 internal server error
“error”: “Account Identification Failed”
Can anyone give me solution for this
SamiBro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.