Saturday 6 May 2017

Pass object to router

I am new to Express.js but I want to learn it the right way. Hence I've started with using Peter Lyon's suggested code structure introduced here: https://github.com/focusaurus/express_code_structure . I am following a passport.js tutorial which requires me to pass the passport object to my router and I am stuck there

This is my index.js:

appCommon.head(app, passport)

// Load all Routers
app.use('/', require('./pages/router'))
app.use('/projects', require('./projects/router'))
app.use('/', require('./authentication/router'))(passport) // Pass passport to router

My authentication/router.js:

var express = require('express')
var join = require('path').join

var router = new express.Router()
// I removed the other functions
function processSignup (req, res) {
    passport.authenticate('local-signup', {
        successRedirect : '/profile', // redirect to the secure profile section
        failureRedirect : '/signup', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    })
}
router.use(express.static(join(__dirname, '../../wwwroot')))
router.get('/login', login)
router.get('/signup', signup)
router.post('/signup', processSignup)
router.get('/profile', isLoggedIn, profile)
router.get('/logout', logout)

module.exports = router

Questions:

  1. Did I pass the passport object correctly to my router?
  2. How can I add the passport object to my router so that the processSignup function is aware of it?


via kentor

No comments:

Post a Comment