18.05.2016 Views

Mittwoch, 18. Mai, 2016

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

MongoDB CRUD the MVC way with Passport Authentication 76<br />

successfully, the user session will be created using the req.login() method provided by Passport<br />

module. After the login operation is completed, a user object will be inside the req.user object.<br />

Le errors<br />

The Connect-Flash¹³² module stores temporary messages in a session object called flash. Messages<br />

stored in the flash object will be cleared once they are presented to the user.<br />

That’s great stuff, install it:<br />

1 npm install connect-flash --save<br />

Then, add the following lines to your express.js file:<br />

1 var flash = require('connect-flash');<br />

2 app.use(flash());<br />

The Connect-Flash module exposes the req.flash() method, which allows you to create and retrieve<br />

flash messages.<br />

Show me the routes<br />

You need to wire up the routes for login and register, so in users.server.routes.js add:<br />

1 app.route('/users').post(users.create).get(users.list);<br />

2<br />

3 app.route('/users/:userId').get(users.read).put(users.update).delete(users.d\<br />

4 elete);<br />

5<br />

6 app.param('userId', users.userByID);<br />

7<br />

8 app.route('/register')<br />

9 .get(users.renderRegister)<br />

10 .post(users.register);<br />

11<br />

12 app.route('/login')<br />

13 .get(users.renderLogin)<br />

14 .post(passport.authenticate('local', {<br />

15 successRedirect: '/',<br />

16 failureRedirect: '/login',<br />

17 failureFlash: true<br />

¹³²https://www.npmjs.com/package/connect-flash

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!