Add new account from login page
This commit is contained in:
parent
663d375dc5
commit
7a5fb6abb5
|
@ -191,7 +191,7 @@ export function logOut() {
|
|||
};
|
||||
}
|
||||
|
||||
export function switchAccount(accountId) {
|
||||
export function switchAccount(accountId,) {
|
||||
return { type: SWITCH_ACCOUNT, accountId };
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
import React from 'react';
|
||||
import { connect } from 'react-redux';
|
||||
import { Redirect } from 'react-router-dom';
|
||||
import ImmutablePureComponent from 'react-immutable-pure-component';
|
||||
import LoginForm from './login_form';
|
||||
import OtpAuthForm from './otp_auth_form';
|
||||
import { logIn, verifyCredentials } from 'soapbox/actions/auth';
|
||||
import { logIn, verifyCredentials, switchAccount } from 'soapbox/actions/auth';
|
||||
|
||||
const mapStateToProps = state => ({
|
||||
me: state.get('me'),
|
||||
|
@ -32,10 +31,14 @@ class LoginPage extends ImmutablePureComponent {
|
|||
}
|
||||
|
||||
handleSubmit = (event) => {
|
||||
const { dispatch } = this.props;
|
||||
const { dispatch, me } = this.props;
|
||||
const { username, password } = this.getFormData(event.target);
|
||||
dispatch(logIn(username, password)).then(({ access_token }) => {
|
||||
return dispatch(verifyCredentials(access_token));
|
||||
}).then(account => {
|
||||
if (typeof me === 'string') {
|
||||
dispatch(switchAccount(account.id));
|
||||
}
|
||||
}).catch(error => {
|
||||
if (error.response.data.error === 'mfa_required') {
|
||||
this.setState({ mfa_auth_needed: true, mfa_token: error.response.data.mfa_token });
|
||||
|
@ -47,9 +50,7 @@ class LoginPage extends ImmutablePureComponent {
|
|||
}
|
||||
|
||||
render() {
|
||||
const { me } = this.props;
|
||||
const { isLoading, mfa_auth_needed, mfa_token } = this.state;
|
||||
if (me) return <Redirect to='/' />;
|
||||
|
||||
if (mfa_auth_needed) return <OtpAuthForm mfa_token={mfa_token} />;
|
||||
|
||||
|
|
|
@ -117,15 +117,15 @@ const reducer = (state, action) => {
|
|||
};
|
||||
|
||||
const maybeReload = (oldState, state, action) => {
|
||||
const conds = [
|
||||
action.type === SWITCH_ACCOUNT,
|
||||
action.type === VERIFY_CREDENTIALS_FAIL && state.get('me') !== oldState.get('me'),
|
||||
];
|
||||
if (action.type === SWITCH_ACCOUNT) {
|
||||
if (location.pathname === '/auth/sign_in') {
|
||||
location.replace('/');
|
||||
} else {
|
||||
location.reload();
|
||||
}
|
||||
}
|
||||
|
||||
// Reload if any of these conditions are true
|
||||
const shouldReload = conds.some(cond => cond);
|
||||
|
||||
if (shouldReload) {
|
||||
if (action.type === VERIFY_CREDENTIALS_FAIL && state.get('me') !== oldState.get('me')) {
|
||||
location.reload();
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue