2017-10-26 5 views
0

애플리케이션 용 사용자 인증 미들웨어를 만들고 싶습니다. 나는이 인증을 위해 데이터베이스의 어떤 데이터도 사용하지 않을 것이다. 가정,NodeJS 사용자 인증 미들웨어

var user = "me"; 
function authme(){ 
//condition} 

"authme"을 라우터에서 미들웨어로 사용합니다.

app.post('/api', authme, function(res, req){ 
}) 

user = me 일 때이 API를 라우트하도록 authme 함수를 작성하고 싶습니다. 나는 이것이 매우 기초적이라는 것을 알고 있지만, 나는 이것을 가능하게 할 수 없었다. 미리 감사드립니다.

답변

1

요청 본문의 사용자로부터 로그인 자격 증명을 받게된다고 가정합니다.

의 모양은 함수 "authme"...

/* 
    Sample request body which you will receive from the client 
    { 
     "userId":"me", 
     "password":"password" 
    } 
*/  
function authme(req,res,next){ 
    if(req.body.userId=="me" && req.body.password=="random_password"){ 
     console.log("User authenticated"); 
     next(); 
    } 
    else{ 
     console.log("Wrong authentication"); 
     res.sendStatus(401); 
    } 
} 

app.post('/api', authme, function(req,res){ 
    //the user has been authenticated. 
})