-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-helpers.js
More file actions
122 lines (106 loc) · 3.62 KB
/
Copy pathauth-helpers.js
File metadata and controls
122 lines (106 loc) · 3.62 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// auth-helpers.js
// ========================================================================
// AUTH HELPERS - DÙNG CHUNG CHO TOÀN APP
// ========================================================================
/**
* Kiểm tra user có phải admin không
*/
window.isUserAdmin = function () {
const role = window.userSession?.role || '';
return String(role).toLowerCase().trim() === 'admin';
};
/**
* Kiểm tra user có role cụ thể không
*/
window.hasRole = function (requiredRole) {
if (!requiredRole) return false;
const role = window.userSession?.role || '';
return (
String(role).toLowerCase().trim() ===
String(requiredRole).toLowerCase().trim()
);
};
// Thêm hàm này ở đầu file để lấy UUID an toàn
window.getCurrentUserId = function () {
// Ưu tiên 1: Đã có trong session
if (window.userSession?.id) return window.userSession.id;
// Fallback 2: Đọc từ localStorage của Supabase
try {
const key = `sb-${
window.supabaseClient?.projectRef || 'default'
}-auth-token`;
const stored = localStorage.getItem(key);
if (stored) {
const parsed = JSON.parse(stored);
return parsed?.user?.id || null;
}
} catch (e) {}
return null;
};
/**
* Load profile từ DB và cập nhật window.userSession
* @returns {Promise<boolean>} true nếu thành công
*/
// ========================================================================
// HÀM LOAD PROFILE - CHỐNG MẤT SESSION KHI F5
// ========================================================================
window.loadUserProfile = async function () {
try {
// 1. BẮT BUỘC dùng getSession() để Supabase tự động móc token từ localStorage ra
const {
data: { session },
error: sessionError,
} = await window.supabaseClient.auth.getSession();
// Nếu vừa F5 mà chưa kịp có session, hoặc báo lỗi -> Trả về false để đá ra login
if (sessionError || !session) {
console.warn(
'⚠️ Mất Session: Supabase không tìm thấy token trong bộ nhớ.'
);
return false;
}
// 2. Nếu đã có session, bắt đầu lấy chi tiết Profile từ Database
const user = session.user;
const { data: profile, error: profileError } = await window.supabaseClient
.from('profiles')
.select('*') // Nếu bảng profiles của bạn lớn, bạn có thể select đích danh các cột cần thiết
.eq('id', user.id)
.single();
if (profileError) {
console.error('❌ Lỗi lấy Profile từ Supabase:', profileError.message);
return false;
}
// 3. Phục hồi thành công! Ghép thông tin User và Profile lại lưu vào biến toàn cục
window.userSession = {
id: user.id,
email: user.email,
...profile,
};
console.log('✅ Đã khôi phục Session thành công sau khi F5!');
return true;
} catch (error) {
console.error('❌ Lỗi hệ thống cực nghiêm trọng khi tải Profile:', error);
return false;
}
};
/**
* Đăng xuất + cleanup
*/
window.logout = async function () {
console.log('👋 Logging out...');
// Clear local state
window.userSession = null;
if (window.appState) {
window.appState.currentUser = null;
window.appState.appInitialized = false;
}
// Sign out từ Supabase
if (window.supabaseClient?.auth) {
await window.supabaseClient.auth.signOut();
}
// Clear localStorage
localStorage.removeItem('sidebar_hidden');
localStorage.removeItem('theme');
// Redirect về login
console.log('🔄 Redirecting to login...');
window.location.href = '/login.html';
};