Passport.authenticate

编程入门 行业动态 更新时间:2024-10-28 06:33:32
本文介绍了Passport.authenticate-用户名和密码为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这可能是一些基本错误,但是我正在看教程,尽管我认为提交登录表单后所做的一切都与我完全一样,但我仍被重定向到"failureRedirect"页面.当我查看护照模块中的源代码时,我有所了解.

this is probably some basic mistake but I am watching tutorial and even though I think I done everything exactly like I should after submitting login form I am redirected to the "failureRedirect" page. When I looked at source code in passport module I something.

之后:

Strategy.prototype.authenticate = function(req, options) { options = options || {}; var username = lookup(req.body, this._usernameField) || lookup(req.query, this._usernameField); var password = lookup(req.body, this._passwordField) || lookup(req.query, this._passwordField); //I added: console.log("U-> " + username); console.log("P-> " + password);

控制台说

U-> null P-> null

然后,不执行其余操作.

Then after this, rest is not executed.

if (!username || !password) { return this.fail({ message: options.badRequestMessage || 'Missing credentials' }, 400); }

我不确定应该在此处发布代码的哪些部分.也许可以帮忙

I am not sure which parts of code should I post here. Maybe this can help

passport.use(new LocalStrategy( function(username, password, done){ console.log("passport.use new LocalStrategy"); //never gets executed

//永远不会执行

//never gets executed

User.getUserByUsername(username, function(err, user){ if (err) throw err; if(!user) { console.log("Unknown user"); return done(null, false, {message: "Uknkown User"}); } UserparePassword(password, user.password, function(err, isMatch){ if (err) throw err; if (isMatch) { return done(null, user); } else { console.log("invalid Pass"); return done(null, false, {message: "Invalid Password"}); } }); }); })); router.post("/login", passport.authenticate("local", {failureRedirect:"/users/login/err", failureFlash:"invalid username or pass"}), function(req, res){ console.log("Authenticated OK"); req.flash("success", "You are logged in"); res.redirect("/xx"); });

推荐答案

我不确定您正在执行的确切实现.可能您正在使用原型模式覆盖身份验证功能.但是,使用Passportjs进行身份验证很简单.我最近在我的副项目中做了一个.请通过下面的链接,以我自己的经验来实现Passportjs我在我的技术博客上写了一篇有据可查的文章.希望这对您有帮助

I am not sure about the exact implementation that you are doing. Probably you are overriding the authenticate functionality using the prototype pattern. However, Authentication using Passportjs is simple. I have done one recently in my side project. Please go through the below link with my own experience on implementing Passportjs I have a well documented artcile that i wrote on my tech blog. Hope this helps you

// complete code for the exmaple node rest api authentication using passport var express = require('express'); var passport = require('passport'); var passportHttp = require('passport-http'); var basicStrategy = passportHttp.BasicStrategy; // using the basic authentication var app = express(); app.get('/',function(req,res){ res.send("There you go"); }); app.use(passport.initialize()); // initialize and use it in express passport.use(new passportHttp.BasicStrategy(function(username,password,done) { if(username === password){ done(null,username); //null means no error and return is the username } else{ return done(null,'there is no entry for you!'); // null means nothing to say, //no error. 2nd is the custom statement business rule } })); // this function hits first when there is an API call. function ensureAuthenticated(req,res,next){ if(req.isAuthenticated()){ next(); // next redirects the user to the next api function waiting to be executed in the express framework }else{ res.sendStatus(403); //forbidden || unauthorized } }; // this means all the API calls that hit via mydomain/api/ uses this authentication. //session is false, because its a HTTP API call. // setting this helps passport to skip the check if its an API call or a session web call app.use('/api',passport.authenticate('basic',{session:false})); // this is served the user once the authentication is a susccess app.get('/api/data',ensureAuthenticated,function(req,res){ var somevalue = [{name: 'foo'}, {name: 'bar'}, {name: 'baz'}]; res.send(somevalue); }); app.listen(3250); console.log('listening to port on ' + 3250);

更多推荐

Passport.authenticate

本文发布于:2023-10-20 01:03:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1509363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Passport   authenticate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!