Reputation: 1085
I am trying to use passport.js in my MEAN stack app for logging in the user. But I am confused about where do I use passport.js to authenticate the user.
Should I use it on the client side as in angular or else I should be using it in the express app and access profile information through the endpoints?
Thank you.
Upvotes: 1
Views: 3705
Reputation: 316
Passport is best suited for servers where all the views are handled by the server. It maybe a Server Side Rendered (SSR) app, or using view engines like Pug.
But when it comes down to working on a REST api server, you will see clearly that Passport is only usable if you are using strategies that would not need you redirecting to a login page and back.
An example of such strategy is passport-google
, which needs to redirect the client view to google login page then calling a callback url to hand in results.
So if you only wish to use strategies like passport-local
, passport-jwt
it is well suited for your REST api that communicates with a client side like angular, otherwise passport is for server-side authentication.
Upvotes: 1
Reputation: 476
Passport.js is middleware that is used server-side with Node.js to implement authentication. You would use Angular (or Postman if you have no front-end yet) to access profile information stored in the database using API endpoints.
There is an excellent video series by Brad Traversy on Youtube that I went through this past weekend that goes over exactly what you're asking about: MEAN Stack Front to Back, where he builds a MEAN authentication app using passport.
Since the code was written 10 months ago, it was a little outdated, but I summarized all of the code-breaking changes in my github repo: https://github.com/Stanza987/mean-auth-app-universal. You can also see the finished product in action and download my source code if you so desire.
Upvotes: 3