From ebaf17e6e45ffa0fddf832b68d5a171db757b01e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Sun, 13 Aug 2017 10:10:24 +0200 Subject: [PATCH] Auto log user after register --- src/public/js/actions/users.js | 4 +++- src/public/js/components/users/signup.js | 15 ++++++++++++++- src/public/js/requests.js | 3 +++ src/public/js/routes.js | 9 ++++++++- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/public/js/actions/users.js b/src/public/js/actions/users.js index 61e9552..a06de7c 100644 --- a/src/public/js/actions/users.js +++ b/src/public/js/actions/users.js @@ -34,7 +34,9 @@ export function updateUser(config) { export function userSignUp(config) { return request( "USER_SIGNUP", - configureAxios().post("/users/signup", config) + configureAxios().post("/users/signup", config), [ + () => loginUser(config.username, config.password), + ], ) } diff --git a/src/public/js/components/users/signup.js b/src/public/js/components/users/signup.js index 80ec06b..d76d960 100644 --- a/src/public/js/components/users/signup.js +++ b/src/public/js/components/users/signup.js @@ -4,6 +4,12 @@ import { bindActionCreators } from "redux" import { userSignUp } from "../../actions/users" +function mapStateToProps(state) { + return { + isLogged: state.userStore.get("isLogged"), + }; +} + const mapDispatchToProps = (dispatch) => bindActionCreators({ userSignUp }, dispatch) @@ -20,6 +26,13 @@ class UserSignUp extends React.PureComponent { "password_confirm": this.refs.passwordConfirm.value, }); } + componentWillReceiveProps(nextProps) { + if (!nextProps.isLogged) { + return + } + // Redirect home + nextProps.router.push("/"); + } render() { return (
@@ -53,4 +66,4 @@ class UserSignUp extends React.PureComponent { ); } } -export default connect(null, mapDispatchToProps)(UserSignUp); +export default connect(mapStateToProps, mapDispatchToProps)(UserSignUp); diff --git a/src/public/js/requests.js b/src/public/js/requests.js index f264068..c47221f 100644 --- a/src/public/js/requests.js +++ b/src/public/js/requests.js @@ -50,6 +50,9 @@ export function request(eventPrefix, promise, callbackEvents = null, mainPayload }) if (callbackEvents) { for (let event of callbackEvents) { + if (typeof event === 'function') { + event = event(); + } dispatch(event); } } diff --git a/src/public/js/routes.js b/src/public/js/routes.js index 163ce69..e92298e 100644 --- a/src/public/js/routes.js +++ b/src/public/js/routes.js @@ -88,7 +88,14 @@ export default function getRoutes(App) { childRoutes: [ { 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",