Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJs frontend & Passport NodeJs backend

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?

like image 816
Jason Dill Avatar asked Apr 10 '26 18:04

Jason Dill


1 Answers

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);
});
like image 145
Sombrero Avatar answered Apr 12 '26 09:04

Sombrero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!