Add new account from login page

This commit is contained in:
Alex Gleason 2021-03-25 14:42:09 -05:00
parent 663d375dc5
commit 7a5fb6abb5
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 15 additions and 14 deletions

View File

@ -191,7 +191,7 @@ export function logOut() {
}; };
} }
export function switchAccount(accountId) { export function switchAccount(accountId,) {
return { type: SWITCH_ACCOUNT, accountId }; return { type: SWITCH_ACCOUNT, accountId };
} }

View File

@ -1,10 +1,9 @@
import React from 'react'; import React from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { Redirect } from 'react-router-dom';
import ImmutablePureComponent from 'react-immutable-pure-component'; import ImmutablePureComponent from 'react-immutable-pure-component';
import LoginForm from './login_form'; import LoginForm from './login_form';
import OtpAuthForm from './otp_auth_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 => ({ const mapStateToProps = state => ({
me: state.get('me'), me: state.get('me'),
@ -32,10 +31,14 @@ class LoginPage extends ImmutablePureComponent {
} }
handleSubmit = (event) => { handleSubmit = (event) => {
const { dispatch } = this.props; const { dispatch, me } = this.props;
const { username, password } = this.getFormData(event.target); const { username, password } = this.getFormData(event.target);
dispatch(logIn(username, password)).then(({ access_token }) => { dispatch(logIn(username, password)).then(({ access_token }) => {
return dispatch(verifyCredentials(access_token)); return dispatch(verifyCredentials(access_token));
}).then(account => {
if (typeof me === 'string') {
dispatch(switchAccount(account.id));
}
}).catch(error => { }).catch(error => {
if (error.response.data.error === 'mfa_required') { if (error.response.data.error === 'mfa_required') {
this.setState({ mfa_auth_needed: true, mfa_token: error.response.data.mfa_token }); this.setState({ mfa_auth_needed: true, mfa_token: error.response.data.mfa_token });
@ -47,9 +50,7 @@ class LoginPage extends ImmutablePureComponent {
} }
render() { render() {
const { me } = this.props;
const { isLoading, mfa_auth_needed, mfa_token } = this.state; const { isLoading, mfa_auth_needed, mfa_token } = this.state;
if (me) return <Redirect to='/' />;
if (mfa_auth_needed) return <OtpAuthForm mfa_token={mfa_token} />; if (mfa_auth_needed) return <OtpAuthForm mfa_token={mfa_token} />;

View File

@ -117,15 +117,15 @@ const reducer = (state, action) => {
}; };
const maybeReload = (oldState, state, action) => { const maybeReload = (oldState, state, action) => {
const conds = [ if (action.type === SWITCH_ACCOUNT) {
action.type === SWITCH_ACCOUNT, if (location.pathname === '/auth/sign_in') {
action.type === VERIFY_CREDENTIALS_FAIL && state.get('me') !== oldState.get('me'), location.replace('/');
]; } else {
location.reload();
}
}
// Reload if any of these conditions are true if (action.type === VERIFY_CREDENTIALS_FAIL && state.get('me') !== oldState.get('me')) {
const shouldReload = conds.some(cond => cond);
if (shouldReload) {
location.reload(); location.reload();
} }
}; };