1.2.0 • Published 1 year ago
passport-discord-auth v1.2.0
Example • Discord • GitHub • NPM
An updated passport authentication strategy for Discord.
Passport strategy for authenticating with Discord using the OAuth 2.0 API.
Installation
# Using npm
> npm install passport-discord-auth
# Using yarn or pnpm
> yarn/pnpm add passport-discord-authUsage
Importing
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' });
}
});Scope
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.ActivitiesReadoractivities.read- Allows your app to fetch data from a user's "Now Playing/Recently Played" list — not currently available for apps.Scope.ActivitiesWriteoractivities.write- Allows your app to update a user's activity - not currently available for apps (NOT REQUIRED FOR GAMESDK ACTIVITY MANAGER).Scope.ApplicationBuildsReadorapplications.builds.read- Allows your app to read build data for a user's applications.Scope.ApplicationBuildsUploadorapplications.builds.upload- Allows your app to upload/update builds for a user's applications - requires Discord approval.Scope.ApplicationsCommandsorapplications.commands- Allows your app to use commands in a guild.Scope.ApplicationsCommandsUpdateorapplications.commands.update- Allows your app to update its commands using a Bearer token - client credentials grant only.Scope.ApplicationsCommandsPermissionsUpdateorapplications.commands.permissions.update- Allows your app to update permissions for its commands in a guild a user has permissions to.Scope.ApplicationsEntitlementsorapplications.entitlements- Allows your app to read entitlements for a user's applications.Scope.ApplicationsStoreUpdateorapplications.store.update- Allows your app to read and update store data (SKUs, store listings, achievements, etc.) for a user's applications.Scope.Botorbot- For oauth2 bots, this puts the bot in the user's selected guild by default.Scope.Connectionsorconnections- Allows /users/@me/connections to return linked third-party accounts.Scope.DmReadordm_channels.read- Allows your app to see information about the user's DMs and group DMs - requires Discord approval.Scope.Emailoremail- Enables /users/@me to return anemail.Scope.GdmJoinorgdm.join- Allows your app to join users to a group dm.Scope.Guildsorguilds- Allows /users/@me/guilds to return basic information about all of a user's guilds.Scope.GuildsJoinorguilds.join- Allows /guilds/{guild.id}/members/{user.id} to be used for joining users to a guild.Scope.GuildMembersReadorguilds.members.read- Allows /users/@me/guilds/{guild.id}/member to return a user's member information in a guild.Scope.Identifyoridentify- Allows /users/@me without email.Scope.MessagesReadormessages.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.RelationshipsReadorrelationships.read- Allows your app to know a user's friends and implicit relationships - requires Discord approval.Scope.RoleConnectionsWriteorrole_connections.write- Allows your app to update a user's connection and metadata for the app.Scope.Rpcorrpc- For local rpc server access, this allows you to control a user's local Discord client - requires Discord approval.Scope.RpcActivitiesUpdateorrpc.activities.update- For local rpc server access, this allows you to update a user's activity - requires Discord approval.Scope.RpcNotificationsReadorrpc.notifications.read- For local rpc server access, this allows you to receive notifications pushed out to the user - requires Discord approval.Scope.RpcVoiceReadorrpc.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.RpcVoiceWriteorrpc.voice.write- For local rpc server access, this allows you to update a user's voice settings - requires Discord approval.Scope.Voiceorvoice- Allows your app to connect to voice on user's behalf and see all the voice members - requires Discord approval.Scope.WebhookIncomingorwebhook.incoming- This generates a webhook that is returned in the oauth token response for authorization code grants.
