nodejs passport authentication token

后端 未结 3 1905
半阙折子戏
半阙折子戏 2020-12-04 04:40

I am writing a nodejs application that I would like to use as both a web application, as well as an API provider. Once a user is authenticated, I want to assign that user a

3条回答
  •  再見小時候
    2020-12-04 05:34

    You can use isAuthenticated() method in passport in nodejs. On every route you can make a check if(req.isAuthenticated()) and if it is already authenticated it will allow you to access the route or you can redirect or perform any other any other execution in else block. In Passport you can return done(null, user) for successful login and it will store the data in the cookie until the session is ended. in user you can information about the user like email, password.

    app.get('/home', (req, res) =>{
        if(req.isAuthenticated()){
            //render home page
        } else {
            // go back to the login page or throw soome error
        }
    }) 
    

提交回复
热议问题