|
| 1 | +const express = require("express"); |
| 2 | +const axios = require("axios"); |
| 3 | +const { stringify } = require("../utilities/utilities.js"); |
| 4 | + |
| 5 | +const router = express.Router(); |
| 6 | + |
| 7 | +const { REDIRECT_URI, CLIENT_ID, CLIENT_SECRET } = process.env; |
| 8 | +const oauthQuery = { |
| 9 | + client_id: CLIENT_ID, |
| 10 | + redirect_uri: REDIRECT_URI, |
| 11 | + response_type: "code", |
| 12 | + scope: "identify guilds" |
| 13 | +}; |
| 14 | + |
| 15 | +router.get("/login", (req, res) => { |
| 16 | + const url = `https://discord.com/api/oauth2/authorize?${stringify( |
| 17 | + oauthQuery |
| 18 | + )}`; |
| 19 | + console.log(url) |
| 20 | + res.redirect(url); |
| 21 | +}); |
| 22 | + |
| 23 | +router.get("/logout", (req, res) => { |
| 24 | + req.session.discord = null |
| 25 | + res.redirect("/"); |
| 26 | +}) |
| 27 | + |
| 28 | +router.get("/callback", async (req, res) => { |
| 29 | + const { code, error } = req.query; |
| 30 | + if (error) return res.redirect("/"); |
| 31 | + |
| 32 | + // Check that the code was provided |
| 33 | + if (!code) { |
| 34 | + return res.status(400).json({ |
| 35 | + status: 400, |
| 36 | + message: 'Missing "code" query parameter' |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + // Call the Discord API with the provided exchange code |
| 41 | + const response = await axios |
| 42 | + .post( |
| 43 | + "https://discord.com/api/oauth2/token", |
| 44 | + stringify({ |
| 45 | + client_id: CLIENT_ID, |
| 46 | + client_secret: CLIENT_SECRET, |
| 47 | + grant_type: "authorization_code", |
| 48 | + code, |
| 49 | + redirect_uri: REDIRECT_URI, |
| 50 | + scope: oauthQuery.scope |
| 51 | + }), |
| 52 | + { headers: { "Content-Type": "application/x-www-form-urlencoded" } } |
| 53 | + ) |
| 54 | + .catch(err => null); |
| 55 | + |
| 56 | + // Check that response was successful |
| 57 | + if (!response) { |
| 58 | + return res.status(500).json({ |
| 59 | + status: 500, |
| 60 | + message: "Unkown error" |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + const { access_token, refresh_token, expires_in } = response.data; |
| 66 | + |
| 67 | + // Set cookies for access_token and refresh_token |
| 68 | + res.clearCookie("access_token"); |
| 69 | + res.clearCookie("refresh_token"); |
| 70 | + |
| 71 | + res.cookie("access_token", access_token, { |
| 72 | + expire: new Date() + expires_in * 1000 // Set cookie expire date |
| 73 | + }); |
| 74 | + res.cookie("refresh_token", refresh_token); |
| 75 | + |
| 76 | + if(!req.session.tokens){ |
| 77 | + req.session.tokens = { |
| 78 | + access: access_token, |
| 79 | + refresh: refresh_token, |
| 80 | + } |
| 81 | + } |
| 82 | + // Redirect |
| 83 | + res.redirect("/api/user"); |
| 84 | +}); |
| 85 | + |
| 86 | +// Refreshes an access_token |
| 87 | +router.post("/refresh", async (req, res) => { |
| 88 | + const { token } = req.body; |
| 89 | + |
| 90 | + // Check that a refresh token was provided |
| 91 | + if (!token) { |
| 92 | + return res.status(400).json({ |
| 93 | + status: 400, |
| 94 | + message: 'Missing "token" property in request body.' |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + // Call the Discord API to refresh the token |
| 99 | + const response = await axios.post( |
| 100 | + "https://discord.com/api/oauth2/token", |
| 101 | + stringify({ |
| 102 | + client_id: CLIENT_ID, |
| 103 | + client_secret: CLIENT_SECRET, |
| 104 | + grant_type: 'refresh_token', |
| 105 | + refresh_token: token, |
| 106 | + redirect_uri: REDIRECT_URI, |
| 107 | + scope: oauthQuery.scope |
| 108 | + }), |
| 109 | + { headers: { "Content-Type": "application/x-www-form-urlencoded" } } |
| 110 | + ).catch(err => err.response); |
| 111 | + |
| 112 | + // Respond with the call response |
| 113 | + res.status(response.status).json(response.data); |
| 114 | +}); |
| 115 | + |
| 116 | +module.exports = router; |
0 commit comments