feat(auth): implement role-based dashboard redirect

This commit is contained in:
dhinesh-m24
2026-01-29 11:43:19 +05:30
parent d07d42ad0b
commit e214e32c17
12 changed files with 713 additions and 78 deletions

View File

@@ -0,0 +1,55 @@
import { getFirestore, doc, getDoc } from "firebase/firestore";
import { app } from "../features/auth/firebase";
export interface UserData {
id: string;
email: string;
fullName?: string;
userRole: "admin" | "client" | "vendor";
photoURL?: string;
}
const db = getFirestore(app);
/**
* Fetch user data from Firestore including their role
* @param uid - Firebase User UID
* @returns UserData object with role information
*/
export const fetchUserData = async (uid: string): Promise<UserData | null> => {
try {
const userDocRef = doc(db, "users", uid);
const userDocSnap = await getDoc(userDocRef);
if (userDocSnap.exists()) {
const data = userDocSnap.data();
return {
id: uid,
email: data.email || "",
fullName: data.fullName,
userRole: data.userRole || "client",
photoURL: data.photoURL,
};
}
return null;
} catch (error) {
console.error("Error fetching user data from Firestore:", error);
throw error;
}
};
/**
* Get the dashboard path for a given user role
* @param userRole - The user's role
* @returns The appropriate dashboard path
*/
export const getDashboardPath = (userRole: string): string => {
const roleMap: Record<string, string> = {
admin: "/dashboard/admin",
client: "/dashboard/client",
vendor: "/dashboard/vendor",
};
return roleMap[userRole.toLowerCase()] || "/dashboard/client";
};