cuốn sách gpt4 ai đã làm

javascript - Express/passport - Passport.authenticate không phải là một chức năng

In lại Tác giả: Vũ trụ không gian Thời gian cập nhật: 2023-11-04 00:30:55 26 4
mua khóa gpt4 Nike

Tôi mới làm quen với Passport.js. Tôi đã theo dõi các blog sau để đăng ký người dùng

https://scotch.io/tutorials/easy-node-authentication-setup-and-local

Nhưng mỗi lần tôi chạy máy chủ và tải /đăng ký trang, nó hiển thị lỗi trên

TypeError: Passport.authenticate không phải là một chức năng tại module.exports (/home/punit/Documents/****/routes/index.js:10:34) tại Layer.handle [as hand_request] (/home/punit/ Documents/****/node_modules/express/lib/router/layer.js:95:5) tại Trim_prefix (/home/punit/Documents/****/node_modules/express/lib/router/index.js:312:13) tại /home/punit/Documents/****/node_modules/express/lib/router/ index.js:280:7 tại Function.process_params (/home/punit/Documents/****/node_modules/express/lib/router/index.js:330:12) lúc tiếp theo (/home/punit/Documents/****/node_modules/express/lib/ router/index.js:271:10) tại /home/punit/Documents/****/node_modules/connect-flash/lib/flash.js:21:5 tại Layer.handle [dưới dạng hand_request] (/home/punit/Documents/****/node_modules/express/lib/router/layer.js:95:5)

Tôi đã kiểm tra các biến được điền trong tệp /routes/index.js.

var mongoose = require('mongoose');
var hộ chiếu = require('hộ chiếu');
var flash = require('connect-flash');
var session = require('express-session');

var Routes = require('./routes/index');
var user = require('./routes/users');

mongoose.connect("localhost:27017/****");

require('./models/strategies')(passport)

var app = express();

Phiên được khởi tạo như sau

app.use(session({
bí mật: '*******',
lưu: đúng,
saveUninitialized: sai
})); // phiên bí mật
app.use(passport.initialize());
app.use(passport.session()); // phiên đăng nhập liên tục
app.use(flash()); // sử dụng connect-flash cho các tin nhắn flash được lưu trong phiên


app.use('/', tuyến đường);
app.use('/users', user);
app.use('/register', các tuyến);

require('./routes/index')(app, hộ chiếu)

Tệp chiến lược.js như sau

var LocalStrategy = require('passport-local').Strategy
var Profile = require('./profile.js')

var ex = chức năng (hộ chiếu) {

Passport.serializeUser(function (người dùng, xong) {
xong(null, user.id)
});

Passport.deserializeUser(function (id, done) {
Profile.findById(_id, function (err, user) {
xong(err, người dùng)
});
});

Passport.use('đăng ký', LocalStrategy mới({
tên người dùngField: 'email',
trường mật khẩu: 'mật khẩu',
passReqToCallback: đúng
},
chức năng (req, điện thoại di động, mật khẩu, xong) {
process.nextTick(function() {
return done(null, false);

});
}))
}

console.log(ex)

module.exports = ex;

Tệp Routes/index.js như sau

module.exports = chức năng (ứng dụng, hộ chiếu) {
/* LẤY trang chủ */
app.get('/login', function(req, res, next) {
res.render('index', { title: 'Express' });
});
app.get('/register', function(req, res, next) {
res.render('register', { title: 'Express' });
});

app.post('/register', Passport.authenticate('register', {
thành côngChuyển hướng: '/profile',
faliureRedirect: '/đăng ký',
faliureFlash: true // cho phép tin nhắn flash
}), hàm(req, res, err) {
console.log(req)
})

}

Bất kỳ loại trợ giúp sẽ được đánh giá rất cao.

câu trả lời hay nhất

Bạn nên sử dụng bộ định tuyến từ tệp tuyến đường hoặc vượt quaứng dụng

Chỉ có một sẽ làm việc.

//app.use('/', các tuyến);
//app.use('/users', user);
//app.use('/register', các tuyến);

require('./routes/index')(app, hộ chiếu)

Bằng cách này, đối tượng Passport sẽ có sẵn trong tệp lộ trình.

Về javascript - Express/passport - Passport.authenticate không phải là một chức năng, chúng tôi đã tìm thấy một câu hỏi tương tự trên Stack Overflow: https://stackoverflow.com/questions/40958812/

26 4 0
không gian vũ trụ
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress