Create a React app with cookie authentication API Express API

My setup is this:

Node server running Express, which runs the API. Inside the subdirectory clientis the Create React App project, proxythe Express API URL is set in the field .

I run both processes and access the CRA in my browser, which sends requests to the Express API through a proxy.

It all works great.

In the production process, I will create a CRA and submit it from the Express application, for example:

app.use('/api/:controller', (req, res, next) => {
    return router(req, res, next)
})
app.get('*', (req, res) => {
    res.sendFile(path.join(`${__dirname}/client/build/index.html`));
})

My only roadblock is an authentication method. I could easily execute the onload request fetchback to the API when the React application is initialized in the browser to find out if the user has a session, but it seems like a waste.

( , ), : 1) cookie 2) currentUser index.html ( ) CRA .

, , - CRA , ? ( )

: , yarn run cra, webpack dev, React. , , .

+4

Source: https://habr.com/ru/post/1693569/


All Articles