Preload: import auth credentials from Mastodon's initial-state

v1.x.x
Alex Gleason 2021-09-15 13:34:22 -05:00
rodzic ce8787c6ba
commit 65a3ab982c
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
2 zmienionych plików z 63 dodań i 0 usunięć

Wyświetl plik

@ -9,6 +9,7 @@ import {
SWITCH_ACCOUNT,
} from 'soapbox/actions/auth';
import { ME_FETCH_SKIP } from 'soapbox/actions/me';
import { MASTODON_PRELOAD_IMPORT } from 'soapbox/actions/preload';
describe('auth reducer', () => {
it('should return the initial state', () => {
@ -311,4 +312,37 @@ describe('auth reducer', () => {
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);
});
});
});

Wyświetl plik

@ -8,6 +8,7 @@ import {
VERIFY_CREDENTIALS_FAIL,
} from '../actions/auth';
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 { validId, isURL } from 'soapbox/utils/auth';
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) => {
switch(action.type) {
case AUTH_APP_CREATED:
@ -245,6 +272,8 @@ const reducer = (state, action) => {
return state.set('me', action.account.get('url'));
case ME_FETCH_SKIP:
return state.set('me', null);
case MASTODON_PRELOAD_IMPORT:
return importMastodonPreload(state, fromJS(action.data));
default:
return state;
}