@oauth-everything/passport-discord
A fully typed Passport strategy for authenticating users with Discord using OAuth 2.0 and the Discord API.
Passport is authentication middleware for Node.js. It allows you to easily add user authentication to your application. It supports any application using Connect-style middleware, including Express.
@oauth-everything/passport-discord
is an authentication Strategy for Passport that allows users to authenticate using their Discord account.
Installation
$ npm install @oauth-everything/passport-discord
Setup/Configuration
Typescript Example
import express from 'express';
import passport from 'passport';
import { Strategy, Profile, VerifyCallback } from '@oauth-everything/passport-discord';
const app = express();
app.use(...);
...
...
passport.serializeUser((user: User, done) => {
done(null, );
})
passport.deserializeUser((id: string, done) => {
done(null, );
});
...
...
passport.use(new Strategy(
{
clientID: "wumpus",
clientSecret: "supmuw",
callbackURL: "https://myapp.com/auth/discord/callback",
scope: [Scope.EMAIL, Scope.GUILDS_JOIN, "webhook.incoming", ...]
},
(accessToken: string, refreshToken: string, profile: Profile, cb: VerifyCallback<User>) => {
console.log(profile);
cb(null, )
}
));
app.get("/auth/discord", passport.authenticate("discord"));
app.get("/auth/discord/callback", passport.authenticate("discord", {
failureRedirect: "/login",
successRedirect: "/"
}));
app.listen(80);
Javascript Example
const express = require('express');
const passport = require('passport');
const { Strategy } = require('@oauth-everything/passport-discord');
const app = express();
app.use(...);
...
...
passport.serializeUser((user, done) => {
done(null, );
})
passport.deserializeUser((id, done) => {
done(null, );
});
...
...
passport.use(new Strategy(
{
clientID: "wumpus",
clientSecret: "supmuw",
callbackURL: "https://myapp.com/auth/discord/callback",
scope: [Scope.EMAIL, Scope.GUILDS_JOIN, "webhook.incoming", ...]
},
(accessToken, refreshToken, profile, cb) => {
console.log(profile);
cb(null, )
}
));
app.get("/auth/discord", passport.authenticate("discord"));
app.get("/auth/discord/callback", passport.authenticate("discord", {
failureRedirect: "/login",
successRedirect: "/"
}));
app.listen(80);
Discord Application Setup
Click to expand
-
Go to https://discordapp.com/developers/applications and create a New Application
-
Copy the Client ID and Client Secret and save them somewhere safe
-
Go to the OAuth tab and add a redirect URL - this will be the URL on your website where Passport is set up
License
The MPL v2 License