From 4b431358e08937748c4675a0d3e3b218d05296d0 Mon Sep 17 00:00:00 2001 From: Lucas Santana Date: Fri, 20 Dec 2024 12:10:59 -0300 Subject: [PATCH] Adicionar User Role --- src/pages/admin/UserManagementPage.tsx | 112 +++++++++++++++++++++++++ src/pages/api/updateRole.ts | 16 ++++ src/pages/api/updateUserRole.ts | 14 ++++ src/scripts/updateUserRole.ts | 33 ++++++++ src/utils/updateUserRole.ts | 20 +++++ 5 files changed, 195 insertions(+) create mode 100644 src/pages/admin/UserManagementPage.tsx create mode 100644 src/pages/api/updateRole.ts create mode 100644 src/pages/api/updateUserRole.ts create mode 100644 src/scripts/updateUserRole.ts create mode 100644 src/utils/updateUserRole.ts diff --git a/src/pages/admin/UserManagementPage.tsx b/src/pages/admin/UserManagementPage.tsx new file mode 100644 index 0000000..1ffeb63 --- /dev/null +++ b/src/pages/admin/UserManagementPage.tsx @@ -0,0 +1,112 @@ +import React from 'react'; +import { supabase } from '../../lib/supabase'; + +interface User { + id: string; + email: string; + user_metadata?: { + role?: string; + }; +} + +export function UserManagementPage() { + const [users, setUsers] = React.useState([]); + const [loading, setLoading] = React.useState(true); + const [error, setError] = React.useState(null); + const [updating, setUpdating] = React.useState(null); + + React.useEffect(() => { + fetchUsers(); + }, []); + + const fetchUsers = async () => { + try { + const { data: { users }, error } = await supabase.auth.admin.listUsers(); + if (error) throw error; + setUsers(users || []); + } catch (err) { + console.error('Erro ao buscar usuários:', err); + setError('Não foi possível carregar os usuários'); + } finally { + setLoading(false); + } + }; + + const handleUpdateRole = async (userId: string, role: string) => { + setUpdating(userId); + try { + const { error } = await supabase.auth.admin.updateUserById( + userId, + { user_metadata: { role } } + ); + if (error) throw error; + await fetchUsers(); + } catch (err) { + console.error('Erro ao atualizar papel:', err); + setError('Não foi possível atualizar o papel do usuário'); + } finally { + setUpdating(null); + } + }; + + if (loading) { + return
Carregando...
; + } + + return ( +
+

Gerenciamento de Usuários

+ + {error && ( +
+ {error} +
+ )} + +
+ + + + + + + + + + {users.map((user) => ( + + + + + + ))} + +
+ Email + + Papel Atual + + Ações +
+ {user.email} + + + {user.user_metadata?.role || 'Não definido'} + + + +
+
+
+ ); +} \ No newline at end of file diff --git a/src/pages/api/updateRole.ts b/src/pages/api/updateRole.ts new file mode 100644 index 0000000..1c71710 --- /dev/null +++ b/src/pages/api/updateRole.ts @@ -0,0 +1,16 @@ +import { supabase } from '../../lib/supabase'; + +export async function updateRole(email: string, role: string) { + const { data: { user }, error } = await supabase.auth.admin.updateUserById( + email, + { + user_metadata: { role } + } + ); + + if (error) { + throw error; + } + + return user; +} \ No newline at end of file diff --git a/src/pages/api/updateUserRole.ts b/src/pages/api/updateUserRole.ts new file mode 100644 index 0000000..76244de --- /dev/null +++ b/src/pages/api/updateUserRole.ts @@ -0,0 +1,14 @@ +import { supabase } from '../../lib/supabase'; + +export async function updateUserRole(userId: string, role: string) { + const { data, error } = await supabase.auth.admin.updateUserById( + userId, + { user_metadata: { role: role } } + ); + + if (error) { + throw error; + } + + return data; +} \ No newline at end of file diff --git a/src/scripts/updateUserRole.ts b/src/scripts/updateUserRole.ts new file mode 100644 index 0000000..53f66ff --- /dev/null +++ b/src/scripts/updateUserRole.ts @@ -0,0 +1,33 @@ +import { createClient } from '@supabase/supabase-js'; + +const supabase = createClient( + 'SUA_URL_DO_SUPABASE', + 'SUA_ANON_KEY' +); + +async function updateUserRole(email: string, role: 'school' | 'teacher' | 'student') { + try { + // Primeiro fazer login como o usuário + const { data: authData, error: authError } = await supabase.auth.signInWithPassword({ + email: email, + password: 'SENHA_DO_USUARIO' // Substitua pela senha real + }); + + if (authError) throw authError; + + // Depois atualizar os metadados + const { data, error } = await supabase.auth.updateUser({ + data: { role: role } + }); + + if (error) throw error; + + console.log('Papel do usuário atualizado com sucesso:', data); + + } catch (err) { + console.error('Erro ao atualizar papel do usuário:', err); + } +} + +// Exemplo de uso: +updateUserRole('email@escola.com', 'school'); \ No newline at end of file diff --git a/src/utils/updateUserRole.ts b/src/utils/updateUserRole.ts new file mode 100644 index 0000000..b3d766e --- /dev/null +++ b/src/utils/updateUserRole.ts @@ -0,0 +1,20 @@ +import { supabase } from '../lib/supabase'; + +export async function updateUserRole(userId: string, role: 'school' | 'teacher' | 'student') { + try { + const { data, error } = await supabase.auth.updateUser({ + data: { role } + }); + + if (error) throw error; + + console.log('Role atualizado com sucesso:', data); + return data; + } catch (err) { + console.error('Erro ao atualizar role:', err); + throw err; + } +} + +// Uso: +// await updateUserRole('id-do-usuario', 'school'); \ No newline at end of file