Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
discord-authorize
Advanced tools
Readme
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.
To get started, install the latest version of discord-authorize
using npm:
npm install discord-authorize@latest
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",
});
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);
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("/");
});
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);
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);
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;
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);
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),
});
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);
If you want the full code visit this
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.
FAQs
A node module for easy authentication with Discord
The npm package discord-authorize receives a total of 1 weekly downloads. As such, discord-authorize popularity was classified as not popular.
We found that discord-authorize demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.