gpt4 book ai didi

node.js - 如何在 Controller 中使用 Passport 中间件

转载 作者:行者123 更新时间:2023-12-04 02:59:39 25 4
gpt4 key购买 nike

试图设置 Passport 本地。用过这个 guide作为灵感。

我遇到的问题是,当我从路由文件调用函数到 Controller 文件时,它不起作用。如果我将 Controller 文件中的代码放在路由文件中,它就可以工作。

这是我的 app.js

const express       = require('express');
const bodyParser = require('body-parser');
const passport = require('passport');
const flash = require('connect-flash');
const morgan = require('morgan');
const cookieParser = require('cookie-parser');
const session = require('express-session');

const Data = require('./app/models/data.js');
const User = require('./app/models/user.js');

require('./config/passport')(passport)

// create express app
const app = express();

// set the view engine to ejs
app.set('view engine', 'ejs');

// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }))

// parse requests of content-type - application/json
app.use(bodyParser.json())

// Configuring the database
const dbConfig = require('./config/database.js');
const mongoose = require('mongoose');

mongoose.Promise = global.Promise;

// Connecting to the database
mongoose.connect(dbConfig.url)
.then(() => {
console.log("Successfully connected to the database");
}).catch(err => {
console.log('Could not connect to the database. Exiting now...');
process.exit();
});

// log every request to the console
app.use(morgan(':remote-user :referrer :method :url :status :res[content-length] - :response-time ms :req[header] :res[header]'));

// read cookies (needed for auth)
app.use(cookieParser());

// required for passport
app.use(session({
secret: 'ilovescotchscotchyscotchscotch',
resave: false,
saveUninitialized: false
})); // session secret

app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions

// use connect-flash for flash messages stored in session
app.use(flash());


// Define start route <url>'/'
app.get('/', (req, res) => {
res.render('pages/index');
});



// Require routes
require('./app/routes/routes.data.js')(app);
require('./app/routes/routes.user.js')(app, passport);


// All loaded, start server
// listen for requests
app.listen(3000, () => {
console.log("Server is listening on port 3000");
});


这是routes.user.js
module.exports = (app) => {
const user = require('../controllers/controller.user.js');
const passport = require('passport');

// Visa Logga in-sidan
app.get('/login', user.login)

// Visa registreringssidan
app.get('/signup', user.signup)

// Skapa user
app.post('/signup', user.createUser)

}

这是 controller.user.js

const User          = require('../models/user.js');
const passport = require('passport');

require('../../config/passport')(passport)

exports.login = (req, res) => {
res.render('pages/login', { message: req.flash('loginMessage') });
};

exports.signup = (req, res) => {
res.render('pages/signup', { message: req.flash('signupMessage') });
};

exports.createUser = (req, res) => {
passport.authenticate('local-signup', {
successRedirect : '/all', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
});
};


当我尝试注册用户时,它超时了。没有更多,只是超时。

但是如果我从 controller.user.js 中移动这部分......

passport.authenticate('local-signup', {
successRedirect : '/all', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
});


...到routes.user.js

    // Skapa user
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/all', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));


.. 一切正常。

为什么它在 controller.user.js 文件中不起作用?

最佳答案

发现了这个; Passport Authenticate doesn't redirect

这是与 @IdanDagan 相同的解决方案发布。

我在 controller.user.js 中更改了这部分

exports.createUser = (req, res, next) => {
passport.authenticate('local-signup', {
successRedirect : '/all', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
});
};


到以下

exports.createUser = (req, res, next) => {
passport.authenticate('local-signup', {
successRedirect : '/all', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
})(req, res, next);
};


......现在它就像一个魅力!

谢谢您的帮助。希望这也能帮助其他人。

关于node.js - 如何在 Controller 中使用 Passport 中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50364757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com