Passport strategy for authenticating with Discord using the OAuth 2.0 API.
# Using npm
> npm install passport-discord-auth
# Using yarn or pnpm
> yarn/pnpm add passport-discord-auth
This library supports both typescript and javascript, with ES6 modules and CommonJS.
// ES6 modules
import { Strategy } from 'passport-discord-auth';
// CommonJS
const { Strategy } = require('passport-discord-auth');
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
passport.use(
new Strategy(
{
clientId: 'CLIENT_ID',
clientSecret: 'CLIENT_SECRET',
callbackUrl: 'http://localhost:3000/auth/discord/callback',
scope: ['identify', 'guilds'],
},
// Do something with the profile
(accessToken, refreshToken, profile, done) => {
done(null, profile);
}
)
);
app.get('/auth/discord', passport.authenticate('discord'));
app.get(
'/auth/discord/callback',
passport.authenticate('discord', {
failureRedirect: '/auth/discord',
}),
(req, res) => {
res.redirect('/');
}
);
Example endpoint that returns the authenticated user:
app.get('/user', (req, res) => {
if (req.isAuthenticated()) {
res.json(req.user);
} else {
res.status(401).json({ message: 'Unauthorized' });
}
});
You can choose to import the Scope
enum and use it to specify the scopes you want to request from the user or you can use the string literals.
Example:
import { Scope } from 'passport-discord-auth';
// ...
passport.use(
new Strategy(
{
// ...
scope: [Scope.Identify, Scope.Guilds, Scope.Email],
}
// ...
)
);
Available scopes:
Scope.ActivitiesRead
oractivities.read
- Allows your app to fetch data from a user's "Now Playing/Recently Played" list — not currently available for apps.Scope.ActivitiesWrite
oractivities.write
- Allows your app to update a user's activity - not currently available for apps (NOT REQUIRED FOR GAMESDK ACTIVITY MANAGER).Scope.ApplicationBuildsRead
orapplications.builds.read
- Allows your app to read build data for a user's applications.Scope.ApplicationBuildsUpload
orapplications.builds.upload
- Allows your app to upload/update builds for a user's applications - requires Discord approval.Scope.ApplicationsCommands
orapplications.commands
- Allows your app to use commands in a guild.Scope.ApplicationsCommandsUpdate
orapplications.commands.update
- Allows your app to update its commands using a Bearer token - client credentials grant only.Scope.ApplicationsCommandsPermissionsUpdate
orapplications.commands.permissions.update
- Allows your app to update permissions for its commands in a guild a user has permissions to.Scope.ApplicationsEntitlements
orapplications.entitlements
- Allows your app to read entitlements for a user's applications.Scope.ApplicationsStoreUpdate
orapplications.store.update
- Allows your app to read and update store data (SKUs, store listings, achievements, etc.) for a user's applications.Scope.Bot
orbot
- For oauth2 bots, this puts the bot in the user's selected guild by default.Scope.Connections
orconnections
- Allows /users/@me/connections to return linked third-party accounts.Scope.DmRead
ordm_channels.read
- Allows your app to see information about the user's DMs and group DMs - requires Discord approval.Scope.Email
oremail
- Enables /users/@me to return anemail
.Scope.GdmJoin
orgdm.join
- Allows your app to join users to a group dm.Scope.Guilds
orguilds
- Allows /users/@me/guilds to return basic information about all of a user's guilds.Scope.GuildsJoin
orguilds.join
- Allows /guilds/{guild.id}/members/{user.id} to be used for joining users to a guild.Scope.GuildMembersRead
orguilds.members.read
- Allows /users/@me/guilds/{guild.id}/member to return a user's member information in a guild.Scope.Identify
oridentify
- Allows /users/@me without email.Scope.MessagesRead
ormessages.read
- For local rpc server api access, this allows you to read messages from all client channels (otherwise restricted to channels/guilds your app creates).Scope.RelationshipsRead
orrelationships.read
- Allows your app to know a user's friends and implicit relationships - requires Discord approval.Scope.RoleConnectionsWrite
orrole_connections.write
- Allows your app to update a user's connection and metadata for the app.Scope.Rpc
orrpc
- For local rpc server access, this allows you to control a user's local Discord client - requires Discord approval.Scope.RpcActivitiesUpdate
orrpc.activities.update
- For local rpc server access, this allows you to update a user's activity - requires Discord approval.Scope.RpcNotificationsRead
orrpc.notifications.read
- For local rpc server access, this allows you to receive notifications pushed out to the user - requires Discord approval.Scope.RpcVoiceRead
orrpc.voice.read
- For local rpc server access, this allows you to read a user's voice settings and listen for voice events - requires Discord approval.Scope.RpcVoiceWrite
orrpc.voice.write
- For local rpc server access, this allows you to update a user's voice settings - requires Discord approval.Scope.Voice
orvoice
- Allows your app to connect to voice on user's behalf and see all the voice members - requires Discord approval.Scope.WebhookIncoming
orwebhook.incoming
- This generates a webhook that is returned in the oauth token response for authorization code grants.