[英]Passing down passportjs to individual route file in ES6
我的路线文件中有一种情况,我需要将实例化的passwordsjs变量导入到路线中。
const api = express.Router();
function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.json({ error: 'user is not logged in', status : 404 });
}
api.route('/signup')
.post(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
})
);
export default api;
我想从主index.js文件中导入护照变量到api模块,在那里我可以访问passportjs来执行注册或登录操作。
import passport from 'passport';
import mongoose from 'mongoose';
import api from './routes/api';
app.server = http.createServer(app);
mongoose.connect(db);
// 3rd party middleware
require('./config/passport')(passport);
app.use(compression());
app.use(cors({
exposedHeaders: ['Link']
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(logger('dev'));
app.use(cookieParser());
app.use(session({ proxy: true,
resave: true,
saveUninitialized: true,
secret: 's3kr3tk3y'
})
);
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(express.static(__dirname + '/../public'));
//router rules
app.set('views', __dirname + '/../public/views');
app.set('view engine', 'jade');
app.get('/', function (req, res) {
res.render('index');
});
app.use('/api', api);
app.server.listen(port, function () {
console.log(' Application started on port ' + port);
});
export default app;
如何以当前给定的格式实现此目的?
我可能喜欢这样
/** ./routes/api */
...
function apiBuilder(passport){
api.route('/signup')
.post(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
})
);
}
export default apiBuilder;
/** index.js */
import passport from 'passport';
import apiBuilder from './routes/api';
...
app.use('/api', apiBuilder(passport));
一旦您的应用程序变得更加复杂,您可能希望进一步将护照库模块化为身份验证模块:
/** ./routes/api */
...
function apiBuilder(auth){
api.route('/signup')
.post(auth.authForSignup);
}
export default apiBuilder;
/** ./path/to/authModule.js */
import passport from 'passport';
...
const auth = {
authForSignup: 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
})
// more authentication scenarios can be put here
};
export default auth;
/** index.js */
import auth from './path/to/authModule';
import apiBuilder from './routes/api';
...
app.use('/api', apiBuilder(auth));
我使用这种模式作为我的应用程序的种子 ,希望这会对你有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.