Sunday, 26 July 2020

Cannot send message with passportjs

Hi everyone, im having issues figuring out how to display a flash message on my index.ejs page.My project structure looks like this :-server.js-views- -index.ejsi can redirect the user succesfully when the user and password is correct, however when it is not , i want to display a message in the ejs index but i wasnt succesful at. Here is my server.js code:this is the passportjs part:passport.use(new LocalStrategy((username, password, cb) => { client.connect(); client.query('SELECT * FROM users where first_name = $1', [username], (err, result) => { console.log(result); if (err) { console.log(err); return cb(err); } if (result.rowCount === 1) { const first = result.rows[0]; bcrypt.compare(password, first.password, (err, res) => { console.log(first.password) if (res) { console.log("MAG") cb(null, { id: first.id, username: first.first_name }) } else { console.log("Incorrect password") cb(null, fa) } }) } if (result.rowCount === 0) { console.log("No user with that name") return cb(null, false, { message: 'No user with that name' }) } }) })) passport.serializeUser((user, done) => { done(null, user.id) }) passport.deserializeUser((id, done) => { client.query('SELECT id FROM users WHERE id = $1', [parseInt(id, 10)], (err, results) => { if (err) { console.log(err); return done(err) } done(null, results.rows[0]) }) }) ​app.use(flash()) app.use(session({ secret: process.env.SESSION, resave: false, saveUninitialized: false })) app.use(passport.initialize()) app.use(passport.session()) ​and here is where the page redirects when the username and pass is correct:app.post('/', passport.authenticate('local'), function (req, res) { res.render('index.ejs', { success: 'WELCOME' }); }); index.ejs html<% if (success != 'a'){ %>

<% success %>

<%} %>​tried adding a message there too and in return done from the local strategy but i cant seem to make it work, at this point im a little burnt out, thats why im asking for help. Thanksss and if i wasnt specific enough im sorry

Submitted July 26, 2020 at 09:54PM by rogerpeke97

No comments:

Post a Comment