gpt4 book ai didi

reactjs - 在Reactjs中的页面中添加 session 超时

转载 作者:行者123 更新时间:2023-12-03 13:50:06 24 4
gpt4 key购买 nike

我想在登录页面添加 session 超时。在 session 超时前几秒钟,页面应显示 session 超时警报。之后, session 应该超时,用户应该被重定向到初始页面。请帮助我做到这一点。这是我的loginform.js 页面

    import React, { Component } from 'react';
import { connect } from 'react-redux';
import { login } from '../../redux/reducer';
import './LoginForm.css';
import { BrowserRouter as Router, Route } from "react-router-dom";

class LoginForm extends Component {

constructor(props) {
super(props);
this.state = {
timeOut:false,
warning:false,
};
this.onSubmit = this.onSubmit.bind(this);
}


componentDidMount(){

setTimeout(() => {
this.setState({timeOut:true});
},30000);//if 8sec is your warning time

setTimeout(() => {
this.setState({timeOut:true});
},10000);//if 10sec is your time out
}

render() {
let {username, password} = this.state;
let {isLoginPending, isLoginSuccess, loginError} = this.props;
if(this.state.timeOut){
return <Route to="/Home" push={true} />
}
else{
return (
<form name="loginForm" onSubmit={this.onSubmit}>
<div className="form-group-collection">
<div className="form-group">
<label>username:</label>
<input type="username" name="username" onChange={e => this.setState({username: e.target.value})} value={username}/>
</div>

<div className="form-group">
<label>Password:</label>
<input type="password" name="password" onChange={e => this.setState({password: e.target.value})} value={password}/>
</div>
</div>

<input type="submit" value="Login" />

<div className="message">
{ isLoginPending && <div>Please wait...</div> }
{ isLoginSuccess && <div>Success.</div> }
{ loginError && <div>{loginError.message}</div> }
</div>
</form>
)
}
}
onSubmit(e) {
e.preventDefault();
let { username, password } = this.state;
this.props.login(username, password);
this.setState({
username: '',
password: ''
});
}
}
const Home = () => (
<div>
<h2>Welcome Admin </h2>

</div>
);

const mapStateToProps = (state) => {
return {
isLoginPending: state.isLoginPending,
isLoginSuccess: state.isLoginSuccess,
loginError: state.loginError
};
}

const mapDispatchToProps = (dispatch) => {
return {
login: (username, password) => dispatch(login(username, password))
};
}

导出默认连接(mapStateToProps, mapDispatchToProps)(LoginForm);这是我的 reducer.js 文件

const SET_LOGIN_PENDING = 'SET_LOGIN_PENDING';
const SET_LOGIN_SUCCESS = 'SET_LOGIN_SUCCESS';
const SET_LOGIN_ERROR = 'SET_LOGIN_ERROR';

export function login(username, password) {
return dispatch => {
dispatch(setLoginPending(true));
dispatch(setLoginSuccess(false));
dispatch(setLoginError(null));

callLoginApi(username, password, error => {
dispatch(setLoginPending(false));
if (!error) {
dispatch(setLoginSuccess(true));
} else {
dispatch(setLoginError(error));
}
});
}
}

function setLoginPending(isLoginPending) {
return {
type: SET_LOGIN_PENDING,
isLoginPending
};
}

function setLoginSuccess(isLoginSuccess) {
return {
type: SET_LOGIN_SUCCESS,
isLoginSuccess
};
}

function setLoginError(loginError) {
return {
type: SET_LOGIN_ERROR,
loginError
}
}

function callLoginApi(username, password, callback) {
setTimeout(() => {
if (username === 'admin' && password === 'password') {
return callback(null);
} else {
return callback(new Error('Invalid username and password'));
}
}, 1000);
}

export default function reducer(state = {
isLoginSuccess: false,
isLoginPending: false,
loginError: null
}, action) {
switch (action.type) {
case SET_LOGIN_PENDING:
return Object.assign({}, state, {
isLoginPending: action.isLoginPending
});

case SET_LOGIN_SUCCESS:
return Object.assign({}, state, {
isLoginSuccess: action.isLoginSuccess
});

case SET_LOGIN_ERROR:
return Object.assign({}, state, {
loginError: action.loginError
});

default:
return state;
}
}

这是我的 store.js

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import reducer from './reducer';

const store = createStore(reducer, {}, applyMiddleware(thunk, logger));
export default store;

我需要进行哪些更改才能使 session 超时生效?

最佳答案

一个非常好的最佳实践是创建一个高阶组件,您可以为每个组件提供超时功能。以下 Stackoverflow 帖子中发布了一个非常相似的问题和解决方案:React-Timeout HOC

他们正在使用 plougsgaard 提供的 React-Timeout 包:React-Timeout

关于reactjs - 在Reactjs中的页面中添加 session 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50036145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com