File size: 1,895 Bytes
4bea261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { txasupabase } from '../../../lib/txasupabase.js';

export async function GET({ cookies }) {
  const token = cookies.get('auth_token')?.value;
  let user = null;
  if (token) {
    try {
      user = JSON.parse(Buffer.from(token, 'base64').toString('utf-8'));
    } catch (e) {}
  }

  if (!user || user.role !== 'admin') {
    return new Response(
      JSON.stringify({ error: 'Từ chối truy cập! Yêu cầu quyền quản trị viên.' }), 
      { status: 403, headers: { 'Content-Type': 'application/json' } }
    );
  }

  try {
    const settings = await txasupabase.getSettings();
    return new Response(JSON.stringify(settings), {
      status: 200,
      headers: { 'Content-Type': 'application/json' }
    });
  } catch (error) {
    return new Response(JSON.stringify({ error: 'Lỗi máy chủ: ' + error.message }), {
      status: 500,
      headers: { 'Content-Type': 'application/json' }
    });
  }
}

export async function POST({ request, cookies }) {
  const token = cookies.get('auth_token')?.value;
  let user = null;
  if (token) {
    try {
      user = JSON.parse(Buffer.from(token, 'base64').toString('utf-8'));
    } catch (e) {}
  }

  if (!user || user.role !== 'admin') {
    return new Response(
      JSON.stringify({ error: 'Từ chối truy cập! Yêu cầu quyền quản trị viên.' }), 
      { status: 403, headers: { 'Content-Type': 'application/json' } }
    );
  }

  try {
    const settingsObj = await request.json();
    await txasupabase.saveSettings(settingsObj);
    return new Response(JSON.stringify({ message: 'Lưu cài đặt thành công!' }), {
      status: 200,
      headers: { 'Content-Type': 'application/json' }
    });
  } catch (error) {
    return new Response(JSON.stringify({ error: 'Lỗi máy chủ: ' + error.message }), {
      status: 500,
      headers: { 'Content-Type': 'application/json' }
    });
  }
}