-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add basic user registration with Discord
- Loading branch information
Showing
6 changed files
with
166 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules | ||
.env | ||
package-lock.json | ||
*.code-workspace | ||
*.code-workspace | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
[ | ||
{ | ||
"username": "test", | ||
"user_id": "123456" | ||
"user_id": 123456 | ||
}, | ||
{ | ||
"username": "Pieloaf", | ||
"user_id": "293850" | ||
"user_id": 293850 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { config } from "dotenv"; | ||
|
||
config(); | ||
|
||
const DISCORD_REDIRECT_URI = | ||
"https://bsf.pieloaf.com/auth/discord-oauth-callback"; | ||
const DISCORD_CLIENT_ID = "1122976027140956221"; | ||
const DISCORD_CLIENT_SECRET = process.env.DISCORD_CLIENT_SECRET as string; | ||
const DISCORD_API_BASE_URL = "https://discord.com/api"; | ||
|
||
export const getDiscordOAuthURL = () => { | ||
let url = new URL(`https://discord.com/api/oauth2/authorize`); | ||
url.searchParams.set("client_id", DISCORD_CLIENT_ID); | ||
url.searchParams.set("redirect_uri", DISCORD_REDIRECT_URI); | ||
url.searchParams.set("response_type", "code"); | ||
url.searchParams.set("scope", "identify guilds"); | ||
return url.toString(); | ||
}; | ||
|
||
export const getDiscorOauthToken = async (grant_code: string) => { | ||
let url = new URL(`${DISCORD_API_BASE_URL}/oauth2/token`); | ||
let body = new URLSearchParams({ | ||
client_id: DISCORD_CLIENT_ID, | ||
client_secret: DISCORD_CLIENT_SECRET, | ||
grant_type: "authorization_code", | ||
code: grant_code, | ||
redirect_uri: DISCORD_REDIRECT_URI, | ||
}); | ||
let requestData = { | ||
method: "POST", | ||
body: body, | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
}, | ||
}; | ||
|
||
let response = await fetch(url.toString(), requestData); | ||
if (response.status === 200) { | ||
return await response.json(); | ||
} else { | ||
throw new Error( | ||
`Error fetching OAuth tokens: [${response.status}] ${response.statusText}` | ||
); | ||
} | ||
}; | ||
|
||
export const getDiscordUser = async (access_token: string) => { | ||
let url = new URL(`${DISCORD_API_BASE_URL}/users/@me`); | ||
let requestData = { | ||
method: "GET", | ||
headers: { | ||
Authorization: `Bearer ${access_token}`, | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
}, | ||
}; | ||
let response = await fetch(url.toString(), requestData); | ||
return await response.json(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters