Skip to content

Commit b26742b

Browse files
committed
feat(dashboard): actually works
1 parent 783ace1 commit b26742b

File tree

5 files changed

+71
-56
lines changed

5 files changed

+71
-56
lines changed

api/src/index.ts

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
const app = express();
4141
const PORT = 18103;
4242

43-
app.use(cors());
43+
// app.use(cors());
4444
app.use(express.json());
4545
app.use((req, _res, next) => {
4646
if (req.headers.cookie) {
@@ -274,7 +274,7 @@ app.get("/get/tracking/:guild/:user", async (req, res) => {
274274
return res.status(200).json(data);
275275
});
276276

277-
app.get("/get/:guild/:user", async (req, res) => {
277+
app.get("/get/:guild/:user", cors(), async (req, res) => {
278278
const { guild, user } = req.params;
279279

280280
const [err, result] = await getUser(user, guild);
@@ -289,7 +289,7 @@ app.get("/get/:guild/:user", async (req, res) => {
289289
}
290290
});
291291

292-
app.get("/get/:guild", async (req, res) => {
292+
app.get("/get/:guild", cors(), async (req, res) => {
293293
const { guild } = req.params;
294294

295295
const [guildErr, guildData] = await getGuild(guild);
@@ -834,8 +834,16 @@ app.get("/auth/callback", async (req, res) => {
834834
res.redirect(`${WEBSITE_URL}/dashboard`);
835835
});
836836

837+
app.options(
838+
"/user/me",
839+
cors({
840+
origin: ["http://localhost:56413", "https://chatr.fun"],
841+
credentials: true,
842+
})
843+
);
844+
837845
app.get(
838-
"/auth/user",
846+
"/user/me",
839847
cors({
840848
origin: ["http://localhost:56413", "https://chatr.fun"],
841849
credentials: true,
@@ -854,8 +862,8 @@ app.get(
854862
}
855863
);
856864

857-
app.post(
858-
"/auth/logout",
865+
app.delete(
866+
"/user/me",
859867
cors({
860868
origin: ["http://localhost:56413", "https://chatr.fun"],
861869
credentials: true,
@@ -871,7 +879,48 @@ app.post(
871879
}
872880
);
873881

874-
app.get("/auth/user/guilds", async (req, res) => {
882+
app.options(
883+
"/dashboard/update-guild",
884+
cors({
885+
origin: ["http://localhost:56413", "https://chatr.fun"],
886+
credentials: true,
887+
})
888+
);
889+
890+
app.post(
891+
"/dashboard/update-guild",
892+
cors({
893+
origin: ["http://localhost:56413", "https://chatr.fun"],
894+
credentials: true,
895+
}),
896+
async (req, res) => {
897+
if (!(await getUserFromRequest(req)))
898+
return res.status(401).json({ message: "Unauthorized" });
899+
900+
const body = req.body;
901+
const { guild } = req.body;
902+
903+
if (!guild) return res.status(400).json({ message: "Illegal request" });
904+
905+
if (body.cooldown) {
906+
await setCooldown(guild, body.cooldown);
907+
}
908+
909+
if (body.updates.enabled === true) {
910+
await enableUpdates(guild);
911+
} else if (body.updates.enabled === false) {
912+
await disableUpdates(guild);
913+
}
914+
915+
if (body.updates.channel) {
916+
await setUpdatesChannel(guild, body.updates.channel);
917+
}
918+
919+
return res.sendStatus(200);
920+
}
921+
);
922+
923+
app.get("/user/me/guilds", async (req, res) => {
875924
const user = await getUserFromRequest(req);
876925

877926
if (!user) return res.status(401).json({ message: "Unauthorized" });
@@ -925,49 +974,9 @@ app.get("/auth/user/guilds", async (req, res) => {
925974
);
926975
});
927976

928-
app.options(
929-
"/auth/update-guild",
930-
cors({
931-
origin: ["http://localhost:56413", "https://chatr.fun"],
932-
credentials: true,
933-
})
934-
);
935-
app.put(
936-
"/auth/update-guild",
937-
cors({
938-
origin: ["http://localhost:56413", "https://chatr.fun"],
939-
credentials: true,
940-
}),
941-
async (req, res) => {
942-
if (!(await getUserFromRequest(req)))
943-
return res.status(401).json({ message: "Unauthorized" });
944-
945-
const body = req.body;
946-
const { guild } = req.body;
947-
948-
if (!guild) return res.status(400).json({ message: "Illegal request" });
949-
950-
if (body.cooldown) {
951-
await setCooldown(guild, body.cooldown);
952-
}
953-
954-
if (body.updates.enabled === true) {
955-
await enableUpdates(guild);
956-
} else if (body.updates.enabled === false) {
957-
await disableUpdates(guild);
958-
}
959-
960-
if (body.updates.channel) {
961-
await setUpdatesChannel(guild, body.updates.channel);
962-
}
963-
964-
return res.sendStatus(204);
965-
}
966-
);
967-
968977
// TODO: fetch from the bot itself using discord.js
969978
// (would allow us to do permission filtering)
970-
app.get("/channels/:guild", authMiddleware, async (req, res) => {
979+
app.get("/dashboard/channels/:guild", authMiddleware, async (req, res) => {
971980
const { guild } = req.params;
972981

973982
const channelsResponse = await fetch(

web/components/navbar.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,13 @@ export const Navbar = () => {
115115
/>
116116
</DropdownTrigger>
117117
<DropdownMenu aria-label="User menu">
118-
<DropdownItem as={NextLink} href="/dashboard">
118+
<DropdownItem>
119+
<p className="text-xs">Signed in as</p>
120+
<p className="text-base font-bold">
121+
{user.name}
122+
</p>
123+
</DropdownItem>
124+
<DropdownItem href="/dashboard">
119125
Dashboard
120126
</DropdownItem>
121127
<DropdownItem

web/lib/queries.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const useUser = () => {
2525
const query = useQuery<User | null>({
2626
queryKey: ["user"],
2727
queryFn: async () => {
28-
const res = await fetch(`${API_URL}/auth/user`, {
28+
const res = await fetch(`${API_URL}/user/me`, {
2929
credentials: "include",
3030
});
3131

web/pages/dashboard/[server].tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default function Dashboard({
3434

3535
const onSubmit = useCallback(async (e: FormEvent) => {
3636
e.preventDefault();
37-
await fetch(`${API_URL}/auth/update-guild`, {
37+
await fetch(`${API_URL}/dashboard/update-guild`, {
3838
body: JSON.stringify({
3939
guild: guild.id,
4040
cooldown: parseInt(cooldown) * 1000,
@@ -47,7 +47,7 @@ export default function Dashboard({
4747
headers: {
4848
"Content-Type": "application/json",
4949
},
50-
method: "PUT",
50+
method: "POST",
5151
});
5252
}, []);
5353

@@ -135,7 +135,7 @@ export default function Dashboard({
135135
}
136136

137137
export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
138-
const userResponse = await fetch(`${API_URL}/auth/user`, {
138+
const userResponse = await fetch(`${API_URL}/user/me`, {
139139
headers: {
140140
cookie: ctx.req.headers.cookie ?? "",
141141
},
@@ -157,7 +157,7 @@ export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
157157
});
158158

159159
const channelsResponse = await fetch(
160-
`${API_URL}/channels/${ctx.params!.server}`,
160+
`${API_URL}/dashboard/channels/${ctx.params!.server}`,
161161
{
162162
headers: {
163163
Authorization: process.env.AUTH!,

web/pages/dashboard/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default function Dashboard({
7373
}
7474

7575
export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
76-
const userResponse = await fetch(`${API_URL}/auth/user`, {
76+
const userResponse = await fetch(`${API_URL}/user/me`, {
7777
headers: {
7878
cookie: ctx.req.headers.cookie ?? "",
7979
},
@@ -88,7 +88,7 @@ export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
8888
},
8989
};
9090

91-
const guildsResponse = await fetch(`${API_URL}/auth/user/guilds`, {
91+
const guildsResponse = await fetch(`${API_URL}/user/me/guilds`, {
9292
headers: {
9393
cookie: ctx.req.headers.cookie ?? "",
9494
},

0 commit comments

Comments
 (0)