I'm currently in the process of creating a dashboard for my Discord bot, but I've encountered an error that reads as follows:
TypeError: req.logIn is not a function
at Strategy.strategy.success (C:\Users\joasb\Desktop\Bot\node_modules\passport\lib\middleware\authenticate.js:247:13)
at verified (C:\Users\joasb\Desktop\Bot\node_modules\passport-oauth2\lib\strategy.js:189:20)
at Strategy._verify (C:\Users\joasb\Desktop\Bot\strategies\discord.js:38:24)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Below are the contents of my files: app.js:
require('dotenv').config()
require('./strategies/discord');
const express = require('express');
const passport = require('passport');
const app = express();
const mongoose = require('mongoose');
const PORT = process.env.PORT || 3002;
const routes = require('./routes');
mongoose.connect(`${process.env.DATABASE_URL}`, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
app.use('/api', routes);
app.use(passport.initialize());
app.use(passport.session());
app.listen(PORT, () => console.log(`Running on port ${PORT}`));
And discord.js
const passport = require('passport');
const DiscordStrategy = require('passport-discord');
const UserDB = require('../database/Schemas/UserDB');
passport.serializeUser((user, done) => {
done(null, user.discordId)
});
passport.deserializeUser(async (user, done) => {
try {
const user = await UserDB.findOne({ discordId });
return user ? done(null, user) : done(null, null);
} catch (err) {
console.log(err)
done(err, null);
}
});
passport.use(
new DiscordStrategy({
clientID: process.env.DASHBOARD_CLIENT_ID,
clientSecret: process.env.DASHBOARD_CLIENT_SECRET,
callbackURL: process.env.DASHBOARD_CALLBACK_URL,
scope: ['identify', 'guilds'],
}, async (accesToken, refreshToken, profile, done) => {
try {
const { id, username, discriminator, avatar, guilds } = profile;
console.log(id, username, discriminator, avatar, guilds);
const findUser = await UserDB.findOne({ discordId: id }, {
discordTag: `${username}#${discriminator}`,
avatar,
guilds: guilds.length,
});
if (findUser) {
console.log(`User was found!`);
return done(null, findUser);
} else {
const newUser = await UserDB.create({
discordId: id,
discordTag: `${username}#${discriminator}`,
avatar,
guilds: guilds.length,
});
return done(null, newUser);
}
} catch (err) {
console.log(err);
return;
}
})
);
I have also implemented multiple strategies which are functioning properly. I am able to authorize my client, but the error occurs afterwards and causes a crash. Do you have any suggestions on how I can resolve this issue?
EDIT: satvik choudhary recommended that I include the package.json file, so here it is:
{
"name": "quabot-db",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^10.0.0",
"express": "^4.17.1",
"mongoose": "^6.0.14",
"passport": "^0.5.0",
"passport-discord": "^0.1.4"
}
}