feat: habilitar acceso automático al Hub por dominio
This commit is contained in:
+22
-11
@@ -82,6 +82,7 @@ interface AuthorizedUserRow {
|
||||
email: string;
|
||||
full_name: string | null;
|
||||
role: "admin" | "member";
|
||||
is_active: boolean;
|
||||
}
|
||||
|
||||
interface AuthorizationResult {
|
||||
@@ -97,11 +98,20 @@ function getOAuthRedirectUrl(): string {
|
||||
return new URL(import.meta.env.BASE_URL, window.location.origin).toString();
|
||||
}
|
||||
|
||||
function getAuthenticatedName(authSession: SupabaseAuthSession, access: AuthorizedUserRow): string {
|
||||
function getAuthenticatedName(
|
||||
authSession: SupabaseAuthSession,
|
||||
access: AuthorizedUserRow | null,
|
||||
): string {
|
||||
const metadata = authSession.user.user_metadata ?? {};
|
||||
const metadataName = metadata.full_name ?? metadata.name;
|
||||
const emailName = access.email.split("@")[0] || "Usuario GLM";
|
||||
return access.full_name?.trim() || (typeof metadataName === "string" ? metadataName.trim() : "") || emailName;
|
||||
const email = authSession.user.email?.trim().toLocaleLowerCase("en-US") ?? "";
|
||||
const emailName = email.split("@")[0] || "Usuario GLM";
|
||||
|
||||
return (
|
||||
access?.full_name?.trim() ||
|
||||
(typeof metadataName === "string" ? metadataName.trim() : "") ||
|
||||
emailName
|
||||
);
|
||||
}
|
||||
|
||||
function getAuthenticatedAvatarUrl(authSession: SupabaseAuthSession): string {
|
||||
@@ -127,26 +137,27 @@ async function authorizeSupabaseSession(authSession: SupabaseAuthSession): Promi
|
||||
};
|
||||
}
|
||||
|
||||
// Todos los correos corporativos entran como Usuario por defecto.
|
||||
// La tabla solo conserva administradores y bloqueos excepcionales.
|
||||
const { data, error } = await supabase
|
||||
.from("glm_hub_authorized_users")
|
||||
.select("email, full_name, role")
|
||||
.select("email, full_name, role, is_active")
|
||||
.eq("email", email)
|
||||
.eq("is_active", true)
|
||||
.maybeSingle();
|
||||
const access = data as AuthorizedUserRow | null;
|
||||
|
||||
if (error) {
|
||||
console.error("No se pudo validar el acceso en Supabase:", error);
|
||||
console.error("No se pudo validar el perfil de acceso:", error);
|
||||
return {
|
||||
session: null,
|
||||
error: "No pudimos validar tu acceso en Supabase. Intenta nuevamente.",
|
||||
error: "No pudimos completar el inicio de sesión. Intenta nuevamente.",
|
||||
};
|
||||
}
|
||||
|
||||
if (!access) {
|
||||
if (access && !access.is_active) {
|
||||
return {
|
||||
session: null,
|
||||
error: "Tu correo no está autorizado para entrar al GLM Hub.",
|
||||
error: "Tu acceso al GLM Hub está deshabilitado. Contacta a IT Support.",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -155,8 +166,8 @@ async function authorizeSupabaseSession(authSession: SupabaseAuthSession): Promi
|
||||
user: {
|
||||
id: authSession.user.id,
|
||||
name: getAuthenticatedName(authSession, access),
|
||||
email: access.email,
|
||||
role: access.role,
|
||||
email,
|
||||
role: access?.role === "admin" ? "admin" : "member",
|
||||
avatarUrl: getAuthenticatedAvatarUrl(authSession),
|
||||
},
|
||||
expiresAt: (authSession.expires_at ?? Math.floor(Date.now() / 1000) + 3600) * 1000,
|
||||
|
||||
Reference in New Issue
Block a user