From 65a3ab982cb23c805cc0e40345683c3a3c3d1bd5 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 15 Sep 2021 13:34:22 -0500 Subject: [PATCH] Preload: import auth credentials from Mastodon's initial-state --- app/soapbox/reducers/__tests__/auth-test.js | 34 +++++++++++++++++++++ app/soapbox/reducers/auth.js | 29 ++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/app/soapbox/reducers/__tests__/auth-test.js b/app/soapbox/reducers/__tests__/auth-test.js index 74dc04283..f138ebe90 100644 --- a/app/soapbox/reducers/__tests__/auth-test.js +++ b/app/soapbox/reducers/__tests__/auth-test.js @@ -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); + }); + }); }); diff --git a/app/soapbox/reducers/auth.js b/app/soapbox/reducers/auth.js index 4822cf6ed..5e9b64104 100644 --- a/app/soapbox/reducers/auth.js +++ b/app/soapbox/reducers/auth.js @@ -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; }