Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var sessionStore = new RedisStore();
- this.app.use(session({
- key: 'express.sid',
- store: sessionStore,
- secret: 'mysecret'
- }));
- ///this.app.use(session({secret:'MySecret'}));
- this.app.use(passport.initialize());
- this.app.use(passport.session());
- this.app.use('/login', (req, res) => {
- res.sendFile((path.join(__dirname, '/login.html')));
- });
- this.app.use('/errorPage', (req, res) => {
- res.sendFile((path.join(__dirname, '/errorPage.html')));
- });
- this.app.get('/auth/google', passport.authenticate('google', {
- scope: [
- //'profile','email'
- 'https://www.googleapis.com/auth/plus.login',
- 'https://www.googleapis.com/auth/plus.profile.emails.read'
- ]
- }));
- this.app.get('/auth/google/callback',
- passport.authenticate('google', {
- successRedirect: '/',
- failureRedirect: '/login'
- })
- );
- this.app.use(ensureAuthenticated)
- // Static assets
- this.app.use('/', serveStatic(this.root));
- // Set app to use router as the default route
- this.app.use('*', router);
- function ensureAuthenticated(req, res, next) {
- if (req.isAuthenticated()) {
- if (!req.user._id)
- res.redirect('/errorPage');
- else
- return next();
- }
- else
- res.redirect('/auth/google');
- }
- // Get socket.io handle
- this.io = socketIo(this.server, {
- transports: ['websocket']
- });
- this.io.use(passportSocketIo.authorize({
- cookieParser: require('cookie-parser'), //optional your cookie-parser middleware function. Defaults to require('cookie-parser')
- key: 'express.sid', //make sure is the same as in your session settings in app.js
- secret: 'mysecret', //make sure is the same as in your session settings in app.js
- store: sessionStore, //you need to use the same sessionStore you defined in the app.use(session({... in app.js
- success: onAuthorizeSuccess, // *optional* callback on success
- fail: onAuthorizeFail, // *optional* callback on fail/error
- }));
- function onAuthorizeSuccess(data, accept) {
- console.log('successful connection to socket.io');
- accept(); //Let the user through
- }
- function onAuthorizeFail(data, message, error, accept) {
- if (error) accept(new Error(message));
- console.log('failed connection to socket.io:', message);
- accept(null, false);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement