Tell us what’s happening:
I’m working on advanced node and express, when i’m on the chat room, the username said undefined instead of my github username. How to fix this?
Your code so far
const passport = require('passport');
const LocalStrategy = require('passport-local');
const bcrypt = require('bcrypt');
const { ObjectID } = require('mongodb');
const GitHubStrategy = require('passport-github').Strategy;
module.exports = function (app, myDataBase) {
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser(async (id, done) => {
myDataBase.findOne({ _id: new ObjectID(id) }, (err, doc) => {
if (err) return console.error(err);
done(null, doc);
});
});
passport.use(new LocalStrategy((username, password, done) => {
myDataBase.findOne({ username: username }, (err, user) => {
console.log(`User ${username} attempted to log in.`);
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!bcrypt.compareSync(password, user.password)) {
return done(null, false);
}
return done(null, user);
});
}));
passport.use(new GitHubStrategy({
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: 'https://ls2t2p-3000.csb.app/auth/github/callback'
},
function (accessToken, refreshToken, profile, cb) {
console.log(profile);
myDataBase.findAndModify(
{ id: profile.id },
{},
{
$setOnInsert: {
id: profile.id,
name: profile.displayName || 'John Doe',
photo: profile.photos[0].value || '',
email: Array.isArray(profile.emails) ? profile.emails[0].value : 'No public email',
created_on: new Date(),
provider: profile.provider || ''
}, $set: {
last_login: new Date()
}, $inc: {
login_count: 1
}
},
{ upsert: true, new: true },
(err, doc) => {
return cb(null, doc.value);
}
);
}
));
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Challenge Information:
Advanced Node and Express - Send and Display Chat Messages