简体   繁体   中英

Middleware between passport and angular

First off, I'm very new to this. I'm writing a sails application with an angular front end and using passport for authentication. I would like to access the session data for the front end after a user has logged in. I've read multiple places that the data in the user object in api/policies/passport.js can only be accessed in a controller-served view, but I have no idea what would go inside this controller. All I have for this so far is the standard code inside api/policies/passport.js, assets/js/controllers.js, and assets/partials/partial1.html. So I don't have any code to show here. Can anyone show me an example of this?

Follow this tutorial to implement the passport.js authentication: https://www.bearfruit.org/2014/07/21/tutorial-easy-authentication-for-sails-js-apps/

After you've done that you can access to the season data in your homepage like this:

<html>

     Welcom back: 
     <% if(typeof user.username != 'undefined'){ user.username }%>
</html>

in the server side you can access the data like this:

index: function(req, res) {
    res.json(req.user);
}

Attach a controller to a view:

in your sails/views folder create a new file called authHp.ejs. In sails/controllers create a new controller called authHpController.js with this code:

module.exports = {

    index: function( req,res ){
        console.log("If you see this a request made to authHp.ejs and to his controller");
        res.view('authHp');
    }

};

Now go to config/routes and add this:

'/authHp': { controller: 'authHpController' }

now it should be working

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM