从第二级域与子域的反应途径

发布于 2025-02-08 23:54:31 字数 1352 浏览 2 评论 0原文

我的React网站上有一个登录页面,该网站上有此URL(本地)

http:// localhost:3000/登录

用户登录时,服务器返回有关用户的一堆信息,包括公司列表,每一个具有子域元素。登录时,所有这些数据都将放入本地存储。

我的目标是,当用户登录到网站时,它们将立即重定向到主页,但在从该公司列出的公司列表中,一个子域与从该公司返回的公司列表匹配服务器。

到目前为止,这是一个迄今为止的内容:

  1. 此文件是一个全局实用程序文件,我将导入登录页面:
const getSubdomainHref = function() {
    const storedUser = JSON.parse(localStorage.getItem("user") || '{}')
    let newHref = window.location.origin

    if (storedUser.companies.length > 0) {
        newHref = window.location.protocol + '//' + storedUser.companies[0].subdomain + '.' + window.location.host
    } 

    return newHref
}

export { getSubdomainHref };
  1. 这是我的登录页面的一部分(重要部分)
import React, { useEffect } from 'react';
import { useNavigate } from "react-router-dom";
import { getSubdomainHref } from "./../utils";

//other code

  useEffect(() => {
    if (user.user_type !== 'anonymous') {
      let newHref = getSubdomainHref()
      navigate(newHref + "/");
    }
  }, [user, navigate]);

这是不起作用的,当我登录时会发生什么。

初始页面:

http:// localhost:3000/登录

结果页面:(注意,返回的子域是'cbd')

http:// localhost:3000/login/login/http://cbd.localhost:3000/

我如何制作我这项工作?

请注意,当我写这篇文章时,我只是一个想法,是从“ cbd.localhost”上不易获得的“ localhost”的局部发展问题。我不确定这是否是一个问题,但可能是。

There is a login page on my React site which has this url (locally)

http://localhost:3000/login

When the user logs in, the server returns a bunch of information about the user including a list of companies, each of which has a subdomain element. All of this data gets put into localStorage upon logging in.

My goal here is that when a user logs into the site, they will get immediately redirected to the home page but with a subdomain matching the first company in their list of companies returned from the server.

This is what have so far:

  1. This file is a global utility file which I will import into the login page:
const getSubdomainHref = function() {
    const storedUser = JSON.parse(localStorage.getItem("user") || '{}')
    let newHref = window.location.origin

    if (storedUser.companies.length > 0) {
        newHref = window.location.protocol + '//' + storedUser.companies[0].subdomain + '.' + window.location.host
    } 

    return newHref
}

export { getSubdomainHref };
  1. And this is part of my login page (the important part)
import React, { useEffect } from 'react';
import { useNavigate } from "react-router-dom";
import { getSubdomainHref } from "./../utils";

//other code

  useEffect(() => {
    if (user.user_type !== 'anonymous') {
      let newHref = getSubdomainHref()
      navigate(newHref + "/");
    }
  }, [user, navigate]);

This is not working, What happens when I log in is this.

Initial page:

http://localhost:3000/login

Resulting page: (note, the subdomain returned is 'cbd')

http://localhost:3000/login/http://cbd.localhost:3000/

how can I make this work?

Note, as I write this, I just had another thought, that being the problem of the localStorage from "localhost" not being readily available at "cbd.localhost". I'm not sure if this is a problem yet but it might be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-柠檬树下少年和吉他 2025-02-15 23:54:31

这里的问题是react-router-dom仅处理内部导航。这意味着它只能导航或重定向到同一React应用程序中的URL IE http:// localhost:3000。这就是为什么子域被解释为内部路径并将其附加到当前路径的原因。

如果需要导航到外部URL,则可以使用window.location对象。您可以创建一个单独的组件以进行导航,也可以直接使用它,如下所示:

import React, { useEffect } from 'react';
import { getSubdomainHref } from "./../utils";


  useEffect(() => {
    if (user.user_type !== 'anonymous') {
      let newHref = getSubdomainHref()
      window.location.href = newHref;
    }
  }, [user]);

The issue here is that the react-router-dom only handles the internal navigation. It means that it can only navigate or redirect to the URLs within the same react application i.e. http://localhost:3000. And that's why the subdomain is interpreted as an internal path and is appended to the current path.

If you need to navigate to an external URL you can use window.location object. You can either create a separate component for navigation or just directly use it as shown below :

import React, { useEffect } from 'react';
import { getSubdomainHref } from "./../utils";


  useEffect(() => {
    if (user.user_type !== 'anonymous') {
      let newHref = getSubdomainHref()
      window.location.href = newHref;
    }
  }, [user]);
情徒 2025-02-15 23:54:31

米塔尔王子是100%正确的。这是我选择解决问题的方法:

  1. 在登录页面上,如果您尚未从正确的子域中登录,将会发生什么是在响应中获得1次使用的用用图令牌,可以在短时间内登录。

  2. 一次。

    收到令牌,我将使用window.location.href方法将我的网站重定向到一个特殊页面,我将专门为此目的而制作该网站,该页面将在代币中使用,然后立即将其传递给后端,以便用户可以be重新认可。 URL看起来像这样: http://subdomain.localhost:3000/redirect/redirect/code

  3. 登录用户后,他们将使用普通反应导航方法将其重定向到主页。

Prince Mittal is 100% correct. This is what I have chosen to do to solve the problem:

  1. on the login page, if you are not already logging in from the correct subdomain what will happen is you will get in the response a 1-time use token which can be used for a short period of time to log in.

  2. once the token is received, I will use the window.location.href method to redirect my site to a special page that I will make specifically for this purpose which will take in the token and then immediately pass it to the backend so the user can be re-authenticated. the url will look something like this: http://subdomain.localhost:3000/redirect/CODE

  3. once the user is logged in they will be redirected to the home page using the normal react navigation method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文