Exercise 16 - Express Session - P1
Exercise 16 - Express Session - P1
Exercise 16 - Express Session - P1
Set up your Express server to use Express sessions to track authenticated users
Enable clients to access secure resources on the server after authentication.
Installing express-session
Still in the conFusionServer folder, install express-session and session-file-store Node modules
as follows:
Using express-session
...
var session = require('express-session');
...
app.use(session({
name: 'session-id',
secret: '12345-67890-09876-54321',
saveUninitialized: false,
resave: false,
}));
console.log(req.session);
if (!req.session.user) {
if (!authHeader) {
1
var err = new Error('You are not authenticated!');
res.setHeader('WWW-Authenticate', 'Basic');
err.status = 401;
next(err);
return;
req.session.user = 'admin';
next(); // authorized
} else {
res.setHeader('WWW-Authenticate', 'Basic');
err.status = 401;
next(err);
else {
console.log('req.session: ',req.session);
next();
else {
err.status = 401;
next(err);
}
2
...
Save the changes, run the server and examine the behavior.
Conclusions
In this exercise you set up the Express server to use express-session to track authenticated users to
provide access to secure resources.