当前位置: 首页 > 知识库问答 >
问题:

React/Redux:登录后重定向

阳建弼
2023-03-14
import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import { FormattedMessage } from 'react-intl';
import { createStructuredSelector } from 'reselect';
import {loginAction} from './actions';


export class Login extends React.PureComponent { // eslint-disable-line react/prefer-stateless-function

constructor(props) {
super(props);
this.state = {
  username: '',
  password: ''
};

// this.handleChange = this.handleChangeUsername.bind(this);
// this.handleSubmit = this.handleChangePassword.bind(this);
}

handleChangeUsername(event) {
 console.log(event.target.value);
 this.setState({username: event.target.value});
 console.log(this.state.username)
}
handleChangePassword(event) {
 this.setState({password: event.target.value});
 console.log(this.state.password)
}

 handleSubmit(event) {

this.props.dispatch(loginAction(this.state));
event.preventDefault();
}
render() {
return (
  <div>

  <div className="loginColumns animated fadeInDown">
    <div className="row">

        <div className="col-md-6">

        </div>
        <div className="col-md-6">
            <div className="ibox-content">
                <form className="m-t" role="form" onSubmit={this.handleSubmit.bind(this)}>
                    <div className="form-group">
                        <input type="email" value={this.state.username} onChange={this.handleChangeUsername.bind(this)} className="form-control" placeholder="Username" required="" />
                    </div>
                    <div className="form-group">
                        <input type="password" value={this.state.password} onChange={this.handleChangePassword.bind(this)} className="form-control" placeholder="Password" required="" />
                    </div>
                    <button type="submit" className="btn btn-primary block full-width m-b">Login</button>

                    <a href="#">
                        <small>Forgot password?</small>
                    </a>
                </form>
            </div>
        </div>
    </div>
    <hr/>
    <div className="row">
        <div className="col-md-6">
            Copyright Example Company
        </div>
        <div className="col-md-6 text-right">
           <small>© 2014-2015</small>
        </div>
    </div>
</div>

  </div>
);
}
}

Login.propTypes = {
 dispatch: PropTypes.func.isRequired,
};

const mapStateToProps = createStructuredSelector({
 // Login: makeSelectLogin(),
});

function mapDispatchToProps(dispatch) {
 return {
 dispatch,
 };
}

 export default connect(mapStateToProps, mapDispatchToProps)(Login);
import {
  DEFAULT_ACTION,
  LOGIN_ACTION
} from './constants';

export function defaultAction() {
 return {
   type: DEFAULT_ACTION,
  };
}

export function loginAction(json) {
  return {
    type: LOGIN_ACTION,
    json
  };
}

Reducer.js

import { fromJS } from 'immutable';
import {
  DEFAULT_ACTION,
  LOGIN_ACTION
} from './constants';

const initialState = fromJS({
    status: false
});

function loginReducer(state = initialState, action) {
    console.log("action", action)
  switch (action.type) {
    case DEFAULT_ACTION:
      return state;

    case LOGIN_ACTION: 
        return _testFunction(state,action.json);
        default:
      return state;
  }
}

function _testFunction(state, json) {
    if(json.username == "abcd@gmail.com" && json.password == "1234")
    return state.set("status", true)
}

export default loginReducer;

我想在成功登录后重定向/主页。我如何重定向?

共有1个答案

叶书
2023-03-14

好吧,你要做的就是这样的事情。将回调传递给您的操作。一旦操作完成,只需调用该回调,它将以编程方式将您重定向到所需的url。提交登录表单后,将输入值与回调函数一起传递给您的操作,如下所示。

  onSubmit(values) {
    this.props.loginAction(values, () => {
      this.props.history.push('/');
    });
  }

然后,在您的操作中,如果您正在调用后端API,在解析promise时,请确保调用像这样传递给操作的回调函数。

  export function loginAction(values, callback) {
  const request = axios.post(`/endpoint`, values)
    .then(() => callback());

  return {
    type: LOGIN,
    payload: request
  };
}

这就是你要做的一切。取决于您的方案和设置,您可以对此进行轻微的修改。我留着给你做练习。希望这能有所帮助。快乐编码!

 类似资料:
  • 问题内容: 我想在我的 react / react-router / flux 应用程序中建立一个Facebook登录名。我在登录事件上注册了一个侦听器,并且希望将用户重定向到(如果他们已登录)。该怎么办?效果不佳,除非完全重新加载了页面。 问题答案: React Router v0.13 该实例从返回可以通过左右(或者,如果内部的阵营组件,你可以从它的上下文对象),以及包含的方法一样,你可以用它

  • 记录器文件中的日志- org.springframework.Security.Access.event.loggerlistener-安全授权失败,原因是:org.springframework.Security.Access.accessdeniedexception:访问被拒绝;通过身份验证的主体:org.springframework.security.authentication.ano

  • 当我尝试在不使用任何异步函数的情况下执行身份验证时(例如,将用户名和密码与react中的硬编码值进行比较),一切工作都很顺利。 但是当我使用express和mongo执行身份验证时,登录时的重定向停止工作。如果我再次登录,那么重定向就会发生。受保护的路由仍然有效(如果用户没有登录,则重定向到登录页)。 下面是我在express+mongo IE中使用do auth的问题的一个小演示。异步还原。这并

  • 我使用设计与Rails为我的用户登录。这一切都很好,但是,我无法得到一个特定的重定向工作。 我是rails新手,所以我做事的方法可能不正确。 本质上,通过一个例子,我允许用户喜欢一个帖子。然而,他们需要登录才能这样做。我在我的控制器中创建了一个名为“喜欢”的动作,控制器具有设计过滤器 此时将显示登录页面。一旦用户登录,我想将他们重定向回他们喜欢的帖子,并调用“like”操作。 我的控制器看起来像

  • 问题内容: 我正在做一个简单的论坛,由一系列论坛组成,每个论坛分别代表首页,主题,postitit,登录名和用户列表页面。在某些页面上,当用户未登录时会出现一个链接。 我想要实现的是在登录后触发重定向(在RequestDispatcher上使用forward()),以便浏览器返回到用户在单击登录链接之前所在的页面。为此,我看到了两种解决方案。 第一种解决方案是使用带有登录按钮的HTML 和一个不可

  • 每个人登录后,我一直无法使用Laravel重定向。连接工作,在我登录后,它重定向到一个空白页面,但如果我更改url路径,我可以访问不同的网页。我们将不胜感激!我正在使用LDAP进行连接,它正在工作。 请让我知道,如果有任何其他代码,我应该提供。 非常感谢。 (重定向IfAuthenticated.php) 命名空间应用程序\ Http\中间件; 使用闭包;使用Illumb\Support\Faca