npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@boateo6/discord-oauth2

v1.2.2

Published

An easy way for Discord OAuth2

Downloads

15

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.

THE END

I know all this looks complicated and even I felt the same the first time I came across this but as I learnt more I got the hang of it and made this package so I or in fact no other person has to go through the hassle of the complicated Discord OAuth2 process and can just use this package setup Discord OAuth in an easy way as compared to the actual method.

Happy coding :D