kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
Make login work again
rodzic
0daa95646e
commit
a731ac88cf
|
@ -94,8 +94,9 @@ function createUserToken(username, password) {
|
||||||
grant_type: 'password',
|
grant_type: 'password',
|
||||||
username: username,
|
username: username,
|
||||||
password: password,
|
password: password,
|
||||||
}).then(response => {
|
}).then(({ data: token }) => {
|
||||||
dispatch(authLoggedIn(response.data));
|
dispatch(authLoggedIn(token));
|
||||||
|
return token;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -143,7 +144,7 @@ export function verifyCredentials(token) {
|
||||||
method: 'get',
|
method: 'get',
|
||||||
url: '/api/v1/accounts/verify_credentials',
|
url: '/api/v1/accounts/verify_credentials',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Bearer ${token.get('access_token')}`,
|
'Authorization': `Bearer ${token}`,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -198,10 +199,10 @@ export function switchAccount(accountId) {
|
||||||
export function fetchOwnAccounts() {
|
export function fetchOwnAccounts() {
|
||||||
return throttle((dispatch, getState) => {
|
return throttle((dispatch, getState) => {
|
||||||
const state = getState();
|
const state = getState();
|
||||||
state.getIn(['auth', 'users']).forEach((token, id) => {
|
state.getIn(['auth', 'users']).forEach(user => {
|
||||||
const account = state.getIn(['accounts', id]);
|
const account = state.getIn(['accounts', user.get('id')]);
|
||||||
if (!account) {
|
if (!account) {
|
||||||
dispatch(verifyCredentials(token));
|
dispatch(verifyCredentials(user.get('access_token')));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, 2000);
|
}, 2000);
|
||||||
|
|
|
@ -18,7 +18,7 @@ export function fetchMe() {
|
||||||
const state = getState();
|
const state = getState();
|
||||||
|
|
||||||
const me = state.getIn(['auth', 'me']);
|
const me = state.getIn(['auth', 'me']);
|
||||||
const token = state.getIn(['auth', 'users', me]);
|
const token = state.getIn(['auth', 'users', me, 'access_token']);
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
dispatch({ type: ME_FETCH_SKIP }); return noOp();
|
dispatch({ type: ME_FETCH_SKIP }); return noOp();
|
||||||
|
|
|
@ -4,8 +4,7 @@ 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 } from 'soapbox/actions/auth';
|
import { logIn, verifyCredentials } from 'soapbox/actions/auth';
|
||||||
import { fetchMe } from 'soapbox/actions/me';
|
|
||||||
|
|
||||||
const mapStateToProps = state => ({
|
const mapStateToProps = state => ({
|
||||||
me: state.get('me'),
|
me: state.get('me'),
|
||||||
|
@ -35,8 +34,8 @@ class LoginPage extends ImmutablePureComponent {
|
||||||
handleSubmit = (event) => {
|
handleSubmit = (event) => {
|
||||||
const { dispatch } = this.props;
|
const { dispatch } = this.props;
|
||||||
const { username, password } = this.getFormData(event.target);
|
const { username, password } = this.getFormData(event.target);
|
||||||
dispatch(logIn(username, password)).then(() => {
|
dispatch(logIn(username, password)).then(({ access_token }) => {
|
||||||
return dispatch(fetchMe());
|
return dispatch(verifyCredentials(access_token));
|
||||||
}).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 });
|
||||||
|
|
|
@ -8,8 +8,7 @@ import SiteLogo from './site_logo';
|
||||||
import SoapboxPropTypes from 'soapbox/utils/soapbox_prop_types';
|
import SoapboxPropTypes from 'soapbox/utils/soapbox_prop_types';
|
||||||
import { defineMessages, injectIntl } from 'react-intl';
|
import { defineMessages, injectIntl } from 'react-intl';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { logIn } from 'soapbox/actions/auth';
|
import { logIn, verifyCredentials } from 'soapbox/actions/auth';
|
||||||
import { fetchMe } from 'soapbox/actions/me';
|
|
||||||
import OtpAuthForm from 'soapbox/features/auth_login/components/otp_auth_form';
|
import OtpAuthForm from 'soapbox/features/auth_login/components/otp_auth_form';
|
||||||
import IconButton from 'soapbox/components/icon_button';
|
import IconButton from 'soapbox/components/icon_button';
|
||||||
|
|
||||||
|
@ -55,8 +54,8 @@ class Header extends ImmutablePureComponent {
|
||||||
handleSubmit = (event) => {
|
handleSubmit = (event) => {
|
||||||
const { dispatch } = this.props;
|
const { dispatch } = this.props;
|
||||||
const { username, password } = this.getFormData(event.target);
|
const { username, password } = this.getFormData(event.target);
|
||||||
dispatch(logIn(username, password)).then(() => {
|
dispatch(logIn(username, password)).then(({ access_token }) => {
|
||||||
return dispatch(fetchMe());
|
return dispatch(verifyCredentials(access_token));
|
||||||
}).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 });
|
||||||
|
|
|
@ -4,19 +4,34 @@ import {
|
||||||
AUTH_APP_AUTHORIZED,
|
AUTH_APP_AUTHORIZED,
|
||||||
AUTH_LOGGED_OUT,
|
AUTH_LOGGED_OUT,
|
||||||
SWITCH_ACCOUNT,
|
SWITCH_ACCOUNT,
|
||||||
|
VERIFY_CREDENTIALS_SUCCESS,
|
||||||
} from '../actions/auth';
|
} from '../actions/auth';
|
||||||
import { Map as ImmutableMap, fromJS } from 'immutable';
|
import { Map as ImmutableMap, fromJS } from 'immutable';
|
||||||
|
|
||||||
const defaultState = ImmutableMap({
|
const defaultState = ImmutableMap({
|
||||||
app: ImmutableMap(),
|
app: ImmutableMap(),
|
||||||
user: ImmutableMap(),
|
tokens: ImmutableMap(),
|
||||||
users: ImmutableMap(),
|
|
||||||
me: null,
|
me: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
const localState = fromJS(JSON.parse(localStorage.getItem('soapbox:auth')));
|
const localState = fromJS(JSON.parse(localStorage.getItem('soapbox:auth')));
|
||||||
const initialState = defaultState.merge(localState);
|
const initialState = defaultState.merge(localState);
|
||||||
|
|
||||||
|
const importToken = (state, token) => {
|
||||||
|
return state.setIn(['tokens', token.access_token], fromJS(token));
|
||||||
|
};
|
||||||
|
|
||||||
|
const importCredentials = (state, token, account) => {
|
||||||
|
return state.withMutations(state => {
|
||||||
|
state.setIn(['users', account.id], ImmutableMap({
|
||||||
|
id: account.id,
|
||||||
|
access_token: token,
|
||||||
|
}));
|
||||||
|
state.setIn(['tokens', token, 'account'], account.id);
|
||||||
|
state.update('me', null, me => me || account.id);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const reducer = (state, action) => {
|
const reducer = (state, action) => {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case AUTH_APP_CREATED:
|
case AUTH_APP_CREATED:
|
||||||
|
@ -24,9 +39,11 @@ const reducer = (state, action) => {
|
||||||
case AUTH_APP_AUTHORIZED:
|
case AUTH_APP_AUTHORIZED:
|
||||||
return state.update('app', ImmutableMap(), app => app.merge(fromJS(action.app)));
|
return state.update('app', ImmutableMap(), app => app.merge(fromJS(action.app)));
|
||||||
case AUTH_LOGGED_IN:
|
case AUTH_LOGGED_IN:
|
||||||
return state.set('user', fromJS(action.token));
|
return importToken(state, action.token);
|
||||||
case AUTH_LOGGED_OUT:
|
case AUTH_LOGGED_OUT:
|
||||||
return state.set('user', ImmutableMap());
|
return state.set('user', ImmutableMap());
|
||||||
|
case VERIFY_CREDENTIALS_SUCCESS:
|
||||||
|
return importCredentials(state, action.token, action.account);
|
||||||
case SWITCH_ACCOUNT:
|
case SWITCH_ACCOUNT:
|
||||||
return state.set('me', action.accountId);
|
return state.set('me', action.accountId);
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -4,7 +4,7 @@ import {
|
||||||
ME_FETCH_SKIP,
|
ME_FETCH_SKIP,
|
||||||
ME_PATCH_SUCCESS,
|
ME_PATCH_SUCCESS,
|
||||||
} from '../actions/me';
|
} from '../actions/me';
|
||||||
import { AUTH_LOGGED_OUT } from '../actions/auth';
|
import { AUTH_LOGGED_OUT, VERIFY_CREDENTIALS_SUCCESS } from '../actions/auth';
|
||||||
|
|
||||||
const initialState = null;
|
const initialState = null;
|
||||||
|
|
||||||
|
@ -13,6 +13,8 @@ export default function me(state = initialState, action) {
|
||||||
case ME_FETCH_SUCCESS:
|
case ME_FETCH_SUCCESS:
|
||||||
case ME_PATCH_SUCCESS:
|
case ME_PATCH_SUCCESS:
|
||||||
return action.me.id;
|
return action.me.id;
|
||||||
|
case VERIFY_CREDENTIALS_SUCCESS:
|
||||||
|
return state || action.account.id;
|
||||||
case ME_FETCH_FAIL:
|
case ME_FETCH_FAIL:
|
||||||
case ME_FETCH_SKIP:
|
case ME_FETCH_SKIP:
|
||||||
case AUTH_LOGGED_OUT:
|
case AUTH_LOGGED_OUT:
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { NOTIFICATIONS_FILTER_SET } from '../actions/notifications';
|
||||||
import { EMOJI_USE } from '../actions/emojis';
|
import { EMOJI_USE } from '../actions/emojis';
|
||||||
import { LIST_DELETE_SUCCESS, LIST_FETCH_FAIL } from '../actions/lists';
|
import { LIST_DELETE_SUCCESS, LIST_FETCH_FAIL } from '../actions/lists';
|
||||||
import { ME_FETCH_SUCCESS } from 'soapbox/actions/me';
|
import { ME_FETCH_SUCCESS } from 'soapbox/actions/me';
|
||||||
|
import { VERIFY_CREDENTIALS_SUCCESS } from 'soapbox/actions/auth';
|
||||||
import { Map as ImmutableMap, fromJS } from 'immutable';
|
import { Map as ImmutableMap, fromJS } from 'immutable';
|
||||||
|
|
||||||
// Default settings are in action/settings.js
|
// Default settings are in action/settings.js
|
||||||
|
@ -17,12 +18,18 @@ const updateFrequentEmojis = (state, emoji) => state.update('frequentlyUsedEmoji
|
||||||
|
|
||||||
const filterDeadListColumns = (state, listId) => state.update('columns', columns => columns.filterNot(column => column.get('id') === 'LIST' && column.get('params').get('id') === listId));
|
const filterDeadListColumns = (state, listId) => state.update('columns', columns => columns.filterNot(column => column.get('id') === 'LIST' && column.get('params').get('id') === listId));
|
||||||
|
|
||||||
|
const importSettings = (state, account) => {
|
||||||
|
account = fromJS(account);
|
||||||
|
const prefs = account.getIn(['pleroma', 'settings_store', FE_NAME], ImmutableMap());
|
||||||
|
return state.merge(prefs);
|
||||||
|
};
|
||||||
|
|
||||||
export default function settings(state = initialState, action) {
|
export default function settings(state = initialState, action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case ME_FETCH_SUCCESS:
|
case ME_FETCH_SUCCESS:
|
||||||
const me = fromJS(action.me);
|
return importSettings(state, action.me);
|
||||||
let fePrefs = me.getIn(['pleroma', 'settings_store', FE_NAME], ImmutableMap());
|
case VERIFY_CREDENTIALS_SUCCESS:
|
||||||
return state.merge(fePrefs);
|
return importSettings(state, action.account);
|
||||||
case NOTIFICATIONS_FILTER_SET:
|
case NOTIFICATIONS_FILTER_SET:
|
||||||
case SETTING_CHANGE:
|
case SETTING_CHANGE:
|
||||||
return state
|
return state
|
||||||
|
|
Ładowanie…
Reference in New Issue