Skip to content

Commit fbc8c09

Browse files
committed
combined some auth serverless functions
1 parent f030b77 commit fbc8c09

File tree

7 files changed

+115
-88
lines changed

7 files changed

+115
-88
lines changed

frontend/api/approval.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
2+
3+
4+
export async function approveHandler(req, res) {
5+
if (req.method !== "POST") {
6+
return res.status(405).end("Method Not Allowed");
7+
}
8+
9+
const { userId } = req.body;
10+
11+
const response = await fetch(`https://api.clerk.dev/v1/users/${userId}`, {
12+
method: "PATCH",
13+
headers: {
14+
Authorization: `Bearer ` + process.env.CLERK_SECRET_KEY,
15+
"Content-Type": "application/json",
16+
},
17+
18+
body: JSON.stringify({
19+
public_metadata: {
20+
// Set Clerk metadata to approved
21+
approved: true
22+
},
23+
}),
24+
});
25+
26+
if (!response.ok) {
27+
return res.status(500).json({ message: "Failed to approve user" });
28+
}
29+
30+
res.status(200).json({ message: "User approved" });
31+
}
32+
33+
export async function checkHandler(req, res) {
34+
const { userId } = req.body;
35+
36+
if (!userId) {
37+
return res.status(400).json({ error: "Missing userId" });
38+
}
39+
40+
const response = await fetch(`https://api.clerk.dev/v1/users/${userId}`, {
41+
headers: {
42+
Authorization: `Bearer sk_test_wGcUTzeo4CJJ2iVUObnFRmkbaPT6pzAXcnonlv8q8w`, // GET RID OF HARDCODED EVENTUALLY
43+
},
44+
});
45+
46+
if (!response.ok) {
47+
return res.status(500).json({ error: "Failed to fetch user from Clerk" });
48+
}
49+
50+
const user = await response.json();
51+
52+
const approved = user.public_metadata?.approved === true;
53+
res.status(200).json({ approved });
54+
}
55+
56+
export async function unapprovedHandler(req, res) {
57+
console.log("Testing unapproved handler");
58+
try {
59+
const response = await fetch("https://api.clerk.dev/v1/users", {
60+
headers: {
61+
Authorization: `Bearer ` + process.env.CLERK_SECRET_KEY,
62+
},
63+
});
64+
65+
if (!response.ok) {
66+
throw new Error(`Clerk API error: ${response.statusText}`);
67+
}
68+
69+
console.log("Response from Clerk API:", response);
70+
71+
const allUsers = await response.json();
72+
73+
// Check the format of the response
74+
const usersArray = Array.isArray(allUsers) ? allUsers : allUsers?.data || [];
75+
76+
const unapprovedUsers = usersArray
77+
.filter(user => user.public_metadata?.approved !== true)
78+
.map(user => ({
79+
id: user.id,
80+
email: user.email_addresses?.[0]?.email_address || "Unknown",
81+
name: `${user.first_name ?? ""} ${user.last_name ?? ""}`.trim()
82+
}));
83+
84+
res.status(200).json({ users: unapprovedUsers });
85+
} catch (error) {
86+
console.error("Failed to fetch users:", error);
87+
res.status(500).json({ error: "Failed to fetch unapproved users" });
88+
}
89+
}
90+
91+
export default async function handler(req, res) {
92+
const { action } = req.query;
93+
94+
console.log("Action:", action);
95+
96+
switch(action) {
97+
case 'approve':
98+
return approveHandler(req, res);
99+
case 'check':
100+
return checkHandler(req, res);
101+
case 'unapproved':
102+
return unapprovedHandler(req, res);
103+
default:
104+
return res.status(400).json({ error: 'Invalid action' });
105+
}
106+
}

frontend/api/approve-user.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

frontend/api/check-approved.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

frontend/api/get-images.js

Whitespace-only changes.

frontend/api/get-unapproved-users.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

frontend/src/app/components/SettingsPage.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,13 @@ export default function SettingsPage() {
5454

5555
useEffect(() => {
5656
const fetchUnapprovedUsers = async () => {
57+
console.log("Is admin:", isAdmin);
5758
if (!isAdmin) return;
5859
try {
59-
const response = await fetch("/api/get-unapproved-users"); // Your serverless API
60+
const response = await fetch("/api/approval?action=unapproved"); // Your serverless API
61+
// const response = await fetch("/api/get-unapproved-users")
6062
const data = await response.json();
63+
console.log("All users:", response);
6164
setApprovals(data.users); // expected shape: [{ id, email, firstName, lastName }]
6265
} catch (error) {
6366
console.error("Error fetching unapproved users:", error);
@@ -83,7 +86,7 @@ export default function SettingsPage() {
8386

8487
const approveVerification = async (id) => {
8588
try {
86-
const res = await fetch("/api/approve-user", {
89+
const res = await fetch("/api/approval?action=approve", {
8790
method: "POST",
8891
headers: {
8992
"Content-Type": "application/json",
@@ -115,6 +118,7 @@ export default function SettingsPage() {
115118
</div>
116119
<div className="body">
117120
<div className="left-column">
121+
{user && (
118122
<div className="profile-card">
119123
<h2 className="profile-card-title">Profile</h2>
120124
<div className="nameText">
@@ -159,6 +163,7 @@ export default function SettingsPage() {
159163
</div>
160164
</form>
161165
</div>
166+
)}
162167
<div className="options-card">
163168
<div className="toggle">
164169
<label className="switch">

frontend/src/app/login/page.jsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ export default function Signin() {
178178
</div>
179179
<div className="inputContainer">
180180
<label
181+
suppressHydrationWarning
181182
className="errorLabel"
182183
style={{
183184
backgroundColor: error ? "rgba(255, 44, 44, 0.2)" : "#FFFFFF",
@@ -230,9 +231,7 @@ export default function Signin() {
230231
className="textButton"
231232
onClick={() => router.push("/reset_password")}
232233
>
233-
<strong>
234-
<p className="tiny">Forgot password?</p>
235-
</strong>
234+
<strong className="tiny">Forgot password?</strong>
236235
</button>
237236
</div>
238237
<div className="inputContainer login-button">

0 commit comments

Comments
 (0)