gpt4 book ai didi

javascript - 只是无法让 Passport.js 工作

转载 作者:可可西里 更新时间:2023-11-01 10:04:24 25 4
gpt4 key购买 nike

编辑:抱歉这么久了,在编译好的JS中添加!我还添加了 Kevs 建议。

我正在开发一个 MEAN 堆栈应用程序(MongoDB、ExpressJS、Angular、NodeJS)并且玩得很开心,尤其是使用 CoffeeScript 。我的问题是我无法让 passport.js 工作,每次它返回 {success: false}。当我在 LocalStrategy 函数中执行 console.log 时,它似乎从未被调用过。知道为什么吗?

一些片段

server.coffee(主文件)

...
Account = mongoose.model 'account'

passport.use 'local-login', new LocalStrategy (_username, password, done) ->
Account.findOne {username:_username}, (error, user) ->
if error
done error
if not user
done null, false, {message:'Incorrect username.'}
done null,user
passport.serializeUser (user, done) ->
console.log 'serialize user'
if user
done null, user
passport.deserializeUser (id, done) ->
Account.findOne({_id:id}).exec (error, user) ->
if user
return done null, user
else
return done null, false
...

编译好的js

...
Account = mongoose.model('account');

passport.use('local-login', new LocalStrategy(function(_username, password, done) {
return Account.findOne({
username: _username
}, function(error, user) {
if (error) {
done(error);
}
if (!user) {
done(null, false, {
message: 'Incorrect username.'
});
}
return done(null, user);
});
}));

passport.serializeUser(function(user, done) {
console.log('serialize user');
if (user) {
return done(null, user);
}
});

passport.deserializeUser(function(id, done) {
return Account.findOne({
_id: id
}).exec(function(error, user) {
if (user) {
return done(null, user);
} else {
return done(null, false);
}
});
});
...

路线.咖啡

...
app.post '/login', (request, result, next) ->
auth = passport.authenticate 'local-login', (err, user) ->
if err
next err
if not user
result.send {success:false}
request.logIn user, (err) ->
if err
next err
result.send {success:true}
auth request, result, next
...

编译好的js

...
app.post('/login', function(request, result, next) {
var auth;
auth = passport.authenticate('local-login', function(err, user, info) {
if (err) {
next(err);
}
if (!user) {
result.send({
success: false
});
}
return request.logIn(user, function(err) {
if (err) {
next(err);
}
return result.send({
success: true
});
});
});
return auth(request, result, next);
});
...

蒙戈.咖啡

...
accountSchema = mongoose.Schema {
username: String
firstname: String
lastname: String
email: String
}

Account = mongoose.model 'account', accountSchema

Account.find({}).exec (error, collection) ->
if collection.length == 0
Account.create {
username: 'alex'
firstname: 'Alex'
lastname: 'Hxxx'
email: 'axxxxx@gmail.com'
}
...

编译好的js

  module.exports = function(config) {
var Account, accountSchema, db;
mongoose.connect("mongodb://" + config.db_host + "/" + config.db_name);
db = mongoose.connection;
db.on('error', function() {
return console.error('Database connection error');
});
db.once('open', function() {
return console.log('Database connection established');
});
accountSchema = mongoose.Schema({
username: String,
firstname: String,
lastname: String,
email: String
});
Account = mongoose.model('account', accountSchema);
return Account.find({}).exec(function(error, collection) {
if (collection.length === 0) {
return Account.create({
username: 'alex',
firstname: 'Alex',
lastname: 'Hxxx',
email: 'axxxxxxxxx@gmail.com'
});
}
});
};

特快咖啡

...
app.use cookie-parser(config.cookiesecret)
app.use session {secret: config.sessionsecret}
app.use passport.initialize()
app.use passport.session()
app.use bodyParser()
...

编译好的js

...
app.use(cookie-parser(config.cookiesecret));
app.use(session({
secret: config.sessionsecret
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser());
...

我的 Angular 登录 Controller

app.controller 'loginCtrl', ['$scope', '$http', ($scope, $http) ->
$scope.login = (username, password) ->
$http.post('/login', {username: username, password: password}).then (response) ->
console.log response.data
if response.data.success
console.log "Logged in as #{username}"
else
console.log "Failed to login as #{username}"
]

编译好的js

  app.controller('loginCtrl', [
'$scope', '$http', function($scope, $http) {
return $scope.login = function(username, password) {
return $http.post('/login', {
username: username,
password: password
}).then(function(response) {
console.log(response.data);
if (response.data.success) {
return console.log("Logged in as " + username);
} else {
return console.log("Failed to login as " + username);
}
});
};
}
]);

和我的登录表单(jade):

h3 Login
form(ng-controller="loginCtrl")
.row
.columns.small-12.large-12
form
label Username
input(placeholder="username",name="username",type="text",ng-model="username")
label Password
input(placeholder="password",name="password",type="password",no-model="password")
div(align="center")
button.button(ng-click="login(username,password)") Login
span  
a.button(href="/user/Register") Register

示例用户创建良好,所以 mongoose 没问题,并且在我开始使用 Passport 之前就已经设置好了。我学习了几个不同的教程,这让我发疯了!

最佳答案

例如,您没有设置 passport.use 的初始参数并将请求传回回调 - 来 self 的代码:

passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) {

// asynchronous
// User.findOne wont fire unless data is sent back
process.nextTick(function() {

db.Account.findOne( { ...

然后您可以使用以下方式进行身份验证:

passport.authenticate('local-login',function(err,user,info){

在您的情况下,您正在调用 passport.authenticate 'local' 因此 passport.use 中的第一个参数应该是 'local' 以便它调用正确的策略

关于javascript - 只是无法让 Passport.js 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23569317/

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