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

discord-authorize

v2.0.1

Published

A node module for easy authentication with Discord

Downloads

16

Readme

discord-authorize: Simplifying Discord Authentication with a Node Module

Introduction

discord-authorize is a powerful Node.js module that streamlines the process of authenticating users with Discord through OAuth2. This document provides a comprehensive guide on how to install, set up, and effectively utilize this module for seamless Discord authentication.

Installation

To get started, install the latest version of discord-authorize using npm:

npm install discord-authorize@latest

Usage (JavaScript)

Setup

Begin by importing the necessary components from the discord-authorize module and initializing a new instance of DiscordAuthorization.

const { DiscordAuthorization, Scopes } = require("discord-authorize");

const discord = new DiscordAuthorization({
  clientId: "YOUR_CLIENT_ID",
  clientSecret: "YOUR_CLIENT_SECRET",
  redirectUri: "YOUR_REDIRECT_URI",
});

Generating Authorization Link

Generate an OAuth2 authorization link by calling the generateOauth2Link() method, which creates a URL for users to grant permissions. This function requires scope(s) and a unique state parameter.

const scopes = [Scopes.Identity, Scopes.Email];
const state = "UNIQUE_STATE_IDENTIFIER";

const authorizationLink = discord.generateOauth2Link({ scopes: scopes }, state);

Handling Tokens

Upon successful authorization, you'll receive a code through the redirect URI's query parameter. Exchange this code for access and refresh tokens using the exchangeCodeForTokens() method. Here's an example using Express.js:

app.get("/auth/callback", async (req, res) => {
  const code = req.query.code;
  const tokens = await discord.exchangeCodeForTokens(code);

  res.cookie("access_token", tokens.accessToken);
  res.cookie("refresh_token", tokens.refreshToken);
  res.redirect("/");
});

Setting Tokens

Use the setAccessToken() and setRefreshToken() methods to set the access and refresh tokens for subsequent requests. Here's how you can do this:

discord.setAccessToken(req.cookies.access_token);
discord.setRefreshToken(req.cookies.refresh_token);

Authorized User's Information

Retrieve authorized user information through the getMyInfo() method, which returns a user object. Here's an example:

const myInfo = await discord.getMyInfo();
console.log(myInfo);

Authorized User's Connections

If you require information about a user's connections, remember to include the Connections scope while generating the OAuth2 link. Subsequently, utilize the getMyConnections() method to retrieve the connections.

const myConnections = await discord.getMyConnections();

const connectionInfo = myConnections.map((connection) => ({
  name: connection.name,
  type: connection.type,
  verified: connection.verified,
}));

The getMyConnections() method provides the following data structure:

id: string;
name: string;
type: string;
friend_sync: boolean;
metadata_visibility: number;
show_activity: boolean;
two_way_link: boolean;
verified: boolean;
visibility: number;

Joining a guild

This module is nothing without the joinGuild() method. To join a guild, you can use joinGuild() method. The method expects GuildJoinOptions which has:

guildId: snowflake;
userId: snowflake;
roles: snowflake[] | undefined;

This method requires a client (bot) in order to join a server. Now let's see the an usage of it:

const discord = new DiscordAuthorize({
  clientId: "YOUR_CLIENT_ID",
  clientSecret: "YOUR_CLIENT_SECRET",
  redirectUri: "YOUR_REDIRECT_URI",
  clientToken: "YOUR_BOT_TOKEN",
});

const response = await discord.joinGuild({
  guildId: "1148367209371021341",
  userId: "1074981842886864891",
});

res.json(response);

Get Authorized User's Guild and it's information

With the getGuilds() function, we can get the information about what guild we are in and the information about the guilds we are in. Let's see an example of using it in the code below:

const guilds = await discord.getGuilds();

res.json(guilds);

Now how to count the guild? We can do this manually but discord-authorize module provides builtin utility function to do it.

const { Utils } = require("discord-authorize");

const guilds = await discord.getGuilds();

res.json({
  totalGuilds: Utils.totalGuildCount(guilds),
});

Refreshing Access Tokens

To manage token expiration, use the refreshToken() method. This function revokes the current access token and provides a new one, ensuring uninterrupted access. Here's how you can do it:

const newAccessToken = await discord.refreshToken();
res.cookie("access_token", newAccessToken.access_token);

Full code

If you want the full code visit this

Conclusion

discord-authorize simplifies Discord authentication in Node.js applications. By following this guide, you can effortlessly integrate Discord authentication, user information retrieval, and token management into your projects.