PassportJS and Express 4 do not save cookies / session correctly

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);
    }
);

:

:

+4
3

:

, ( req.login()) .


, , . . . . , User .

+2

Google, .

, express-session , . . .

, express-session cookie-session :

app.use(cookieSession({
    name: 'MyAppName',
    keys: ['very secret key'],
    maxAge: 30 * 24 * 60 * 60 * 1000 // 30 days
}));

PassportJS , !

0

@JamesMonger @pulse0ne, , . cookie-. , - . ,

server.get('/auth/google/callback', passport.authenticate('google'),
(req, res) => {
     res.redirect('/api/current_user');
});

After adding session: true, the session was created and I was able to extract it from the request.

server.get('/auth/google/callback', passport.authenticate('google', {session: true}),
(req, res) => {
      res.redirect('/api/current_user');
});

Versions:

    "body-parser": "^1.19.0",
    "cookie-session": "^1.3.3",
    "express": "^4.16.4",
    "passport": "^0.4.0",
0
source

All Articles