bradtraversy / node_passport_login

Node.js login, registration and access control using Express and Passport
1.74k stars 1.29k forks source link

req.flash doesn't work #39

Open harmnot opened 5 years ago

harmnot commented 5 years ago

I follow your instructions but I failed on req.flash, is that flash method return array?

hv7214 commented 5 years ago

Same here

inspiretk commented 4 years ago

Whats the error? You installed flash? npm install -S connect-flash and in app.js const flash = require('connect-flash')

In the app.js file, you added the middleware?

// connect flash middleware
app.use(flash())
// global variables. Creating our own middleware. Custom middleware coming from flash
app.use((req, res, next) => { // color code the message type
    res.locals.success_msg = req.flash('success_msg')
    res.locals.error_msg = req.flash('error_msg')
    res.locals.error = req.flash('error')
    next()
})

All flash does is stores the message in the cookie, so when you get redirected to another page, it has that message to display

Andrey-Yur commented 4 years ago

Seems, the problem is located in the messages.ejs file.

shashwat0802 commented 4 years ago

@inspiretk I am running into the error throw new TypeError('app.use() requires a middleware function') ^

TypeError: app.use() requires a middleware function at Function.use

I literally copy pasted these files but still this error is there

inspiretk commented 4 years ago

Did you install it? You supposed to clone the whole project, then install all the requirements: npm install.

I havn't used this for a long time, so I don't follow it anymore, but when I did it, it worked fine

Follow the tutorial video again, it should work. In the console log, sometimes it tells you which file and line the error is, pay attention to that part and see.

shashwat0802 commented 4 years ago

@inspiretk thanks for your response but I found out what the error was and thought of mentioning it here I mistakenly installed 'express-sessions' which is the wrong package it is 'express-session' instead without the s

BinahUtuedor commented 3 years ago

Same here. Not working

BinahUtuedor commented 3 years ago

I found what my error was and resolved it. I used a wrong reference success_messasges instead of success_msg

bigpreshy commented 3 years ago

I fixed this by removing the cookie property

app.use(session({
  secret:'secret',
  saveUninitialized: true,
  resave: true
}));

instead of this:

app.use(session({
    secret: 'secret',
    resave: false,
    saveUninitialized: true,
    cookie: { secure: true }
  }));

You can put it back when you deploy your application to production

nodejsdevs commented 1 week ago

https://nodeer.space/questions/some-requests-in-nodejs-may-not-have-access-to-the-reqflash-method