[英]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.