localforage: remember auth accounts

This commit is contained in:
Alex Gleason 2021-10-20 16:27:36 -05:00
parent c8cec8fdac
commit 0b94774fbe
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
6 changed files with 48 additions and 14 deletions

View file

@ -19,6 +19,7 @@ import { obtainOAuthToken, revokeOAuthToken } from 'soapbox/actions/oauth';
import sourceCode from 'soapbox/utils/code'; import sourceCode from 'soapbox/utils/code';
import { getFeatures } from 'soapbox/utils/features'; import { getFeatures } from 'soapbox/utils/features';
import { isStandalone } from 'soapbox/utils/state'; import { isStandalone } from 'soapbox/utils/state';
import KVStore from 'soapbox/storage/kv_store';
export const SWITCH_ACCOUNT = 'SWITCH_ACCOUNT'; export const SWITCH_ACCOUNT = 'SWITCH_ACCOUNT';
@ -31,6 +32,10 @@ export const VERIFY_CREDENTIALS_REQUEST = 'VERIFY_CREDENTIALS_REQUEST';
export const VERIFY_CREDENTIALS_SUCCESS = 'VERIFY_CREDENTIALS_SUCCESS'; export const VERIFY_CREDENTIALS_SUCCESS = 'VERIFY_CREDENTIALS_SUCCESS';
export const VERIFY_CREDENTIALS_FAIL = 'VERIFY_CREDENTIALS_FAIL'; export const VERIFY_CREDENTIALS_FAIL = 'VERIFY_CREDENTIALS_FAIL';
export const AUTH_ACCOUNT_REMEMBER_REQUEST = 'AUTH_ACCOUNT_REMEMBER_REQUEST';
export const AUTH_ACCOUNT_REMEMBER_SUCCESS = 'AUTH_ACCOUNT_REMEMBER_SUCCESS';
export const AUTH_ACCOUNT_REMEMBER_FAIL = 'AUTH_ACCOUNT_REMEMBER_FAIL';
export const messages = defineMessages({ export const messages = defineMessages({
loggedOut: { id: 'auth.logged_out', defaultMessage: 'Logged out.' }, loggedOut: { id: 'auth.logged_out', defaultMessage: 'Logged out.' },
invalidCredentials: { id: 'auth.invalid_credentials', defaultMessage: 'Wrong username or password' }, invalidCredentials: { id: 'auth.invalid_credentials', defaultMessage: 'Wrong username or password' },
@ -157,6 +162,28 @@ export function verifyCredentials(token, accountUrl) {
}; };
} }
export function rememberAuthAccount(accountUrl) {
return (dispatch, getState) => {
dispatch({ type: AUTH_ACCOUNT_REMEMBER_REQUEST, accountUrl });
return KVStore.getItemOrError(`authAccount:${accountUrl}`).then(account => {
dispatch(importFetchedAccount(account));
dispatch({ type: AUTH_ACCOUNT_REMEMBER_SUCCESS, account, accountUrl });
if (account.id === getState().get('me')) dispatch(fetchMeSuccess(account));
return account;
}).catch(error => {
dispatch({ type: AUTH_ACCOUNT_REMEMBER_FAIL, error, accountUrl, skipAlert: true });
});
};
}
export function loadCredentials(token, accountUrl) {
return (dispatch, getState) => {
return dispatch(rememberAuthAccount(accountUrl)).finally(() => {
return dispatch(verifyCredentials(token, accountUrl));
});
};
}
export function logIn(intl, username, password) { export function logIn(intl, username, password) {
return (dispatch, getState) => { return (dispatch, getState) => {
return dispatch(createAppAndToken()).then(() => { return dispatch(createAppAndToken()).then(() => {

View file

@ -69,7 +69,7 @@ export function loadInstance() {
return (dispatch, getState) => { return (dispatch, getState) => {
const host = getHost(getState()); const host = getHost(getState());
return dispatch(rememberInstance(host)).finally(instance => { return dispatch(rememberInstance(host)).finally(() => {
return dispatch(fetchInstance()); return dispatch(fetchInstance());
}); });
}; };

View file

@ -1,6 +1,6 @@
import api from '../api'; import api from '../api';
import { importFetchedAccount } from './importer'; import { importFetchedAccount } from './importer';
import { verifyCredentials } from './auth'; import { loadCredentials } from './auth';
import { getAuthUserId, getAuthUserUrl } from 'soapbox/utils/auth'; import { getAuthUserId, getAuthUserUrl } from 'soapbox/utils/auth';
export const ME_FETCH_REQUEST = 'ME_FETCH_REQUEST'; export const ME_FETCH_REQUEST = 'ME_FETCH_REQUEST';
@ -38,7 +38,7 @@ export function fetchMe() {
} }
dispatch(fetchMeRequest()); dispatch(fetchMeRequest());
return dispatch(verifyCredentials(token, accountUrl)).catch(error => { return dispatch(loadCredentials(token, accountUrl)).catch(error => {
dispatch(fetchMeFail(error)); dispatch(fetchMeFail(error));
}); });
}; };
@ -66,7 +66,6 @@ export function fetchMeRequest() {
export function fetchMeSuccess(me) { export function fetchMeSuccess(me) {
return (dispatch, getState) => { return (dispatch, getState) => {
dispatch(importFetchedAccount(me));
dispatch({ dispatch({
type: ME_FETCH_SUCCESS, type: ME_FETCH_SUCCESS,
me, me,

View file

@ -1,15 +1,16 @@
import { showAlertForError } from '../actions/alerts'; import { showAlertForError } from '../actions/alerts';
const defaultFailSuffix = 'FAIL'; const isFailType = type => type.endsWith('_FAIL');
const isRememberFailType = type => type.endsWith('_REMEMBER_FAIL');
const shouldShowError = ({ type, skipAlert }) => {
return !skipAlert && isFailType(type) && !isRememberFailType(type);
};
export default function errorsMiddleware() { export default function errorsMiddleware() {
return ({ dispatch }) => next => action => { return ({ dispatch }) => next => action => {
if (action.type && !action.skipAlert) { if (shouldShowError(action)) {
const isFail = new RegExp(`${defaultFailSuffix}$`, 'g'); dispatch(showAlertForError(action.error));
if (action.type.match(isFail)) {
dispatch(showAlertForError(action.error));
}
} }
return next(action); return next(action);

View file

@ -4,7 +4,7 @@
*/ */
import { ME_FETCH_SUCCESS, ME_PATCH_SUCCESS } from 'soapbox/actions/me'; import { ME_FETCH_SUCCESS, ME_PATCH_SUCCESS } from 'soapbox/actions/me';
import { VERIFY_CREDENTIALS_SUCCESS } from 'soapbox/actions/auth'; import { VERIFY_CREDENTIALS_SUCCESS, AUTH_ACCOUNT_REMEMBER_SUCCESS } from 'soapbox/actions/auth';
import { Map as ImmutableMap, fromJS } from 'immutable'; import { Map as ImmutableMap, fromJS } from 'immutable';
const initialState = ImmutableMap(); const initialState = ImmutableMap();
@ -24,6 +24,7 @@ export default function accounts_meta(state = initialState, action) {
case ME_PATCH_SUCCESS: case ME_PATCH_SUCCESS:
return importAccount(state, fromJS(action.me)); return importAccount(state, fromJS(action.me));
case VERIFY_CREDENTIALS_SUCCESS: case VERIFY_CREDENTIALS_SUCCESS:
case AUTH_ACCOUNT_REMEMBER_SUCCESS:
return importAccount(state, fromJS(action.account)); return importAccount(state, fromJS(action.account));
default: default:
return state; return state;

View file

@ -4,7 +4,11 @@ import {
ME_FETCH_SKIP, ME_FETCH_SKIP,
ME_PATCH_SUCCESS, ME_PATCH_SUCCESS,
} from '../actions/me'; } from '../actions/me';
import { AUTH_LOGGED_OUT, VERIFY_CREDENTIALS_SUCCESS } from '../actions/auth'; import {
AUTH_LOGGED_OUT,
AUTH_ACCOUNT_REMEMBER_SUCCESS,
VERIFY_CREDENTIALS_SUCCESS,
} from '../actions/auth';
const initialState = null; const initialState = null;
@ -14,11 +18,13 @@ export default function me(state = initialState, action) {
case ME_PATCH_SUCCESS: case ME_PATCH_SUCCESS:
return action.me.id; return action.me.id;
case VERIFY_CREDENTIALS_SUCCESS: case VERIFY_CREDENTIALS_SUCCESS:
case AUTH_ACCOUNT_REMEMBER_SUCCESS:
return state || action.account.id; return state || action.account.id;
case ME_FETCH_FAIL:
case ME_FETCH_SKIP: case ME_FETCH_SKIP:
case AUTH_LOGGED_OUT: case AUTH_LOGGED_OUT:
return false; return false;
case ME_FETCH_FAIL:
return [401, 403].includes(action.error.response.status) ? false : state;
default: default:
return state; return state;
} }