react路由器重定向不保留本地存储

uxh89sit  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(178)

在我的react spa中,我实现了一个社交登录,它基本上就是有效的。
验证后的重定向url为https://example.com/oauth2,在那里,我在社交登录后成功重定向。
现在我想为我的用户保存一些闪烁,所以我想重定向oauth2-say-to home 路线。
比如:
在app.js中:

import React from 'react';
import { Switch, Route, Redirect } from 'react-router';
import Home from "./Home";
import Oauth2 from "./Oauth2";

export default (
  <Switch>
    <Route path="/" exact component={Home} />
    <Redirect from="/oauth2" to="/" />
  </Switch>
);

我遇到的“问题”是,如果我使用 react-router Redirect ,localstorage中的用户身份验证信息丢失,因此我将丢失身份验证。
如果我这样做:
在app.js中:

import React from 'react';
import { Switch, Route } from 'react-router';
import Home from "./Home";
import Oauth2 from "./Oauth2";

export default (
  <Switch>
    <Route path="/" exact component={Home} />
    <Route path="/oauth2" component={Oauth2} />
  </Switch>
);

在oauth2.js中:

import { useHistory } from "react-router-dom";
export default function Oauth2() {
  const history = useHistory();

  useEffect(() => {
    history.replace("/");
  }, [history]);

  return null;
}

localstorage是安全的,重定向是有效的(尽管我一直有https://example.com/oauth2 在地址栏里,我希望https://example.com/ ...).
我担心我使用了错误的重定向策略,或者我没有在社交登录后使用快乐路径。。。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题