oauth-everything / passport-discord

A Passport strategy for authenticating with https://discordapp.com/
Mozilla Public License 2.0
19 stars 5 forks source link

TokenError: Missing "code_verifier" #15

Open mastercuber55 opened 2 years ago

mastercuber55 commented 2 years ago

My Code:

const url = require("url");
const passport = require("passport");
const bodyParser = require("body-parser");
const session = require("express-session");
const Strategy = require("passport-discord").Strategy;
const MemoryStore = require("memorystore")(session);
module.exports = {
  setup: async(app,urls,client) => {
    passport.serializeUser((user, done) => done(null, user));
  passport.deserializeUser((obj, done) => done(null, obj));

  let domain;
  let callbackUrl;

  try {
    const domainUrl = new URL(urls.domain);
    domain = {
      host: domainUrl.hostname,
      protocol: domainUrl.protocol,
    };
  } catch (e) {
    console.log(e);
    throw new Error("Invalid Site Domain/Url Was Provided!");
  }
    callbackUrl = `${domain.protocol}//${domain.host}${urls.callback}`;

  passport.use(
    new Strategy(
      {
        clientID: client.id,
        clientSecret: client.secret,
        callbackURL: callbackUrl,
        scope: client.scopes
      },
      (accessToken, refreshToken, profile, done) => {
        process.nextTick(() => done(null, profile));
      },
    ),
  );

  app.use(
    session({
      store: new MemoryStore({ checkPeriod: 86400000 }),
      secret:
                "#@%#&^$^$%@$^$&%#$%@#$%$^%&$%^#$%@#$%#E%#%@$FEErfgr3g#%GT%536c53cc6%5%tv%4y4hrgrggrgrgf4n",
      resave: false,
      saveUninitialized: false,
    }),
  );

  app.use(passport.initialize());
  app.use(passport.session());

  app.locals.domain = urls.domain.split("//")[1];

  app.use(bodyParser.json());
  app.use(
    bodyParser.urlencoded({
      extended: true,
    }),
  );
  app.get(
    urls.login,
    (req, res, next) => {
      if (req.session.backURL) {
        req.session.backURL = req.session.backURL;
      } else if (req.headers.referer) {
        const parsed = url.parse(req.headers.referer);
        if (parsed.hostname === app.locals.domain) {
          req.session.backURL = parsed.path;
        }
      } else {
        req.session.backURL = "/";
      }
      next();
    },
    passport.authenticate("discord"),
  );

  app.get(
    urls.callback,
    passport.authenticate("discord", { failureRedirect: "/" }),(
      req,
      res,
    ) => {
      if (req.session.backURL) {
        const backURL = req.session.backURL;
        req.session.backURL = null;
        res.redirect(backURL);
      } else {
        res.redirect("/");
      }
    },
  );

  app.get(urls.logout, function(req, res) {
    req.session.destroy(() => {
      req.logout();
      res.redirect("/");
    });
  });
  },
  checkAuth:(req, res, next, login) => {
    if (req.isAuthenticated()) return next();
    req.session.backURL = req.url;
    res.redirect(login);
  }
}

I Call This Thing With

require('./thatfile.js').setup(expressApp,urlPaths,clientInfo)

All I Know Is This Code Worked In The Past.

henry232323 commented 2 years ago

What exactly is the issue? Do you have an error?

ErrorDoc404 commented 1 year ago
TokenError: Missing "code_verifier"
    at OAuth2Strategy.parseErrorResponse (/home/container/node_modules/passport-oauth2/lib/strategy.js:373:12)
    at OAuth2Strategy._createOAuthError (/home/container/node_modules/passport-oauth2/lib/strategy.js:420:16)
    at /home/container/node_modules/passport-oauth2/lib/strategy.js:177:45
    at /home/container/node_modules/oauth/lib/oauth2.js:191:18
    at passBackControl (/home/container/node_modules/oauth/lib/oauth2.js:132:9)
    at IncomingMessage.<anonymous> (/home/container/node_modules/oauth/lib/oauth2.js:157:7)
    at IncomingMessage.emit (node:events:525:35)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
henry232323 commented 1 year ago

Its been a while since I'd run into this issue. I'd say double check that your client ID and secret are what you expect them to be

ErrorDoc404 commented 1 year ago

i renew my token and it worked thanx

Robz187 commented 8 months ago

i also came upon this issue i renewed my token aswell but its still the same error

snow6692 commented 2 weeks ago

i also came upon this issue i renewed my token aswell but its still the same error

Did you fix it?

Robz187 commented 1 week ago

i also came upon this issue i renewed my token aswell but its still the same error

Did you fix it?

Sadly no i just used google auth instead i dont know if its a react problem cause i found a website in angular wich uses the discord oauth with no problem but the OC is no longer working on it i am to New in coding to understand his work and update it to fix the problem