我有一个登录表单,在单击时调用以下函数。
然后使用this.props.history.push()
重定向
handleSubmit(event) {
event.preventDefault();
const { email, password } = this.state;
axios({
method: 'post',
url: 'http://localhost:3003/login',
data: qs.stringify({ email, password }),
headers: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
}
}).then(res => {
console.log("set cookie")
//the accestoken is set as a cookie in order to check routes
Cookies.set('accesstoken', res.data.accesstoken);
console.log("those are the props")
console.log(this.props);
this.props.history.push('/'); //the bug
}).catch(err => {
console.log(err)
})
}
但我面临的问题是,当我第一次登录时,重定向无法工作。它设置了所有的cookie等等,但是它从来没有将用户重定向到想要的目录。所以我被迫在浏览器搜索栏中输入我想要的路由来重定向自己。
这意味着,一旦我强迫自己进入想要的目录,如果我注销(基本上删除cookies),并尝试再次登录。这一次重定向起作用了。
在我用Ctrl+F5清除所有缓存之前,它会正常工作,这恰好导致了我在第一次登录时出现的相同问题,所以我不得不再次将自己重定向为manuall。
编辑:这是我的路由外观
<BrowserRouter>
<Switch>
<Route exact path="/login" render={(props) => <LoginPage {...props}/>} />
<PrivateRoute authed={this.state.isAuthenticated} exact path="/" render={(props) => <RegisterPage />} />
</Switch>
</BrowserRouter>
这些是我的私人路线
import { Route } from 'react-router-dom';
从“React”导入React;从“react-router”导入{Redirect};
export default ({ component: Component, render: renderFn, authed, ...rest }) =>{
//The privateroute is fed with the auth state of app.js and evaluates the render based on that . If flase always renders "/"
if (Component){
return (
<Route
{...rest}
render={props =>
authed === true ? (
<Component {...props} />
) : (
<Redirect to={{ pathname: '/login', state: { from: props.location } }} />
)
}
/>
)
} else {
return ( //Second case is for iframe based renders
<Route {...rest} render={props => authed === true ? renderFn(props) : <Redirect to={{ pathname: '/login', state: { from: props.location } }} /> } />
);
}
}
编辑2:
应用程序JS
constructor(props) {
super(props);
console.log("PROPS APPJS")
console.log(props)
//checks if user is autheticated within the system in order to manage routes
this.state = {
authenticationChecked: false,
isAuthenticated: false
}
}
componentDidMount() {
//calls the auth service to decide the auth state value
isAuthenticated().then((result) => {
if (result === true) {
this.setState({ isAuthenticated: true, authenticationChecked: true})
} else {
this.setState({ isAuthenticated: false, authenticationChecked: true})
}
});
}
login = (email, password) => {
var thiscomponent = this;
axios({
method: 'post',
url: 'http://localhost:3003/login',
data: qs.stringify({ email, password }),
headers: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
}
}).then(res => {
console.log("set cookie")
//the accestoken is set as a cookie in order to check routes
Cookies.set('accesstoken', res.data.accesstoken);
console.log("those are the props")
console.log(this.props);
this.setState({ isAuthenticated: true }, () => {
thiscomponent.props.history.push('/'); //the bug
})
}).catch(err => {
console.log(err)
})
}
.
.
.
.
.
.
.
<BrowserRouter>
<Switch>
<PrivateRoute authed={this.state.isAuthenticated} exact path="/" render={(props) => <NewLandingPage login={this.login} {...props} exact />} />
</Switch>
</BrowserRouter>
登录页
handleSubmit(event) {
const { email, password } = this.state;
this.props.login(email, password)
event.preventDefault();
}
编辑:登录页面道具
{"history":{"length":15,"action":"POP","location":{"pathname":"/login","search":"?email=test4%40test.com&password=test","hash":""}},"location":{"pathname":"/login","search":"?email=test4%40test.com&password=test","hash":""},"match":{"path":"/login","url":"/login","isExact":true,"params":{}}}
您在何时何地设置IsAuthenticated?-lehm.ro 7分钟前
@lehm.ro在componentDidMount()期间,在我的app.js中,默认值isAuthenticated是false-mouchin777 6分钟前,然后重定向后,必须使用this.setstate使其为true,否则您的专用路由不会显示。但为此,您需要将true状态传递给app.js,然后触发一个函数来为isAuthenticated true设置state,然后重定向到该路由
设置您的历史记录在app.js中工作而不被道具传递:uncattle typeerror:无法读取未定义的属性'push'(React-Router-Dom)
为了在app
组件中使用history
,请将其与withorter
一起使用。只有当组件没有接收到路由器道具
时,才需要使用withorter
,
当您的组件是路由器呈现的组件的嵌套子组件,或者您没有将路由器道具传递给它,或者当组件根本没有链接到路由器,并且呈现为与路由分离的组件时,可能会发生这种情况。
import React from 'react';
import { Route , withRouter} from 'react-router-dom';
import Dashboard from './Dashboard';
import Bldgs from './Bldgs';
var selectedTab;
class App extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
selectedTab = 0;
}
handleClick(value) {
selectedTab = value;
// console.log(selectedTab);
this.props.history.push('/Bldgs');
// console.log(this.props);
}
render() {
var _this = this;
return (
<div>
<Route exact path="/" render={(props) => <Dashboard {...props} handleClick={_this.handleClick} />} />
<Route path="/Bldgs" component={Bldgs} curTab={selectedTab} />
</div>
);
}
}
export default withRouter(App);
WiThouter上的[文档][1]
[1]:https://reacttraining.com/react-router/web/api/withorter
app.js中的更改
<Route exact path="/login" render={(props) => <LoginPage login={this.login} {...props} />} />
并添加
login = (email, password) => {
var thiscomponent = this;
axios({
method: 'post',
url: 'http://localhost:3003/login',
data: qs.stringify({ email, password }),
headers: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
}
}).then(res => {
console.log("set cookie")
//the accestoken is set as a cookie in order to check routes
Cookies.set('accesstoken', res.data.accesstoken);
console.log("those are the props")
console.log(this.props);
this.setState({ isAuthenticated: true }, () => {
thiscomponent.props.history.push('/'); //the bug
})
}).catch(err => {
console.log(err)
})
}
并且在LoginPage中
handleSubmit(event) {
const { email, password } = this.state;
this.props.login(email, password)
event.preventDefault();
}
我使用的是Spring Security 4.1.1,但我遇到了一个问题:我试图访问URL,应用程序会重定向到登录页面。到现在为止,一直都还不错。 但是,成功登录后,应用程序会再次将我重定向到登录页面,并且不会创建任何会话,因此即使尝试直接访问URL(在URL栏中键入),应用程序也会重定向到登录页面。 有一些URL我必须要求登录才能访问它们。其他的,我可以访问无需身份验证。这些我不需要验证的URL
每个人登录后,我一直无法使用Laravel重定向。连接工作,在我登录后,它重定向到一个空白页面,但如果我更改url路径,我可以访问不同的网页。我们将不胜感激!我正在使用LDAP进行连接,它正在工作。 请让我知道,如果有任何其他代码,我应该提供。 非常感谢。 (重定向IfAuthenticated.php) 命名空间应用程序\ Http\中间件; 使用闭包;使用Illumb\Support\Faca
记录器文件中的日志- org.springframework.Security.Access.event.loggerlistener-安全授权失败,原因是:org.springframework.Security.Access.accessdeniedexception:访问被拒绝;通过身份验证的主体:org.springframework.security.authentication.ano
Reducer.js 我想在成功登录后重定向/主页。我如何重定向?
我使用设计与Rails为我的用户登录。这一切都很好,但是,我无法得到一个特定的重定向工作。 我是rails新手,所以我做事的方法可能不正确。 本质上,通过一个例子,我允许用户喜欢一个帖子。然而,他们需要登录才能这样做。我在我的控制器中创建了一个名为“喜欢”的动作,控制器具有设计过滤器 此时将显示登录页面。一旦用户登录,我想将他们重定向回他们喜欢的帖子,并调用“like”操作。 我的控制器看起来像
问题内容: 我正在做一个简单的论坛,由一系列论坛组成,每个论坛分别代表首页,主题,postitit,登录名和用户列表页面。在某些页面上,当用户未登录时会出现一个链接。 我想要实现的是在登录后触发重定向(在RequestDispatcher上使用forward()),以便浏览器返回到用户在单击登录链接之前所在的页面。为此,我看到了两种解决方案。 第一种解决方案是使用带有登录按钮的HTML 和一个不可