Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. var sessionStore = new RedisStore();
  2.  
  3. this.app.use(session({
  4. key: 'express.sid',
  5. store: sessionStore,
  6. secret: 'mysecret'
  7. }));
  8. ///this.app.use(session({secret:'MySecret'}));
  9. this.app.use(passport.initialize());
  10. this.app.use(passport.session());
  11.  
  12. this.app.use('/login', (req, res) => {
  13. res.sendFile((path.join(__dirname, '/login.html')));
  14. });
  15.  
  16. this.app.use('/errorPage', (req, res) => {
  17. res.sendFile((path.join(__dirname, '/errorPage.html')));
  18. });
  19. this.app.get('/auth/google', passport.authenticate('google', {
  20. scope: [
  21. //'profile','email'
  22. 'https://www.googleapis.com/auth/plus.login',
  23. 'https://www.googleapis.com/auth/plus.profile.emails.read'
  24. ]
  25. }));
  26.  
  27. this.app.get('/auth/google/callback',
  28. passport.authenticate('google', {
  29. successRedirect: '/',
  30. failureRedirect: '/login'
  31. })
  32. );
  33. this.app.use(ensureAuthenticated)
  34. // Static assets
  35. this.app.use('/', serveStatic(this.root));
  36. // Set app to use router as the default route
  37. this.app.use('*', router);
  38.  
  39. function ensureAuthenticated(req, res, next) {
  40. if (req.isAuthenticated()) {
  41. if (!req.user._id)
  42. res.redirect('/errorPage');
  43. else
  44. return next();
  45. }
  46. else
  47. res.redirect('/auth/google');
  48. }
  49. // Get socket.io handle
  50. this.io = socketIo(this.server, {
  51. transports: ['websocket']
  52. });
  53.  
  54. this.io.use(passportSocketIo.authorize({
  55. cookieParser: require('cookie-parser'), //optional your cookie-parser middleware function. Defaults to require('cookie-parser')
  56. key: 'express.sid', //make sure is the same as in your session settings in app.js
  57. secret: 'mysecret', //make sure is the same as in your session settings in app.js
  58. store: sessionStore, //you need to use the same sessionStore you defined in the app.use(session({... in app.js
  59. success: onAuthorizeSuccess, // *optional* callback on success
  60. fail: onAuthorizeFail, // *optional* callback on fail/error
  61. }));
  62.  
  63. function onAuthorizeSuccess(data, accept) {
  64. console.log('successful connection to socket.io');
  65. accept(); //Let the user through
  66. }
  67.  
  68. function onAuthorizeFail(data, message, error, accept) {
  69. if (error) accept(new Error(message));
  70. console.log('failed connection to socket.io:', message);
  71. accept(null, false);
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement