From d3904bea5c1e42c45beb17e7d61cf40a69bc2e3c Mon Sep 17 00:00:00 2001 From: Andrew Peebles Date: Sat, 16 Apr 2016 11:21:23 -0700 Subject: [PATCH] support for auto-login from /change. relies on a pending pull request on express-stormpath. --- docs/api.md | 7 +++++++ src/components/ChangePasswordForm.js | 21 ++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/api.md b/docs/api.md index 50fe514..a661886 100644 --- a/docs/api.md +++ b/docs/api.md @@ -404,6 +404,13 @@ Renders a change password form. The parameter `spToken` is required in order for ``` +After the user has changed their password, the default form will show a link the user can click on that takes them +to the login page. If you want to automatcially log the user in after they've changed their password, then add: + +```html + +``` + Customize the form by providing your own markup. ```html diff --git a/src/components/ChangePasswordForm.js b/src/components/ChangePasswordForm.js index f34b80d..79a9e21 100644 --- a/src/components/ChangePasswordForm.js +++ b/src/components/ChangePasswordForm.js @@ -2,6 +2,7 @@ import React from 'react'; import ReactMixin from 'react-mixin'; import { History, Link } from 'react-router'; +import context from '../context'; import utils from '../utils'; import LoginLink from '../components/LoginLink'; import LoadingText from '../components/LoadingText'; @@ -92,7 +93,7 @@ export default class ChangePasswordForm extends React.Component { }); } - UserActions.changePassword(data, (err) => { + UserActions.changePassword(data, (err, acct) => { if (err) { if (err.status === 404) { err.message = 'The reset password token is not valid. Please try resetting your password again.'; @@ -108,6 +109,13 @@ export default class ChangePasswordForm extends React.Component { isFormProcessing: false, isFormSent: true }); + + if ( this.props.autoLogin && ( acct && acct.email ) ) { + UserActions.login({ username: acct.email, password: data.password }, ( err ) => { + if ( err ) utils.logWarning('', 'Tried to autoLogin, but there was a problem: ' + err.message); + if ( ! err ) this._performRedirect(); + }); + } }); }; @@ -131,6 +139,17 @@ export default class ChangePasswordForm extends React.Component { } } + _performRedirect() { + var router = context.getRouter(); + var homeRoute = router.getHomeRoute(); + var authenticatedHomeRoute = router.getAuthenticatedHomeRoute(); + var { location } = this.props; + var passthru = (location && location.state) ? location.state.nextPathname : null; + var redirectTo = this.props.redirectTo || passthru || (authenticatedHomeRoute || {}).path || (homeRoute || {}).path || '/'; + console.log( 'redirecting to:', redirectTo ); + this.history.pushState(null, redirectTo); + } + _mapFormFieldHandler(element, tryMapField) { if (element.type === 'input' || element.type === 'textarea') { if (element.props.type !== 'submit') {