froggit
froggit

Reputation: 89

express | node.js | oauth2 not authenticating

so, im using express, and passport-oauth2 / passport-discord to create a website for my discord bot, but the checkauth function always returns false, and during the auth process, i just get redirected to the main auth route and nothing happens. (i never get redirected to the dashboard)

heres the code: (i tried to include every file relative to the problem):

// strategy file 

const DiscordStrategy = require('passport-discord').Strategy;
const passport = require('passport');
const DiscordUser = require('.././models/DiscordUser');
var session = require('express-session')


passport.serializeUser((user, done) =>{
    done(null, user.id)
})

passport.deserializeUser(async (id, done) =>{
    const user = await DiscordUser.findById(id);
    if(user){
        done(null, user);
    }
})


passport.use(new DiscordStrategy({
    clientID: process.env.CLIENT_ID,
    clientSecret: process.env.CLIENT_SECRET,
    callbackURL: process.env.CLIENT_REDIRECT,
    scope: ['identify', 'guilds', 'email', 'bot']
}, async (accessToken, refreshToken, profile, done) => {
    try{
        const user = await DiscordUser.findOne({ discordId: profile.id});
        if(user) {
            console.log('a known user entered!')
            done(null, user);
        }else{
            console.log('a new user entered!')
            const newUser = await DiscordUser.create({
                discordId: profile.id,
                username: profile.username,
                discriminator: profile.discriminator,
                email: profile.email,
                guilds: profile.guilds
            });
            const savedUser = await newUser.save();
            done(null, savedUser);
        }

    }catch(err) {
        console.log(err);
        done(err, null);
    }
    
}));










//checkauth file

module.exports = async (req, res, next) => {
    if(req.isAuthenticated()){
        return next();
    } else {
        return res.redirect("/auth");
    }
};









//auth router

const router = require('express').Router();
const passport = require('passport');

router.get('/', passport.authenticate('discord'));

router.get('/redirect', passport.authenticate('discord', {
    failureRedirect: '/forbidden',
    successRedirect: '/dashboard'
}), (req, res) => {
    res.sendStatus(200);
})


module.exports = router;









//dashboard router

const router = require('express').Router();
var session = require('express-session');
const CheckAuth = require('../CheckAuth');


router.get('/', CheckAuth, (req, res) => {
    res.sendFile('../views/dashboard/index')
})


module.exports = router;

in the main file i just create a random cookie, define the routes, and use app.use(passport.initialize()) and app.use(passport.session()).

if you need anything else let me know, ty :)

Upvotes: 1

Views: 467

Answers (1)

sebastian-ruehmann
sebastian-ruehmann

Reputation: 503

In the documentation they use the callback method instead of successRedirect.

router.get('/redirect', passport.authenticate('discord', {
    failureRedirect: '/'
}), function(req, res) {
    res.redirect('/secretstuff') // Successful auth
});

Upvotes: 0

Related Questions