kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
Preload: import auth credentials from Mastodon's initial-state
rodzic
ce8787c6ba
commit
65a3ab982c
|
@ -9,6 +9,7 @@ import {
|
||||||
SWITCH_ACCOUNT,
|
SWITCH_ACCOUNT,
|
||||||
} from 'soapbox/actions/auth';
|
} from 'soapbox/actions/auth';
|
||||||
import { ME_FETCH_SKIP } from 'soapbox/actions/me';
|
import { ME_FETCH_SKIP } from 'soapbox/actions/me';
|
||||||
|
import { MASTODON_PRELOAD_IMPORT } from 'soapbox/actions/preload';
|
||||||
|
|
||||||
describe('auth reducer', () => {
|
describe('auth reducer', () => {
|
||||||
it('should return the initial state', () => {
|
it('should return the initial state', () => {
|
||||||
|
@ -311,4 +312,37 @@ describe('auth reducer', () => {
|
||||||
expect(result.get('me')).toEqual(null);
|
expect(result.get('me')).toEqual(null);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('MASTODON_PRELOAD_IMPORT', () => {
|
||||||
|
it('imports the user and token', () => {
|
||||||
|
const action = {
|
||||||
|
type: MASTODON_PRELOAD_IMPORT,
|
||||||
|
data: require('soapbox/__fixtures__/mastodon_initial_state.json'),
|
||||||
|
};
|
||||||
|
|
||||||
|
const expected = fromJS({
|
||||||
|
me: 'https://mastodon.social/@benis911',
|
||||||
|
app: {},
|
||||||
|
users: {
|
||||||
|
'https://mastodon.social/@benis911': {
|
||||||
|
id: '106801667066418367',
|
||||||
|
access_token: 'Nh15V9JWyY5Fshf2OJ_feNvOIkTV7YGVfEJFr0Y0D6Q',
|
||||||
|
url: 'https://mastodon.social/@benis911',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
tokens: {
|
||||||
|
'Nh15V9JWyY5Fshf2OJ_feNvOIkTV7YGVfEJFr0Y0D6Q': {
|
||||||
|
access_token: 'Nh15V9JWyY5Fshf2OJ_feNvOIkTV7YGVfEJFr0Y0D6Q',
|
||||||
|
account: '106801667066418367',
|
||||||
|
me: 'https://mastodon.social/@benis911',
|
||||||
|
scope: 'read write follow push',
|
||||||
|
token_type: 'Bearer',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = reducer(undefined, action);
|
||||||
|
expect(result).toEqual(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -8,6 +8,7 @@ import {
|
||||||
VERIFY_CREDENTIALS_FAIL,
|
VERIFY_CREDENTIALS_FAIL,
|
||||||
} from '../actions/auth';
|
} from '../actions/auth';
|
||||||
import { ME_FETCH_SKIP } from '../actions/me';
|
import { ME_FETCH_SKIP } from '../actions/me';
|
||||||
|
import { MASTODON_PRELOAD_IMPORT } from 'soapbox/actions/preload';
|
||||||
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable';
|
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable';
|
||||||
import { validId, isURL } from 'soapbox/utils/auth';
|
import { validId, isURL } from 'soapbox/utils/auth';
|
||||||
import { trim } from 'lodash';
|
import { trim } from 'lodash';
|
||||||
|
@ -227,6 +228,32 @@ const deleteUser = (state, account) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const importMastodonPreload = (state, data) => {
|
||||||
|
return state.withMutations(state => {
|
||||||
|
const accountId = data.getIn(['meta', 'me']);
|
||||||
|
const accountUrl = data.getIn(['accounts', accountId, 'url']);
|
||||||
|
const accessToken = data.getIn(['meta', 'access_token']);
|
||||||
|
|
||||||
|
if (validId(accessToken) && validId(accountId) && isURL(accountUrl)) {
|
||||||
|
state.setIn(['tokens', accessToken], fromJS({
|
||||||
|
access_token: accessToken,
|
||||||
|
account: accountId,
|
||||||
|
me: accountUrl,
|
||||||
|
scope: 'read write follow push',
|
||||||
|
token_type: 'Bearer',
|
||||||
|
}));
|
||||||
|
|
||||||
|
state.setIn(['users', accountUrl], fromJS({
|
||||||
|
id: accountId,
|
||||||
|
access_token: accessToken,
|
||||||
|
url: accountUrl,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
maybeShiftMe(state);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const reducer = (state, action) => {
|
const reducer = (state, action) => {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case AUTH_APP_CREATED:
|
case AUTH_APP_CREATED:
|
||||||
|
@ -245,6 +272,8 @@ const reducer = (state, action) => {
|
||||||
return state.set('me', action.account.get('url'));
|
return state.set('me', action.account.get('url'));
|
||||||
case ME_FETCH_SKIP:
|
case ME_FETCH_SKIP:
|
||||||
return state.set('me', null);
|
return state.set('me', null);
|
||||||
|
case MASTODON_PRELOAD_IMPORT:
|
||||||
|
return importMastodonPreload(state, fromJS(action.data));
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue