首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >history.replace()在auth0类和react路由器v4中不起作用

history.replace()在auth0类和react路由器v4中不起作用
EN

Stack Overflow用户
提问于 2019-04-22 00:03:04
回答 1查看 1.1K关注 0票数 0

history.replace()在auth0类中不起作用

代码语言:javascript
运行
复制
//src/history.js

import { createBrowserHistory } from 'history';

export default createBrowserHistory();

然后,我尝试将它导入到我的Auth0类中,以使用history.replace()。这是来自类的两个函数,以及我如何尝试使用这个函数。

代码语言:javascript
运行
复制
//components/Auth.js

handleAuthentication() {
        this.auth0.parseHash((err, authResult) => {
            if (authResult && authResult.accessToken && authResult.idToken) {
                this.setSession(authResult);
            } else if (err) {
                console.log(err);
                alert(`Error: ${err.error}. Check the console for further details.`);

                location.hash = "";
                //location.pathname = "/";
                history.replace("/");
            }
        });
    }

    setSession(authResult) {
        // Set isLoggedIn flag in localStorage
        //localStorage.setItem('isLoggedIn', 'true');

        // Set the time that the access token will expire at
        let expiresAt = JSON.stringify((authResult.expiresIn * 1000) + new Date().getTime());

        localStorage.setItem("access_token", authResult.accessToken);
        localStorage.setItem("id_token", authResult.idToken);
        localStorage.setItem("expires_at", expiresAt);

        location.hash = "";
        //location.pathname = "/dashboard";
        history.replace("/dashboard");

    }

我还在这里将历史作为道具添加到我的路由器中。

代码语言:javascript
运行
复制
<Router history={history}>
            <Switch>
                <Route exact path="/" render={(props) => <Home auth={this.props.auth} {...props} />}/>
                <Route path="/dashboard" render={(props) => <Dashboard auth={this.props.auth} {...props} />}/>
                <Route path="/callback" render={(props) => <Callback auth={this.props.auth} {...props} />}/>
            </Switch>
</Router>

我读过用withRouter()包装组件,但这似乎只对组件有效,在这种情况下,我只是尝试使用一个类。

EN

回答 1

Stack Overflow用户

发布于 2019-04-23 05:54:09

在运行时,您是否在控制台中看到错误?这似乎与Auth0没有直接关系,更多的是在调用时填充到顶部的道具方面。谢谢!

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55784336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档