4
노드 응용 프로그램에 여권 인증을 구현하려고하는데 응답 (res) 속성에 액세스하기 전에 리디렉션이 필요한 이유를 이해할 수 없습니까?여권 노드가 성공을 인증했습니다.
app.get('/api/loginFailure', function(req, res) {
res.status(401).json({message: 'Login Failed', success: true});
});
app.get('/api/loginSuccess', function(req, res) {
res.status(200).json({message:'Welcome!', success: true});
});
// process the login form
app.post('/api/login', passport.authenticate('local-login', {
successRedirect: '/api/loginSuccess',
failureRedirect: '/api/loginFailure'}));
여기서 알 수 있듯이 successRedirect를 사용하여 다른 경로에 액세스하여 json 응답을 되돌려 보낼 수 있습니다. 노드 api가 프론트 엔드에 대해 불가지론 자임을 의도하기 때문에 실제 애플리케이션을 리디렉션하지 않기를 바란다.
로컬 로그인 전략은 다음과 같습니다. 내 어려움이 내가 그 방법에서 돌아 오는 길에 있을지도 모른다는 생각이 든다.
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) { // callback with email and password from our form
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({
'local.email': email
},
function(err, user) {
// if there are any errors, return the error before anything else
if (err)
return done(err);
// if no user is found, return the message
if (!user) {
return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
}
// if the user is found but the password is wrong
if (!user.validPassword(password)) {
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
}
// all is well, return successful user
return done(null, user);
});
}));
나는하지 flashdata 무엇을 모두 제거하려는,하지만 지금은 단지/API에 2 개 개의 추가 API 노선을 축소 할 수있는 대한/로그인은 좋은 것입니다.
을 가이드 더를 위해 없었다 무엇인가? 출발점? 위에서 강조한 내용을 수행 할 수 있음을 이해합니다. 아직 이해할 수없는 것은 위의 동일한 논리를 적용 할 수있는 맞춤 전략에서 돌아 오는 방법입니다. – Dean
당신은 이미 이것과 함께'passport.use()'블록을 사용하려고 시도 했습니까? – t0mppa
app.post ('/ login', passport.authenticate ('local-login'), function (req, res) {})를 시도했습니다. 속성 res는 사용할 수 없습니다. 그래서 내 로컬 전략의 내 반환 진술이 잘못되었다고 생각하는 이유입니다. – Dean