fix: use shared supabase client with auth session in admin panels
- UserManagementPanel: replace createClient() with supabase from supabaseClient - ErrorLogPanel: same — reuse authenticated supabase singleton - Remove production_lead role from ROLES/ROLE_LABELS/ROLE_TONES - Remove service_role key dependency — RLS handles auth - Fixes: admin Users tab showing empty (RLS couldnt resolve auth.uid())
This commit is contained in:
parent
c996805122
commit
2fea387d43
|
|
@ -2,10 +2,9 @@ import React, { useState, useEffect, useCallback, useRef } from 'react';
|
|||
import { Panel } from '../UI/Panel';
|
||||
import { Badge } from '../UI/Badge';
|
||||
import { Select } from '../UI/Select';
|
||||
import { createClient } from '@supabase/supabase-js';
|
||||
import { supabase } from '../../supabaseClient';
|
||||
|
||||
|
||||
const supabaseUrl = import.meta.env.VITE_SUPABASE_URL;
|
||||
const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY;
|
||||
|
||||
const DATE_RANGES = [
|
||||
{ value: 'today', label: 'Сегодня' },
|
||||
|
|
@ -55,7 +54,7 @@ export default function ErrorLogPanel() {
|
|||
const [copied, setCopied] = useState(false);
|
||||
const [deleting, setDeleting] = useState(false);
|
||||
const intervalRef = useRef(null);
|
||||
const client = createClient(supabaseUrl, supabaseAnonKey);
|
||||
|
||||
|
||||
const getRangeStart = (range) => {
|
||||
const now = new Date();
|
||||
|
|
@ -69,7 +68,7 @@ export default function ErrorLogPanel() {
|
|||
|
||||
const fetchErrors = useCallback(async () => {
|
||||
setFetchError(null);
|
||||
let query = client.from('client_error_logs').select('*').order('created_at', { ascending: false });
|
||||
let query = supabase.from('client_error_logs').select('*').order('created_at', { ascending: false });
|
||||
const rangeStart = getRangeStart(filterRange);
|
||||
if (rangeStart) query = query.gte('created_at', rangeStart);
|
||||
if (filterType) query = query.eq('error_type', filterType);
|
||||
|
|
|
|||
|
|
@ -3,19 +3,16 @@ import { Panel } from '../UI/Panel';
|
|||
import { Badge } from '../UI/Badge';
|
||||
import { Input } from '../UI/Input';
|
||||
import { Select } from '../UI/Select';
|
||||
import { createClient } from '@supabase/supabase-js';
|
||||
|
||||
const supabaseUrl = import.meta.env.VITE_SUPABASE_URL;
|
||||
const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY;
|
||||
const supabaseServiceKey = import.meta.env.VITE_SUPABASE_SERVICE_ROLE_KEY;
|
||||
|
||||
const ROLES = ['admin', 'driver', 'logistician', 'manager', 'mega_admin', 'production_lead'];
|
||||
import { supabase } from '../../supabaseClient';
|
||||
|
||||
const ROLES = ['admin', 'driver', 'logistician', 'manager', 'mega_admin'];
|
||||
|
||||
const ROLE_LABELS = {
|
||||
mega_admin: 'Суперадмин',
|
||||
admin: 'Администратор',
|
||||
manager: 'Менеджер',
|
||||
production_lead: 'Начальник производства',
|
||||
logistician: 'Логист',
|
||||
driver: 'Водитель-экспедитор',
|
||||
};
|
||||
|
|
@ -24,7 +21,6 @@ const ROLE_TONES = {
|
|||
mega_admin: 'danger',
|
||||
admin: 'warning',
|
||||
manager: 'info',
|
||||
production_lead: 'info',
|
||||
logistician: 'accent',
|
||||
driver: 'accent',
|
||||
};
|
||||
|
|
@ -54,11 +50,11 @@ export default function UserManagementPanel() {
|
|||
const [addError, setAddError] = useState(null);
|
||||
const mobile = useIsMobile();
|
||||
|
||||
const client = createClient(supabaseUrl, supabaseAnonKey);
|
||||
const adminClient = supabaseServiceKey ? createClient(supabaseUrl, supabaseServiceKey) : null;
|
||||
|
||||
|
||||
|
||||
const fetchRoles = useCallback(async () => {
|
||||
const { data, error: err } = await client.from('roles').select('id, name').order('name');
|
||||
const { data, error: err } = await supabase.from('roles').select('id, name').order('name');
|
||||
if (err) { setError(err.message); return []; }
|
||||
setRoles(data || []);
|
||||
return data || [];
|
||||
|
|
@ -98,18 +94,18 @@ export default function UserManagementPanel() {
|
|||
}
|
||||
setAddSubmitting(true);
|
||||
try {
|
||||
if (!adminClient) {
|
||||
if (!supabase) {
|
||||
setAddError('Требуется VITE_SUPABASE_SERVICE_ROLE_KEY для управления пользователями.');
|
||||
return;
|
||||
}
|
||||
const roleId = getRoleId(addForm.role);
|
||||
const { data: authData, error: authErr } = await adminClient.auth.admin.createUser({
|
||||
const { data: authData, error: authErr } = await supabase.auth.admin.createUser({
|
||||
email: addForm.email,
|
||||
email_confirm: true,
|
||||
user_metadata: { name: addForm.name },
|
||||
});
|
||||
if (authErr) throw authErr;
|
||||
const { error: insertErr } = await adminClient
|
||||
const { error: insertErr } = await supabase
|
||||
.from('users')
|
||||
.insert({ id: authData.user.id, email: addForm.email, name: addForm.name, role_id: roleId });
|
||||
if (insertErr) throw insertErr;
|
||||
|
|
@ -127,7 +123,7 @@ export default function UserManagementPanel() {
|
|||
const roleId = getRoleId(newRoleName);
|
||||
if (!roleId) return;
|
||||
try {
|
||||
const { error: err } = await (adminClient || client)
|
||||
const { error: err } = await (supabase || client)
|
||||
.from('users')
|
||||
.update({ role_id: roleId })
|
||||
.eq('id', userId);
|
||||
|
|
@ -141,9 +137,9 @@ export default function UserManagementPanel() {
|
|||
|
||||
const handleDeleteUser = async (userId) => {
|
||||
try {
|
||||
const { error: err } = await (adminClient || client).from('users').delete().eq('id', userId);
|
||||
const { error: err } = await (supabase || client).from('users').delete().eq('id', userId);
|
||||
if (err) throw err;
|
||||
if (adminClient) await adminClient.auth.admin.deleteUser(userId);
|
||||
if (supabase) await supabase.auth.admin.deleteUser(userId);
|
||||
setDeleteConfirmId(null);
|
||||
await fetchUsers();
|
||||
} catch (err) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue