-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
94 lines (88 loc) · 3.01 KB
/
index.html
File metadata and controls
94 lines (88 loc) · 3.01 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>supabase</title>
</head>
<body>
<section>
<form id="controller">
<input type="text" name="content" />
<button>Add Content</button>
</form>
<section>
<form id="file">
<input type="file" id="fileInput" />
<button>Add File</button>
</form>
</section>
<section id="container"></section>
</section>
<script>
const supabaseAnonKey = "";
const supabaseUrl = "";
const tableName = "TEST_TABLE1";
const form = document.querySelector("#controller");
form.addEventListener("submit", async (event) => {
event.preventDefault();
// 한번 form 관련된 걸 다 밀어버려서...
// prevent default 때문에... 여기서 실행해줘야함...
const formData = new FormData(form);
console.log(formData.get("content"));
// 401 권한 부족 이슈
// RLS을 풀면 ... -> RLS를 풀면 다 풀리는듯;;;
//form데이터 입력
const response = await fetch(`${supabaseUrl}/rest/v1/${tableName}`, {
method: "POST",
headers: {
apikey: supabaseAnonKey,
Authorization: `Bearer ${supabaseAnonKey}`,
"Content-Type": "application/json",
Prefer: "return=representation",
},
body: JSON.stringify({
content: formData.get("content"),
}),
});
console.log(await response.json());
//테이블 데이터 가져오기
const response2 = await fetch(`${supabaseUrl}/rest/v1/${tableName}`, {
method: "GET",
headers: {
apikey: supabaseAnonKey,
Authorization: `Bearer ${supabaseAnonKey}`,
"Content-Type": "application/json",
},
});
const data = await response2.json();
console.log(data);
document.querySelector("#container").textContent = JSON.stringify(data);
});
const bucketName = "test-bucket";
const form2 = document.querySelector("#file");
form2.addEventListener("submit", async (event) => {
event.preventDefault();
const { files } = document.querySelector("#fileInput");
const file = files[0];
console.log(file);
const folderName = "uploads";
const fileName = `${Date.now()}_${file.name}`; // 안겹치려고 // uuid.
const filePath = `${folderName}/${fileName}`;
const response = await fetch(
`${supabaseUrl}/storage/v1/object/${bucketName}/${filePath}`,
{
method: "put",
headers: {
apikey: supabaseAnonKey,
Authorization: `Bearer ${supabaseAnonKey}`,
"Content-Type": file.type,
},
body: file,
}
);
console.log(await response.json());
});
</script>
</body>
</html>