I have an application using Express 4 with Passport 0.3.2. I have set up a strategy passport-localthat receives the correct user information when the endpoint is /sessionsent with a username and password.
However, user information is never stored correctly. Since it is req.useralways undefined in all listeners, it req.isAuthenticated()always returns false.
I saw other messages that often find problems with ordering a middleware installation, however I ordered them in the right way, and so I'm not sure where to go from here.
Here is my listener POSTfor /session:
app.post("/session",
passport.authenticate('local'),
(req: any, res: any) => {
// if we reach this point, we authenticated correctly
res.sendStatus(201);
}
);
Here is my setup LocalStrategy:
passport.use(new LocalStrategy(
(username, password, done) => {
let users = userRepository.getAll();
let usernameFilter = users.filter(u => u.getUsername() === username);
if (!usernameFilter || usernameFilter.length !== 1) {
return done(null, false, { message: 'Incorrect username.' });
}
if (!password || password !== "correct") {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, usernameFilter[0]);
}
));
Here is my application installation:
let app = express();
app.use(cookieParser());
app.use(bodyParser.json());
app.use(expressSession({
secret: 'my secret key',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
:
"body-parser": "^1.15.1",
"cookie-parser": "^1.4.3",
"express": "^4.13.4",
"express-session": "^1.13.0",
"passport": "^0.3.2",
"passport-local": "^1.0.0"
POST /session, . :
app.post("/session",
passport.authenticate('local', {
session: false
}),
(req: express.Request, res: express.Response) => {
req.logIn(req.user, (err: any) => {
if (err)
throw err;
});
// if we reach this point, we authenticated correctly
res.sendStatus(201);
}
);
:
: