Auto log user after register

This commit is contained in:
Grégoire Delattre 2017-08-13 10:10:24 +02:00
parent 3f2fd35195
commit ebaf17e6e4
4 changed files with 28 additions and 3 deletions

View File

@ -34,7 +34,9 @@ export function updateUser(config) {
export function userSignUp(config) { export function userSignUp(config) {
return request( return request(
"USER_SIGNUP", "USER_SIGNUP",
configureAxios().post("/users/signup", config) configureAxios().post("/users/signup", config), [
() => loginUser(config.username, config.password),
],
) )
} }

View File

@ -4,6 +4,12 @@ import { bindActionCreators } from "redux"
import { userSignUp } from "../../actions/users" import { userSignUp } from "../../actions/users"
function mapStateToProps(state) {
return {
isLogged: state.userStore.get("isLogged"),
};
}
const mapDispatchToProps = (dispatch) => const mapDispatchToProps = (dispatch) =>
bindActionCreators({ userSignUp }, dispatch) bindActionCreators({ userSignUp }, dispatch)
@ -20,6 +26,13 @@ class UserSignUp extends React.PureComponent {
"password_confirm": this.refs.passwordConfirm.value, "password_confirm": this.refs.passwordConfirm.value,
}); });
} }
componentWillReceiveProps(nextProps) {
if (!nextProps.isLogged) {
return
}
// Redirect home
nextProps.router.push("/");
}
render() { render() {
return ( return (
<div className="container"> <div className="container">
@ -53,4 +66,4 @@ class UserSignUp extends React.PureComponent {
); );
} }
} }
export default connect(null, mapDispatchToProps)(UserSignUp); export default connect(mapStateToProps, mapDispatchToProps)(UserSignUp);

View File

@ -50,6 +50,9 @@ export function request(eventPrefix, promise, callbackEvents = null, mainPayload
}) })
if (callbackEvents) { if (callbackEvents) {
for (let event of callbackEvents) { for (let event of callbackEvents) {
if (typeof event === 'function') {
event = event();
}
dispatch(event); dispatch(event);
} }
} }

View File

@ -88,7 +88,14 @@ export default function getRoutes(App) {
childRoutes: [ childRoutes: [
{ {
path: "/users/signup", path: "/users/signup",
component: UserSignUp component: UserSignUp,
onEnter: function(nextState, replace, next) {
if (isLoggedIn()) {
// User is already logged in, redirect him to the default route
replace(defaultRoute);
}
next();
},
}, },
{ {
path: "/users/login", path: "/users/login",