Skip to content

Commit 7b2ba83

Browse files
committed
done
1 parent 5e6f166 commit 7b2ba83

File tree

8 files changed

+51
-82
lines changed

8 files changed

+51
-82
lines changed

netlify/functions/discord-auth.js

+18-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// netlify/functions/discord-auth.js
2-
const axios = require('axios');
32
const admin = require('firebase-admin');
4-
const { Client } = require('discord.js');
3+
const { Client, GatewayIntentBits, Partials } = require("discord.js");
54

65
if (!admin.apps.length) {
76
admin.initializeApp({
@@ -25,32 +24,25 @@ client.login(process.env.DISCORD_TOKEN).then();
2524
const ready = new Promise((resolve) => client.once('ready', resolve));
2625

2726
exports.handler = async function (event) {
28-
const { code, state } = event.queryStringParameters;
29-
const clientId = process.env.DISCORD_CLIENT_ID;
30-
const clientSecret = process.env.DISCORD_CLIENT_SECRET;
31-
const redirectUri = `${process.env.URL}/.netlify/functions/discord-auth`;
27+
const { access_token, state, token_type } = event.queryStringParameters;
28+
29+
if (!access_token || !state || !token_type) {
30+
return {
31+
statusCode: 400,
32+
body: 'Bad Request',
33+
};
34+
}
3235

3336
try {
34-
const tokenResponse = await axios.post('https://discord.com/api/oauth2/token', null, {
35-
params: {
36-
client_id: clientId,
37-
client_secret: clientSecret,
38-
grant_type: 'authorization_code',
39-
code,
40-
redirect_uri: redirectUri,
41-
},
37+
const response = await fetch('https://discord.com/api/users/@me', {
4238
headers: {
43-
'Content-Type': 'application/x-www-form-urlencoded',
39+
Authorization: `${token_type} ${access_token}`,
4440
},
45-
});
41+
}).then((res) => res.json());
4642

47-
const userResponse = await axios.get('https://discord.com/api/users/@me', {
48-
headers: {
49-
Authorization: `Bearer ${tokenResponse.data.access_token}`,
50-
},
51-
});
43+
console.log(response);
5244

53-
const discordId = userResponse.data.id;
45+
const discordId = response.id;
5446
const userId = decodeURIComponent(state);
5547

5648
await ready
@@ -74,11 +66,12 @@ exports.handler = async function (event) {
7466
client.destroy().then();
7567

7668
return {
77-
statusCode: 200,
78-
body: JSON.stringify({ discordInvite: invite.url }),
69+
statusCode: 302,
70+
headers: {
71+
Location: `${process.env.URL}/profile`
72+
}
7973
};
8074
} catch (error) {
81-
console.error(error);
8275
return {
8376
statusCode: 500,
8477
body: 'Internal Server Error',

package-lock.json

+2-53
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"@chakra-ui/react": "^2.8.1",
1313
"@fontsource/archivo": "^5.0.12",
1414
"@hookform/resolvers": "^3.3.1",
15-
"axios": "^1.5.1",
1615
"chakra-react-select": "^4.7.3",
1716
"dayjs": "^1.11.10",
1817
"discord.js": "^14.13.0",

src/pages/auth/discord.tsx

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {PageLoader} from "@/components/loading";
2+
import {BaseLayout} from "@/layout";
3+
import {useEffect} from "react";
4+
import {useRouter} from "next/router";
5+
6+
const DiscordAuth = () => {
7+
const router = useRouter();
8+
9+
useEffect(() => {
10+
if(typeof window === 'undefined')
11+
return;
12+
13+
const query = window.location.hash.slice(1);
14+
router.push(`/.netlify/functions/discord-auth?${query}`).then();
15+
}, [router]);
16+
17+
return (
18+
<PageLoader/>
19+
);
20+
};
21+
22+
DiscordAuth.Layout = BaseLayout;
23+
24+
export default DiscordAuth;
File renamed without changes.

src/pages/profile.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ const Index = () => {
110110

111111
const skillsArr = val?.skills?.map((el: any) => el.value);
112112
const dbData: Form = {
113-
team: null,
113+
discordId: user?.discordId || null,
114+
discordInvite: user?.discordInvite || null,
115+
team: user?.team || null,
114116
accept: user?.accept || false,
115117
college: college || user?.college || null,
116118
...val,

src/pages/wizard.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ const Wizard = () => {
6060

6161
const skillsArr = val.skills?.map((el: { value: string }) => el.value);
6262
const dbData: Form = {
63+
discordId: null,
64+
discordInvite: null,
6365
team: null,
6466
accept: null,
6567
college: college || null,

src/views/profile/components/ProfileBar.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ export const ProfileBar = ({
2626

2727
const handleConnectDiscord = () => {
2828
const state = encodeURIComponent(user.mobile);
29-
const redirectUri = encodeURIComponent(`${window.location.origin}/.netlify/functions/discord-auth`);
29+
const redirectUri = encodeURIComponent(`${window.location.origin}/auth/discord`);
3030
const clientId = process.env.NEXT_PUBLIC_CLIENT_ID;
31-
return router.push(`https://discord.com/api/oauth2/authorize?client_id=${clientId}&redirect_uri=${redirectUri}&response_type=code&scope=identify&state=${state}`);
31+
return router.push(`https://discord.com/api/oauth2/authorize?client_id=${clientId}&redirect_uri=${redirectUri}&response_type=token&scope=identify&state=${state}`);
3232
};
3333

3434
const valid = user.discordInvite?.expiry && user.discordInvite.expiry > Date.now();

0 commit comments

Comments
 (0)