3

I am building a project which is API RESTful with NodeJS, Express & Passport. And for the Frontend of this application I want to use ReactJS.

Can I create a separate project with create-app-react for my frontend and fetch my API while keeping the benefits of Passport ? Or I should send always the user's information in my request? Or serve my Frontend on the same server of the API?

4
  • Are you asking if you can have a separate app for your API vs what serves your react app? Yes of course. Commented Nov 8, 2017 at 15:04
  • No, it's more "Can I have a separate app and benefit of the passport-local initialiazed on my API?". And if yes, should I send user data at each request on my API? Commented Nov 8, 2017 at 15:16
  • It depends how you're authenticating. If your using sessions, then you can use redis store and share sessions between two express apps. Commented Nov 8, 2017 at 15:23
  • Yes, I am using sessions. Commented Nov 8, 2017 at 15:37

1 Answer 1

1

Yes, you can have a client & a server on 2 differents port, and keep the benefits of Passport.

But for that, when you fetch you API use credentials : 'include'

Because, by default, fetch won't send or receive any cookies from the server, resulting in unauthenticated requests if the site relies on maintaining a user session (to send cookies, the credentials init option must be set).

Example:

fetch('https://example.com', {
  credentials: 'include'  
})
.then( res => {
  // Some stuff...
})
.catch(err => {
  console.log(err);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.