-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-ownership.html
More file actions
242 lines (218 loc) · 10.3 KB
/
debug-ownership.html
File metadata and controls
242 lines (218 loc) · 10.3 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<!DOCTYPE html>
<html>
<head>
<title>Debug Chat Ownership</title>
<script src="https://unpkg.com/@nhost/nhost-js@latest"></script>
</head>
<body>
<h1>Debug Chat Ownership</h1>
<div>
<button onclick="signIn()">1. Sign In</button>
<button onclick="checkChats()">2. Check My Chats</button>
<button onclick="createNewChat()">3. Create New Chat</button>
<button onclick="testChatOwnership()">4. Test Chat Ownership</button>
</div>
<div id="result"></div>
<script>
// Initialize Nhost
const nhost = new NhostClient({
subdomain: 'wertssqowjrkykmeovsg',
region: 'ap-south-1'
});
async function signIn() {
const result = document.getElementById('result');
result.innerHTML = 'Signing in...';
try {
const { session, error } = await nhost.auth.signIn({
email: 'test@example.com',
password: 'password123'
});
if (error) {
result.innerHTML = `<div style="color: red;">Sign in failed: ${error.message}</div>`;
} else {
const user = nhost.auth.getUser();
result.innerHTML = `
<div style="color: green;">
✅ Signed in successfully!<br>
<strong>User ID:</strong> ${user.id}<br>
<strong>Email:</strong> ${user.email}
</div>
`;
}
} catch (error) {
result.innerHTML = `<div style="color: red;">Sign in error: ${error.message}</div>`;
}
}
async function checkChats() {
const result = document.getElementById('result');
result.innerHTML = 'Checking chats...';
try {
const accessToken = await nhost.auth.getAccessToken();
const user = nhost.auth.getUser();
if (!accessToken) {
result.innerHTML = '<div style="color: red;">Please sign in first!</div>';
return;
}
const response = await fetch('https://wertssqowjrkykmeovsg.hasura.ap-south-1.nhost.run/v1/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`
},
body: JSON.stringify({
query: `
query GetMyChats {
chats {
id
title
user_id
created_at
}
}
`
})
});
const data = await response.json();
console.log('Chats response:', data);
if (data.errors) {
result.innerHTML = `<div style="color: red;">Error: ${JSON.stringify(data.errors, null, 2)}</div>`;
} else {
const chats = data.data.chats || [];
result.innerHTML = `
<div>
<h3>Your Chats (User ID: ${user?.id}):</h3>
${chats.length === 0 ?
'<p style="color: orange;">No chats found. Create a new chat first!</p>' :
chats.map(chat => `
<div style="border: 1px solid #ccc; margin: 5px; padding: 10px;">
<strong>Chat ID:</strong> ${chat.id}<br>
<strong>Title:</strong> ${chat.title}<br>
<strong>Owner ID:</strong> ${chat.user_id}<br>
<strong>Match:</strong> ${chat.user_id === user?.id ? '✅ You own this' : '❌ Not yours'}<br>
<strong>Created:</strong> ${new Date(chat.created_at).toLocaleString()}
</div>
`).join('')
}
</div>
`;
}
} catch (error) {
result.innerHTML = `<div style="color: red;">Error: ${error.message}</div>`;
}
}
async function createNewChat() {
const result = document.getElementById('result');
result.innerHTML = 'Creating new chat...';
try {
const accessToken = await nhost.auth.getAccessToken();
if (!accessToken) {
result.innerHTML = '<div style="color: red;">Please sign in first!</div>';
return;
}
const response = await fetch('https://wertssqowjrkykmeovsg.hasura.ap-south-1.nhost.run/v1/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`
},
body: JSON.stringify({
query: `
mutation CreateNewChat($title: String!) {
insert_chats_one(object: { title: $title }) {
id
title
user_id
created_at
}
}
`,
variables: {
title: "Debug Test Chat"
}
})
});
const data = await response.json();
console.log('Create chat response:', data);
if (data.errors) {
result.innerHTML = `<div style="color: red;">Error: ${JSON.stringify(data.errors, null, 2)}</div>`;
} else {
const chat = data.data.insert_chats_one;
result.innerHTML = `
<div style="color: green;">
✅ New chat created!<br>
<strong>Chat ID:</strong> ${chat.id}<br>
<strong>Title:</strong> ${chat.title}<br>
<strong>Owner ID:</strong> ${chat.user_id}<br>
<strong>Created:</strong> ${new Date(chat.created_at).toLocaleString()}<br>
<br>
<strong>Use this Chat ID for testing!</strong>
</div>
`;
}
} catch (error) {
result.innerHTML = `<div style="color: red;">Error: ${error.message}</div>`;
}
}
async function testChatOwnership() {
const result = document.getElementById('result');
const chatId = prompt('Enter the Chat ID to test ownership:');
if (!chatId) return;
result.innerHTML = 'Testing chat ownership...';
try {
const accessToken = await nhost.auth.getAccessToken();
const user = nhost.auth.getUser();
if (!accessToken) {
result.innerHTML = '<div style="color: red;">Please sign in first!</div>';
return;
}
const response = await fetch('https://wertssqowjrkykmeovsg.hasura.ap-south-1.nhost.run/v1/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`
},
body: JSON.stringify({
query: `
query CheckChatOwnership($chatId: uuid!, $userId: uuid!) {
chats_by_pk(id: $chatId) {
id
title
user_id
}
}
`,
variables: {
chatId: chatId,
userId: user.id
}
})
});
const data = await response.json();
console.log('Ownership check response:', data);
if (data.errors) {
result.innerHTML = `<div style="color: red;">Error: ${JSON.stringify(data.errors, null, 2)}</div>`;
} else {
const chat = data.data.chats_by_pk;
if (!chat) {
result.innerHTML = `<div style="color: red;">❌ Chat not found: ${chatId}</div>`;
} else {
const ownsChat = chat.user_id === user.id;
result.innerHTML = `
<div style="color: ${ownsChat ? 'green' : 'red'};">
<h3>${ownsChat ? '✅ You own this chat!' : '❌ You do NOT own this chat'}</h3>
<strong>Chat ID:</strong> ${chat.id}<br>
<strong>Chat Title:</strong> ${chat.title}<br>
<strong>Chat Owner ID:</strong> ${chat.user_id}<br>
<strong>Your User ID:</strong> ${user.id}<br>
<strong>Match:</strong> ${ownsChat ? 'YES' : 'NO'}
</div>
`;
}
}
} catch (error) {
result.innerHTML = `<div style="color: red;">Error: ${error.message}</div>`;
}
}
</script>
</body>
</html>