feat(auth): implement role-based dashboard redirect
This commit is contained in:
55
apps/web/src/services/firestoreService.ts
Normal file
55
apps/web/src/services/firestoreService.ts
Normal 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";
|
||||
};
|
||||
Reference in New Issue
Block a user