> For the complete documentation index, see [llms.txt](https://ccapeng.gitbook.io/bookstores/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ccapeng.gitbook.io/bookstores/topic-protect-react.md).

# Server Side react javascript

* The source repository is <https://github.com/ccapeng/bookstore_pro>
* Once you have react deployed to public web site, then you have exposed your code logic.
* So, put it to server side and only allow login user to accept.\
  In `frontend/templates/index.html`

  ```markup
        <div id="app"></div>
        <script src="/js/main.js"></script>
  ```

  and in `fontend/urls.py`, map `js` to `urlpatterns`.

  ```python
    urlpatterns = [
            path('', views.index),
            path('login/', views.login),
            path('logout/', views.logout),
            path('login_submit/', views.login_submit),
            path('js/<filename>', views.js)
    ]
  ```

  I have js output from django view.
* In `frontend/views.py`, the js file is only available after login. JS is a protected file, then just output file system.

  ```python
    @login_required(login_url='/login/')
    def js(request, filename):
            file_path = os.path.join(settings.BASE_DIR, "frontend", "js", filename)
            print("Load js", file_path)
            if os.path.exists(file_path):
                    with open(file_path, 'rb') as fh:
                            response = HttpResponse(fh.read(), content_type="application/javascript")
                            return response

            raise Http404
  ```

  Through this concept, server side react can be in any kind back end server.
* Also in the django restframework, all views are login required.\
  `LoginRequiredMixin` is always the first argument in the class.

  ```python
    class CategoryViewSet(LoginRequiredMixin, viewsets.ModelViewSet):
            """ Category ViewSet """
            login_url = '/login/'
            serializer_class = CategorySerializer
            queryset = Category.objects.all()
  ```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ccapeng.gitbook.io/bookstores/topic-protect-react.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
