mern-passport | boilerplate example of using passport | Authentication library
kandi X-RAY | mern-passport Summary
kandi X-RAY | mern-passport Summary
A boilerplate example of using passport.js for authenticating a MERN application
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of mern-passport
mern-passport Key Features
mern-passport Examples and Code Snippets
Community Discussions
Trending Discussions on mern-passport
QUESTION
I'm trying to implement login using passport.js
with express-session
following the example here but I'm having trouble persisting logins. I notice the session cookie is not being sent on any route unless the route includes passport.authenticate('local')
, which adds the cookie I guess. This means the /getUser
route always returns no user, and a new session is added to the MongoDB store every time I login.
My routes are as follows:
...ANSWER
Answered 2020-Jul-31 at 11:49From a glance at the full source code, it doesn't look like you've set withCredentials: true
in the Axios requests you're making to your backend. Passport, at a high level, works like this:
Your
/login
and/register
routes send a cookie back in their response. This is stored by your browser for future requests to identify the session (which, by your comment, seem to be working correctly).When you want to make an authenticated request to your backend, Axios has to explicitly send the cookie back in the request if it's cross-origin — that's where
withCredentials
comes in. Without it, Axios isn't sending the cookie with the request to identify the session.
By setting withCredentials: true
, Axios should send the cookie back in the requests and Passport & express-session will use it to identify the user.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install mern-passport
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page