简体   繁体   English

将身份验证令牌从 Django 传递到 React/Redux 前端

[英]Pass auth token from Django to React/Redux frontend

My situation is kind of unique.我的情况有点独特。 The website I'm working on originally used Django for both frontend and backend.我正在开发的网站最初将 Django 用于前端和后端。 Now, the Django website is also being used as a backend for a React/Redux frontend, and also uses the default Django token authentication for API calls.现在,Django 网站也被用作 React/Redux 前端的后端,并且还使用默认的 Django 令牌身份验证进行 API 调用。 This is all fine so far.到目前为止,这一切都很好。 The problem is, I'm trying to make sure the React/Redux application doesn't load unless the user is logged in on the Django website first.问题是,我试图确保除非用户先登录 Django 网站,否则 React/Redux 应用程序不会加载。 Basically you would have to login to the Django website first, in order to access the React/Redux application, otherwise you would be redirected back to the login screen of the Django application.基本上,您必须先登录 Django 网站,才能访问 React/Redux 应用程序,否则您将被重定向回 Django 应用程序的登录屏幕。

I'm wondering if there is a way to send the current user's Django authentication token through the request, to be received and stored by the React/Redux app in order to make API calls back to the Django application?我想知道是否有办法通过请求发送当前用户的 Django 身份验证令牌,由 React/Redux 应用程序接收和存储,以便将 API 调用返回给 Django 应用程序?

I've searched up online and all I can find is tutorials using a login page in the React/Redux app which is not what I want.我在网上搜索过,我能找到的只是在 React/Redux 应用程序中使用登录页面的教程,这不是我想要的。 Any help would be appreciated.任何帮助,将不胜感激。 Thank you!谢谢!

In the simplest scenario, I guess you host both apps under the same domain, you could just create an endpoint that requires regular Django authentication and generate a token.在最简单的情况下,我猜您将两个应用程序托管在同一个域下,您可以创建一个需要常规 Django 身份验证的端点并生成一个令牌。 In case of unauthenticated call to that endpoint, you would redirect to the Django login page.如果对该端点进行未经身份验证的调用,您将重定向到 Django 登录页面。

Please provide more details about your setup, then I could propose something more specific.请提供有关您的设置的更多详细信息,然后我可以提出更具体的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Django后端到React-Redux前端的对象JSON响应对象 - Object of objects JSON response from Django backend to React-Redux frontend django,反应,redux 认证令牌是 null - django , react , redux authentication token is null 如果 Django 不提供我的前端应用程序,我如何为 Django 的 auth contrib 视图获取 CSRF 令牌? - How do I get a CSRF token for Django's auth contrib views if my frontend app is not served by Django? 将数据从 React 前端传递到 Django 后端 - Passing data from React frontend to Django Backend 如何将 django 身份验证令牌传递给查看或序列化程序? - How do I pass a django auth token to view or serializer? 如何从 django rest 框架访问 jwt 令牌到 ZD18B8624A0F5F721DA7B82365FC56 - How to access a jwt token from a django rest framework to angular frontend Auth0:如何使用M2M令牌和React Frontend提交POST - Auth0: How to Submit POST Using M2M Token with React Frontend 如何修复(CSRF 令牌丢失或不正确)反应/redux + django - how to fix (CSRF token missing or incorrect ) react/redux + django Dockerize 一个 React-Django 项目,其中前端由 Django 提供 - Dockerize a React-Django project where the frontend is served from Django Django 令牌和 Session 身份验证 - Django Token and Session auth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM