Created
August 26, 2020 08:18
-
-
Save InsiderJanggo/f57b3ebb747cf3ab91382db65afbea9d to your computer and use it in GitHub Desktop.
A Simple Discord OAUTH2 Login
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express'); | |
| const session = require('express-session'); | |
| const PORT = 3000; | |
| const app = express(); | |
| app.set('port', PORT); | |
| app.set(express.static(__dirname +"/public")); | |
| app.set('view engine', 'ejs'); | |
| app.use(session({ | |
| secret: '48738924783748273742398747238', | |
| resave: false, | |
| saveUninitialized: false, | |
| expires: 604800000, | |
| })); | |
| require("./router")(app); | |
| app.listen(PORT, () => console.info(`Listening on port ${PORT}`)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const {Router} = require('express'); | |
| const routes = Router(); | |
| const fetch = require('node-fetch'); | |
| const FormData = require('form-data'); | |
| const SCOPE = ["identify", "guilds"] | |
| const CLIENT_ID =" "; | |
| const CLIENT_ID = ""; | |
| const REDIRECT_URL =""; | |
| const OAUTH2_LINK = ""; | |
| routes.get('/', (req, res) => { | |
| if (req.session.user) return res.redirect('/'); | |
| const authorizeUrl = OAUTH2_LINK; | |
| res.redirect(authorizeUrl); | |
| }); | |
| routes.get('/callback', (req, res) => { | |
| if (req.session.user) return res.redirect('/'); | |
| const accessCode = req.query.code; | |
| if (!accessCode) throw new Error('No access code returned frm Discord'); | |
| const data = new FormData(); | |
| data.append('client_id', CLIENT_ID); | |
| data.append('client_secret', CLIENT_SECRET); | |
| data.append('grant_type', 'authorization_code'); | |
| data.append('redirect_uri', REDIRECT_URL); | |
| data.append('scope', SCOPE.join(' ')); | |
| data.append('code', accessCode); | |
| fetch('https://discordapp.com/api/oauth2/token', { | |
| method: 'POST', | |
| body: data | |
| }) | |
| .then(res => res.json()) | |
| .then(response => { | |
| fetch('https://discordapp.com/api/users/@me', { | |
| method: 'GET', | |
| headers: { | |
| authorization: `${response.token_type} ${response.access_token}` | |
| }, | |
| }) | |
| .then(res2 => res2.json()) | |
| .then(userResponse => { | |
| userResponse.tag = `${userResponse.username}#${userResponse.discriminator}`; | |
| userResponse.avatarURL = userResponse.avatar ? `https://cdn.discordapp.com/avatars/${userResponse.id}/${userResponse.avatar}?size=256` : null; | |
| req.session.user = userResponse; | |
| res.redirect('/'); | |
| }); | |
| }); | |
| }); | |
| module.exports = routes; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = (app) => { | |
| app.use('/authorize', require('./login')); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment