Middleware between passport and angular

419 views Asked by At

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?

1

There are 1 answers

8
Matan Gubkin On BEST ANSWER

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