{"id":28958688,"url":"https://github.com/nandubit/passport-dc","last_synced_at":"2026-05-09T07:02:39.263Z","repository":{"id":294388990,"uuid":"986727777","full_name":"NanduBit/passport-dc","owner":"NanduBit","description":"Passport strategy for authentication with Discord (discord.com)","archived":false,"fork":false,"pushed_at":"2025-05-20T14:15:11.000Z","size":24,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-26T01:49:00.819Z","etag":null,"topics":["0auth2","auth","authentication","discord","discord-js","express","login","session","sociallogin","website"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/passport-dc","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NanduBit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-20T03:30:38.000Z","updated_at":"2025-07-04T10:40:45.000Z","dependencies_parsed_at":"2025-06-21T15:41:32.898Z","dependency_job_id":"2fb33f18-4757-4cbd-9f71-c7d5cf8cfdf0","html_url":"https://github.com/NanduBit/passport-dc","commit_stats":null,"previous_names":["nanduwastaken/passport-dc","nandubit/passport-dc","nanduwastaken/passport-discord"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/NanduBit/passport-dc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NanduBit%2Fpassport-dc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NanduBit%2Fpassport-dc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NanduBit%2Fpassport-dc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NanduBit%2Fpassport-dc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NanduBit","download_url":"https://codeload.github.com/NanduBit/passport-dc/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NanduBit%2Fpassport-dc/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32810381,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-08T08:22:46.396Z","status":"online","status_checked_at":"2026-05-09T02:00:06.633Z","response_time":123,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["0auth2","auth","authentication","discord","discord-js","express","login","session","sociallogin","website"],"created_at":"2025-06-23T23:01:55.015Z","updated_at":"2026-05-09T07:02:39.257Z","avatar_url":"https://github.com/NanduBit.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# passport-dc\n\u003e [!NOTE]\n\u003e **Potential successor to [`passport-discord`](https://www.npmjs.com/package/passport-discord).**  \n\u003e This package aims to provide a modern, maintained, and improved Discord OAuth2 strategy for Passport.\n\n\n[Passport](http://passportjs.org/) strategy for authenticating with [Discord](https://discord.com/) using OAuth 2.0.\n\nThis module lets you authenticate using Discord in your Node.js applications. By plugging into Passport, Discord authentication can be easily and unobtrusively integrated into any application or framework that supports [Connect](http://www.senchalabs.org/connect/)-style middleware.\n\n---\n\n## Quick Start Example\n\n\nA ready-to-use example is available [Here](#full-summary-code) or at [`example/`](./example) folder(more detailed).  \nYou can run it to see how to integrate `passport-dc` with an Express app.\n\n## Installation\n\n```bash\nnpm install passport-dc\n```\n\n---\n\n## Usage\n\n### Configure Strategy\n\n```javascript\nconst passport = require('passport');\nconst DiscordStrategy = require('passport-dc').Strategy;\n\npassport.use(new DiscordStrategy({\n    clientID: 'YOUR_CLIENT_ID',\n    clientSecret: 'YOUR_CLIENT_SECRET',\n    callbackURL: 'YOUR_CALLBACK_URL',\n    scope: ['identify', 'email', 'guilds', 'guilds.join']\n}, function(accessToken, refreshToken, profile, done) {\n    profile.refreshToken = refreshToken; // store this for later use\n    // User profile returned from Discord\n    // Save user to your DB here if needed\n    return done(null, profile);\n}));\n```\n\n### Authentication Routes\n\n```javascript\napp.get('/auth/discord', passport.authenticate('discord'));\n\napp.get('/auth/discord/callback',\n    passport.authenticate('discord', { failureRedirect: '/' }),\n    function(req, res) {\n        // Successful authentication\n        res.redirect('/profile');\n    }\n);\n```\n\n### Session Support\n\n```javascript\npassport.serializeUser(function(user, done) {\n    done(null, user);\n});\npassport.deserializeUser(function(obj, done) {\n    done(null, obj);\n});\n```\n\n---\n\n## Retrieving User Profile\n\nThe user's Discord profile will be available as `req.user` after authentication.  \n\n```js\nfunction checkAuth(req, res, next) {\n    if (req.isAuthenticated()) return next();\n    res.send('not logged in :(');\n}\n\napp.get('/profile', checkAuth, function (req, res) {\n    res.json(req.user)\n});\n```\n---\n\nExample structure:\n\n```json\n{\n  \"id\": \"852381000528035890\",\n  \"username\": \"nanduwastaken\",\n  \"avatar\": \"40914b2ae07f53ee8f81f0efd1a2ba64\",\n  \"discriminator\": \"0\",\n  \"email\": \"nanduisnotdumb@gmail.com\",\n  \"verified\": true,\n  \"provider\": \"discord\",\n  \"accessToken\": \"atZNodYnY6W1ttya9ert56161OohGa\",\n  \"avatarUrl\": \"https://cdn.discordapp.com/avatars/852381000528035890/40914b2ae07f53ee8f81f0efd1a2ba64.png?size=1024\",\n  \"connections\": [ ... ],\n  \"guilds\": [ ... ],\n  \"fetchedAt\": \"2025-05-20T05:02:58.310Z\"\n}\n```\n\n---\n\n## Adding a User to a Guild\n\nThis package exposes a helper to add a user to a Discord guild (server) using the `guilds.join` scope.\n\n### Usage\n\n```javascript\nconst { addUserToGuild } = require('passport-dc');\n\n// Example usage after authentication:\naddUserToGuild(\n    'YOUR_GUILD_ID',        // Guild/server ID\n    req.user.id,            // User's Discord ID\n    req.user.accessToken,   // User's OAuth2 access token\n    'YOUR_BOT_TOKEN',       // Your bot token (must be in the guild)\n    function(err) {\n        if (err) {\n            // Handle error (user not added)\n            console.error('Failed to add user to guild:', err.message);\n        } else {\n            // Success!\n            console.log('User added to guild!');\n        }\n    }\n);\n```\n\n- The bot must be in the guild and have the `guilds.join` scope and appropriate permissions.\n- If you want to set options like nickname or roles, pass an options object before the callback: (The Bot needs to have permission to change nicknames and etc. [Read More]())\n\n```javascript\naddUserToGuild(guildId, userId, accessToken, botToken, { nick: 'CoolUser' }, callback);\n```\n---\n## Refresh Token Usage\n\nIf you need to refresh a user's Discord access token (for example, to keep them authenticated without requiring a new login), you can use the [`passport-oauth2-refresh`](https://www.npmjs.com/package/passport-oauth2-refresh) package.\n\n\n```javascript\nconst DiscordStrategy = require('passport-dc').Strategy;\nconst refresh = require('passport-oauth2-refresh');\n\nconst discordStrat = new DiscordStrategy({\n    clientID: 'YOUR_CLIENT_ID',\n    clientSecret: 'YOUR_CLIENT_SECRET',\n    callbackURL: 'YOUR_CALLBACK_URL',\n    scope: ['identify', 'email', 'guilds', 'guilds.join']\n}, function(accessToken, refreshToken, profile, done) {\n    profile.refreshToken = refreshToken; // Store for later refreshes\n    // Save user and tokens to your DB here if needed\n    return done(null, profile);\n});\n\npassport.use(discordStrat);\nrefresh.use(discordStrat);\n\n// Later, when you need to refresh the access token:\n// Make sure where ever you call this function to have the profile data, since it is passed into this function\nrefresh.requestNewAccessToken('discord', profile.refreshToken, function(err, accessToken, refreshToken) {\n    if (err) {\n        // Handle error\n        console.error('Failed to refresh access token:', err);\n        return;\n    }\n    // Use the new accessToken (and optionally update refreshToken)\n    profile.accessToken = accessToken;\n    if (refreshToken) {\n        profile.refreshToken = refreshToken;\n    }\n    // Continue with your logic\n});\n```\n\n- Make sure to store the `refreshToken` securely for each user.\n- Use the refreshed `accessToken` for future API requests on behalf of the user.\n\n\n---\n## Full Summary Code\n\n- For more detailed exmaples look at the examples at [`example/`](./example) folder.\n\n\u003cdetails\u003e\n    \n\u003csummary\u003eResulting Code\u003c/summary\u003e\n\n```js\nconst passport = require('passport');\nconst DiscordStrategy = require('passport-dc').Strategy;\nconst { addUserToGuild } = require('passport-dc');\n\n// Configure the Discord strategy\npassport.use(new DiscordStrategy({\n    clientID: 'YOUR_CLIENT_ID',\n    clientSecret: 'YOUR_CLIENT_SECRET',\n    callbackURL: 'YOUR_CALLBACK_URL',\n    scope: ['identify', 'email', 'guilds', 'guilds.join']\n}, function(accessToken, refreshToken, profile, done) {\n    profile.refreshToken = refreshToken; // store this for later use\n    // User profile returned from Discord\n    // Save user to your DB here if needed\n    return done(null, profile);\n}));\n\napp.get('/', (req, res) =\u003e {\n    res.send('Welcome! \u003ca href=\"/auth/discord\"\u003eLogin with Discord\u003c/a\u003e');\n});\n\n// Authentication routes\napp.get('/auth/discord', passport.authenticate('discord'));\n\napp.get('/auth/discord/callback',\n    passport.authenticate('discord', { failureRedirect: '/' }),\n    function(req, res) {\n        // Successful authentication\n        res.redirect('/profile');\n    }\n);\n\n// Session support\npassport.serializeUser(function(user, done) {\n    done(null, user);\n});\npassport.deserializeUser(function(obj, done) {\n    done(null, obj);\n});\n\n// Middleware to check authentication\nfunction checkAuth(req, res, next) {\n    if (req.isAuthenticated()) return next();\n    res.send('not logged in :(');\n}\n\n// Profile route\napp.get('/profile', checkAuth, function (req, res) {\n    res.json(req.user);\n});\n\n// Example: Add user to guild after authentication\napp.post('/add-to-guild', checkAuth, function(req, res) {\n    addUserToGuild(\n        'YOUR_GUILD_ID',        // Guild/server ID\n        req.user.id,            // User's Discord ID\n        req.user.accessToken,   // User's OAuth2 access token\n        'YOUR_BOT_TOKEN',       // Your bot token (must be in the guild)\n        function(err) {\n            if (err) {\n                console.error('Failed to add user to guild:', err.message);\n                return res.status(500).send('Failed to add user to guild');\n            }\n            res.send('User added to guild!');\n        }\n    );\n});\n```\n\n\u003c/details\u003e\n\n---\n\n## License\n\nMIT\n\n---\n\n## Links\n\n- [Passport](http://passportjs.org/)\n- [Discord Developer Portal](https://discord.com/developers/applications)\n- [Original passport-discord](https://github.com/nicholastay/passport-discord)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnandubit%2Fpassport-dc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnandubit%2Fpassport-dc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnandubit%2Fpassport-dc/lists"}