feat: Pass user role and agent type via cookies for social sign-up and include them in the social authentication API request.

This commit is contained in:
pradeepkumar
2026-03-26 15:18:12 +05:30
parent 2fccc0a5e3
commit d863fdb1c9
2 changed files with 24 additions and 8 deletions

View File

@@ -94,6 +94,16 @@ export default function SignUpPage() {
};
const handleSocialLogin = (provider: 'google' | 'facebook' | 'twitter') => {
// If agent, require agent type selection
if (userType === 'AGENT' && !selectedAgentTypeId) {
setError('Please select an agent type before signing up');
return;
}
// Set cookies with selected role and agent type so server-side auth callback can read them
document.cookie = `socialAuthRole=${userType};path=/;max-age=300;SameSite=Lax`;
if (userType === 'AGENT' && selectedAgentTypeId) {
document.cookie = `socialAuthAgentTypeId=${selectedAgentTypeId};path=/;max-age=300;SameSite=Lax`;
}
signIn(provider, { callbackUrl: '/' });
};
@@ -317,14 +327,6 @@ export default function SignUpPage() {
{/* Social Login Icons */}
<div className="flex justify-center items-center gap-8">
{/* Apple */}
<button
type="button"
className="w-10 h-10 flex items-center justify-center hover:opacity-70 transition-opacity"
>
<img src="/assets/icons/apple.svg" alt="Apple" className="w-6 h-6" />
</button>
{/* Google */}
<button
type="button"

View File

@@ -114,6 +114,18 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
// For OAuth providers, sync with backend
if (account?.provider === "google" || account?.provider === "facebook" || account?.provider === "twitter") {
try {
// Read role and agent type from cookies (set by signup page before OAuth redirect)
let role: string | undefined;
let agentTypeId: string | undefined;
try {
const { cookies } = await import("next/headers");
const cookieStore = await cookies();
role = cookieStore.get("socialAuthRole")?.value;
agentTypeId = cookieStore.get("socialAuthAgentTypeId")?.value;
} catch {
// Cookie may not be available
}
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/auth/social`,
{
@@ -125,6 +137,8 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
email: user.email,
name: user.name,
avatar: user.image,
role,
agentTypeId,
}),
}
);