18.05.2016 Views

Mittwoch, 18. Mai, 2016

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Users controller modifications<br />

Change the app/controllers/users.server.controller.js file to look like this:<br />

1 var User = require('mongoose').model('User'),<br />

2 passport = require('passport');<br />

3<br />

4 var getErrorMessage = function(err) {<br />

5 var message = '';<br />

6 if (err.code) {<br />

7 switch (err.code) {<br />

8 case 11000:<br />

9 case 11001:<br />

10 message = 'Username already exists';<br />

11 break;<br />

12 default:<br />

13 message = 'Something went wrong';<br />

14 }<br />

15 }<br />

16 else {<br />

17 for (var errName in err.errors) {<br />

18 if (err.errors[errName].message)<br />

19 message = err.errors[errName].message;<br />

20 }<br />

21 }<br />

22<br />

23 return message;<br />

24 };<br />

25<br />

26 exports.renderLogin = function(req, res, next) {<br />

27 if (!req.user) {<br />

28 res.render('login', {<br />

29 title: 'Log-in Form',<br />

30 messages: req.flash('error') || req.flash('info')<br />

31 });<br />

32 }<br />

33 else {<br />

34 return res.redirect('/');<br />

35 }<br />

36 };<br />

37<br />

38 exports.renderRegister = function(req, res, next) {<br />

39 if (!req.user) {

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

Saved successfully!

Ooh no, something went wrong!