-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetStudents.js
More file actions
51 lines (46 loc) · 1.57 KB
/
getStudents.js
File metadata and controls
51 lines (46 loc) · 1.57 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
// Import supabase client
const { createClient } = require("@supabase/supabase-js");
// Initialize Supabase client
const supabaseUrl = "https://johsonffbpnjjjowpsvo.supabase.co";
const supabaseKey =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImpvaHNvbmZmYnBuampqb3dwc3ZvIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTcxOTQ3Nzg1MCwiZXhwIjoyMDM1MDUzODUwfQ.KlwJkIMj3GncMll9bRTmDngTn2gXQJRe5-q8_mvBFZE";
const supabase = createClient(supabaseUrl, supabaseKey);
// Handler function
exports.handler = async (req, res) => {
const responseHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, Cookie',
};
if (req.method === 'OPTIONS') {
return {
statusCode: 200,
headers: responseHeaders,
body: JSON.stringify({ message: 'CORS preflight request successful' }),
};
}
try {
const { data, error } = await supabase
.from('students')
.select('*');
if (error) {
return {
statusCode: 500,
headers: responseHeaders,
body: JSON.stringify({ error: error.message }),
};
} else {
return {
statusCode: 200,
headers: responseHeaders,
body: JSON.stringify(data),
};
}
} catch (error) {
return {
statusCode: 500,
headers: responseHeaders,
body: JSON.stringify({ error: error.message }),
};
}
};