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-auth.ts

v1.0.9

Published

This package provides a comprehensive and easy-to-use solution for implementing Discord OAuth2 authentication in your TypeScript applications. Designed with simplicity and flexibility in mind, it allows developers to quickly integrate Discord's OAuth2 aut

Downloads

14

Readme

discord-auth.ts


This package provides a comprehensive and easy-to-use solution for implementing Discord OAuth2 authentication in your TypeScript applications. Designed with simplicity and flexibility in mind, it allows developers to quickly integrate Discord's OAuth2 authentication flow into their projects, enabling secure and seamless user authentication.


Table of content


Installing

To install the package, simply run the following command in your project directory:

npm install discord-auth.ts@latest

or

 npm install git+https://github.com/Marco12223/discord-auth.ts.git

to load the latest version from the repository.


Usage

Typescript

import { DiscordAuth, Scopes } from 'discord-auth.ts';

const oauth2 = new DiscordAuth('CLIENT_ID', 'CLIENT_SECRET', 'REDIRECT_URI', [Scopes.IDENTIFY, Scopes.EMAIL]);

The DiscordAuth class takes four parameters:

  • CLIENT_ID: Your Discord application's client ID.
  • CLIENT_SECRET: Your Discord application's client secret.
  • REDIRECT_URI: The URI to redirect users to after authentication.
  • SCOPES: An array of OAuth2 scopes to request from the user.

Once you've created an instance of the DiscordAuth class, you can use its methods to generate the necessary URLs and handle the authentication flow. Here's an example of how you can generate the authorization URL and redirect users to it:

const oauth2Link = oauth2.getAuthUrl();

This will return a URL that you can redirect users to in order to start the authentication flow. After the user has authenticated with Discord, they will be redirected back to the REDIRECT_URI you provided earlier. You can then use the tokenExchange method to exchange the authorization code for an access token:

const code = req.query.code; // Get the authorization code from the request (Express example), of course you can get it from anywhere.
const token = await oauth2.accessHandler().tokenExchange(code)

This will return an object containing the access token, refresh token, and token expiration time. You can use the access token to make authenticated requests to the Discord API on behalf of the user.

Javascript

To use the package in JavaScript, you can follow the same steps as in TypeScript, but without the type annotations. Here's an example of how you can set up the authentication flow in your application:

const { DiscordAuth, Scopes } = require('discord-auth.ts/dist/discordAuth.js');

const oauth2 = new DiscordAuth('CLIENT_ID', 'CLIENT_SECRET', 'REDIRECT_URI', [Scopes.IDENTIFY, Scopes.EMAIL]);

The DiscordAuth class takes four parameters:

  • CLIENT_ID: Your Discord application's client ID.
  • CLIENT_SECRET: Your Discord application's client secret.
  • REDIRECT_URI: The URI to redirect users to after authentication.
  • SCOPES: An array of OAuth2 scopes to request from the user.

Once you've created an instance of the DiscordAuth class, you can use its methods to generate the necessary URLs and handle the authentication flow. Here's an example of how you can generate the authorization URL and redirect users to it:

const oauth2Link = oauth2.getAuthUrl();

This will return a URL that you can redirect users to in order to start the authentication flow. After the user has authenticated with Discord, they will be redirected back to the REDIRECT_URI you provided earlier. You can then use the tokenExchange method to exchange the authorization code for an access token:

const code = req.query.code; // Get the authorization code from the request (Express example), of course you can get it from anywhere.
const token = await oauth2.accessHandler().tokenExchange(code)

This will return an object containing the access token, refresh token, and token expiration time. You can use the access token to make authenticated requests to the Discord API on behalf of the user.

Example

Here's a complete example of how you can set up the authentication flow in an Express application (For TypeScript but it can be easily converted to JavaScript):

import express from 'express';
import { DiscordAuth, Scopes } from 'discord-auth.ts';
const app = express(); // Create a new Express application
const port = 3000; // Port to run the Express server on

const oauth2 = new DiscordAuth('CLIENT_ID', 'CLIENT_SECRET', 'http://localhost:3000/auth', [Scopes.IDENTIFY, Scopes.EMAIL]); // Create a new instance of the DiscordAuth class

// Initial route to start the OAuth2 flow (http://localhost:3000/)
app.get('/', (req, res) => {
    const oauth2Link = oauth2.getAuthUrl();
    res.redirect(oauth2Link);
});

// Redirect URI for the OAuth2 flow (http://localhost:3000/auth)
app.get('/auth', async (req, res) => {
    const code = req.query.code; // Get the authorization code from the request
    const accessToken = await oauth2.accessHandler().tokenExchange(code); // Exchange the authorization code for an access token
    const userData = await oauth2.user(accessToken).getUser(); // Get the user's information from Discord
    res.send(userData.id); // Respond with the user's Discord ID
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`); // Start the Express server
});

Documentation

We currently do not have a dedicated documentation page, but you can find detailed information about the package and its methods in the source code. The package is designed to be simple and intuitive to use, so you should be able to get started quickly by following the examples provided above.


License

This package is licensed under the MIT License. You are free to use, modify, and distribute it as you see fit. For more information, please refer to the LICENSE file.