How to allow only logged in user to to view static html page in react, nginx and dotnetcore backend

Viewed 24

I've a react application with username/password login authentications with dotnetcore backend. I want to add a static html page for terms and condition as a separate static html url e.g. localhost/terms/index.html. I can put terms/index.html in the public folder of the react application or serve as a separate url in the nginx. But I want to make static html page terms/index.html available to only logged in users in react app. With current implementation, terms/index html is available even after I logged out of the react application. How can I do this either in react app or in nginx configuration?

    location / {
        root   html\myreactappwithlogin;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
    
    location /terms {
        root   html\terms;
        try_files $uri /index.html =404;
    }
    
    location /api {
        proxy_pass http://localhost:9000;
    }
1 Answers

Why not just add the terms page to your react app? Then the behavior becomes the same as any other route in your application. Your nginx will always send the main index.html. React will be downloaded as usual and then the client routing will load the /terms page.

If you're worried about your react bundle size increasing because of adding this static page to your react app, you can make use of code splitting by using dynamic import syntax (import(path_to_file))

Related