@boateo6/discord-oauth2
v1.2.2
Published
An easy way for Discord OAuth2
Downloads
12
Maintainers
Readme
Welcome to Discord OAuth2 by boateo6
This is a simplified method to handle Discord OAuth2 requests.
Let's get on the setup part of this
BASIC SETUP
If you are running this on localhost then use the following for the setup process:
const auth = new OAuth2({
client_id: "Your Client ID",
client_secret: "Your Client Seceret",
redirect: {
uri: "Your redirect URI", // Example: /api/discord/auth/redirect
port: 8000, // The port you will be listening on
localhost: true // 'true' if you are running this on localhost else 'false'
}
})
If you have a domain then use the following:
const auth = new OAuth2({
client_id: "Your Client ID",
client_secret: "Your Client Seceret",
redirect: {
baseurl: "https://example.com" // The base URL of your domain
uri: "Your redirect URI", // Example: /api/discord/auth/redirect
port: 8000, // The port you will be listening on
localhost: false // 'true' if you are running this on localhost else 'false'
}
})
EVENTS
When a user authorizes their Discord account:
auth.on("new", (output,userinfo) => {
/*
if everything goes correctly then output argument will return something like this:
{
token_type: 'Bearer',
access_token: 'AN_ACCESS_TOKEN',
expires_in: 604800,
refresh_token: 'A_REFRESH_TOKEN',
scope: 'SCOPES_YOU_REQUESTED'
}
and the userinfo argument will return information about the user
*/
})
NOTE: Remember to save the access token and refresh token returned in the output argument somewhere as it will be required to get the user info in the future! If you don't save it you will have to make the user sign in again.
Getting user info
Format:
auth.getUserInfo(options,refreshCallback(data))
Here 'options' is an object with the following properties: access_token and refresh_token and 'refreshCallback' is a function that will be called in case the access token had expired and a new one was generated to return the requested user into. Here the new access_token and refresh_token will be returned in the callback so you can save the new tokens as it would be needed to get the user info like we just did.
Here is an example to request user info:
auth.getUserInfo({
access_token: "ACCESS_TOKEN_HERE", // This is the access token you saved earlier
refresh_token: "REFRESH_TOKEN_HERE" // This is the refresh token you saved earlier
},(data)=>{
/*
Update the previously saved tokens with the new tokens which are returned in the below format:
{
token_type: 'Bearer',
access_token: 'AN_ACCESS_TOKEN',
expires_in: 604800,
refresh_token: 'A_REFRESH_TOKEN',
scope: 'SCOPES_YOU_REQUESTED'
}
*/
}).then(res=>{
/*
Here res is the user info returned in the below format:
{
ok: true,
userinfo: {...}
}
here 'ok' will be 'true' or 'false' depending on if getting the user info was a success or no
and 'userinfo' will be the user's information you requested
If there was an erro it will return in the format:
{
ok: false,
error: `Failed to retrieve user info!`,
options: {...}
}
here 'ok' will be false as we failed to get the user info and 'error' will be the error message and 'options' will be the options object you passed in the getUserInfo function.
*/
})
NOTE: If the old access token had expired then a 'refresh_data' property will be added in the return object which will contain the new access token and refresh token in the below format:
{
token_type: 'Bearer',
access_token: 'AN_ACCESS_TOKEN',
expires_in: 604800,
refresh_token: 'A_REFRESH_TOKEN',
scope: 'SCOPES_YOU_REQUESTED'
}
NOTE: The Refresh Callback function will also be fired in case the old access token had expired and a new one was generated.
Full Code
// NOTE: Structure of auth will change depdning on if you are running this on localhost or not so scroll to the top of the readme to know more
const auth = new OAuth2({
client_id: "Your Client ID",
client_secret: "Your Client Seceret",
redirect: {
uri: "Your redirect URI",
port: 8000,
localhost: true
}
})
auth.on("new", (output,userinfo) => {
console.log(output,userinfo)
})
auth.getUserInfo({
access_token: "ACCESS_TOKEN",
refresh_token: "REFRESH_TOKEN"
},(data)=>{
console.log("Saving new tokens for future use!")
}).then(res=>{
console.log(res)
})
Functions
auth.isLoggedIn("ACCESS_TOKEN")
The above checks if the access token provided is still valid or no. If valid it will return 'true' else if it is invalid/expired it will return 'false'. You can use this to make it so that the user will need to sign in again after sometime (whenever the access token expires) by you making them sign in if isLoggedIn returns 'false' instead of automatically signing them in by refreshing the access token with the help of a refresh token.