2022-12-25 15:31:07 -08:00
|
|
|
import { List as ImmutableList, Map as ImmutableMap, Record as ImmutableRecord, fromJS } from 'immutable';
|
2022-06-16 12:32:17 -07:00
|
|
|
import trim from 'lodash/trim';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-01-10 14:01:24 -08:00
|
|
|
import { MASTODON_PRELOAD_IMPORT } from 'soapbox/actions/preload';
|
2022-12-25 15:31:07 -08:00
|
|
|
import BuildConfig from 'soapbox/build-config';
|
2022-11-15 12:42:22 -08:00
|
|
|
import KVStore from 'soapbox/storage/kv-store';
|
2022-01-10 14:17:52 -08:00
|
|
|
import { validId, isURL } from 'soapbox/utils/auth';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2020-04-05 16:39:22 -07:00
|
|
|
import {
|
|
|
|
AUTH_APP_CREATED,
|
|
|
|
AUTH_LOGGED_IN,
|
|
|
|
AUTH_APP_AUTHORIZED,
|
2020-04-11 12:41:13 -07:00
|
|
|
AUTH_LOGGED_OUT,
|
2021-03-23 17:06:55 -07:00
|
|
|
SWITCH_ACCOUNT,
|
2021-03-23 22:05:06 -07:00
|
|
|
VERIFY_CREDENTIALS_SUCCESS,
|
2021-03-24 12:15:36 -07:00
|
|
|
VERIFY_CREDENTIALS_FAIL,
|
2020-04-05 16:39:22 -07:00
|
|
|
} from '../actions/auth';
|
2022-01-10 14:17:52 -08:00
|
|
|
import { ME_FETCH_SKIP } from '../actions/me';
|
2020-04-05 14:54:51 -07:00
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
import type { AxiosError } from 'axios';
|
|
|
|
import type { AnyAction } from 'redux';
|
|
|
|
import type { APIEntity, Account as AccountEntity } from 'soapbox/types/entities';
|
|
|
|
|
|
|
|
export const AuthAppRecord = ImmutableRecord({
|
2022-12-26 05:09:09 -08:00
|
|
|
access_token: null as string | null,
|
|
|
|
client_id: null as string | null,
|
|
|
|
client_secret: null as string | null,
|
|
|
|
id: null as string | null,
|
|
|
|
name: null as string | null,
|
|
|
|
redirect_uri: null as string | null,
|
|
|
|
token_type: null as string | null,
|
|
|
|
vapid_key: null as string | null,
|
|
|
|
website: null as string | null,
|
2022-12-25 15:31:07 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
export const AuthTokenRecord = ImmutableRecord({
|
2022-12-31 10:48:31 -08:00
|
|
|
access_token: '',
|
2022-12-26 05:09:09 -08:00
|
|
|
account: null as string | null,
|
2022-12-31 10:48:31 -08:00
|
|
|
created_at: 0,
|
2022-12-25 15:31:07 -08:00
|
|
|
expires_in: null as number | null,
|
|
|
|
id: null as number | null,
|
2022-12-26 05:09:09 -08:00
|
|
|
me: null as string | null,
|
2022-12-25 15:31:07 -08:00
|
|
|
refresh_token: null as string | null,
|
2022-12-31 10:48:31 -08:00
|
|
|
scope: '',
|
|
|
|
token_type: '',
|
2022-12-26 05:09:09 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
export const AuthUserRecord = ImmutableRecord({
|
2022-12-31 10:48:31 -08:00
|
|
|
access_token: '',
|
|
|
|
id: '',
|
|
|
|
url: '',
|
2022-12-25 15:31:07 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
export const ReducerRecord = ImmutableRecord({
|
|
|
|
app: AuthAppRecord(),
|
|
|
|
tokens: ImmutableMap<string, AuthToken>(),
|
|
|
|
users: ImmutableMap<string, AuthUser>(),
|
|
|
|
me: null as string | null,
|
|
|
|
});
|
|
|
|
|
|
|
|
type AuthToken = ReturnType<typeof AuthTokenRecord>;
|
|
|
|
type AuthUser = ReturnType<typeof AuthUserRecord>;
|
|
|
|
type State = ReturnType<typeof ReducerRecord>;
|
|
|
|
|
|
|
|
const buildKey = (parts: string[]) => parts.join(':');
|
2021-09-04 12:18:47 -07:00
|
|
|
|
|
|
|
// For subdirectory support
|
2022-12-25 15:31:07 -08:00
|
|
|
const NAMESPACE = trim(BuildConfig.FE_SUBDIRECTORY, '/') ? `soapbox@${BuildConfig.FE_SUBDIRECTORY}` : 'soapbox';
|
2021-09-04 12:18:47 -07:00
|
|
|
|
|
|
|
const STORAGE_KEY = buildKey([NAMESPACE, 'auth']);
|
|
|
|
const SESSION_KEY = buildKey([NAMESPACE, 'auth', 'me']);
|
|
|
|
|
2021-04-10 15:15:52 -07:00
|
|
|
const getSessionUser = () => {
|
2021-09-04 12:18:47 -07:00
|
|
|
const id = sessionStorage.getItem(SESSION_KEY);
|
2021-07-09 13:54:32 -07:00
|
|
|
return validId(id) ? id : undefined;
|
2021-04-10 15:15:52 -07:00
|
|
|
};
|
|
|
|
|
2022-12-26 05:32:58 -08:00
|
|
|
const getLocalState = () => {
|
|
|
|
const state = JSON.parse(localStorage.getItem(STORAGE_KEY)!);
|
|
|
|
|
|
|
|
if (!state) return undefined;
|
|
|
|
|
|
|
|
return ReducerRecord({
|
|
|
|
app: AuthAppRecord(state.app),
|
|
|
|
tokens: ImmutableMap(Object.entries(state.tokens).map(([key, value]) => [key, AuthTokenRecord(value as any)])),
|
|
|
|
users: ImmutableMap(Object.entries(state.users).map(([key, value]) => [key, AuthUserRecord(value as any)])),
|
|
|
|
me: state.me,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-04-10 15:15:52 -07:00
|
|
|
const sessionUser = getSessionUser();
|
2022-12-26 05:32:58 -08:00
|
|
|
export const localState = getLocalState(); fromJS(JSON.parse(localStorage.getItem(STORAGE_KEY)!));
|
2021-03-23 22:05:06 -07:00
|
|
|
|
2021-07-09 13:54:32 -07:00
|
|
|
// Checks if the user has an ID and access token
|
2022-12-25 15:31:07 -08:00
|
|
|
const validUser = (user?: AuthUser) => {
|
2021-07-09 13:54:32 -07:00
|
|
|
try {
|
2022-12-25 15:31:07 -08:00
|
|
|
return !!(user && validId(user.id) && validId(user.access_token));
|
2022-04-11 12:58:48 -07:00
|
|
|
} catch (e) {
|
2021-07-09 13:54:32 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Finds the first valid user in the state
|
2022-12-25 15:31:07 -08:00
|
|
|
const firstValidUser = (state: State) => state.users.find(validUser);
|
2021-07-09 13:54:32 -07:00
|
|
|
|
2021-08-21 14:54:53 -07:00
|
|
|
// For legacy purposes. IDs get upgraded to URLs further down.
|
2022-12-25 15:31:07 -08:00
|
|
|
const getUrlOrId = (user?: AuthUser): string | null => {
|
2021-08-21 14:54:53 -07:00
|
|
|
try {
|
2022-12-25 15:31:07 -08:00
|
|
|
const { id, url } = user!.toJS();
|
|
|
|
return (url || id) as string;
|
2021-08-21 14:54:53 -07:00
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-24 12:15:36 -07:00
|
|
|
// If `me` doesn't match an existing user, attempt to shift it.
|
2022-12-25 15:31:07 -08:00
|
|
|
const maybeShiftMe = (state: State) => {
|
|
|
|
const me = state.me!;
|
|
|
|
const user = state.users.get(me);
|
2021-03-24 12:15:36 -07:00
|
|
|
|
2021-08-21 14:54:53 -07:00
|
|
|
if (!validUser(user)) {
|
2021-07-09 13:54:32 -07:00
|
|
|
const nextUser = firstValidUser(state);
|
2021-08-21 14:54:53 -07:00
|
|
|
return state.set('me', getUrlOrId(nextUser));
|
2021-03-24 12:15:36 -07:00
|
|
|
} else {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-09 13:54:32 -07:00
|
|
|
// Set the user from the session or localStorage, whichever is valid first
|
2022-12-25 15:31:07 -08:00
|
|
|
const setSessionUser = (state: State) => state.update('me', me => {
|
|
|
|
const user = ImmutableList<AuthUser>([
|
|
|
|
state.users.get(sessionUser!)!,
|
|
|
|
state.users.get(me!)!,
|
2021-07-09 13:54:32 -07:00
|
|
|
]).find(validUser);
|
|
|
|
|
2021-08-21 14:54:53 -07:00
|
|
|
return getUrlOrId(user);
|
2021-07-09 13:54:32 -07:00
|
|
|
});
|
2021-03-25 13:15:37 -07:00
|
|
|
|
2021-03-24 14:18:14 -07:00
|
|
|
// Upgrade the initial state
|
2022-12-25 15:31:07 -08:00
|
|
|
const migrateLegacy = (state: State) => {
|
2021-03-24 14:18:14 -07:00
|
|
|
if (localState) return state;
|
|
|
|
return state.withMutations(state => {
|
2022-12-25 15:31:07 -08:00
|
|
|
const app = AuthAppRecord(JSON.parse(localStorage.getItem('soapbox:auth:app')!));
|
|
|
|
const user = fromJS(JSON.parse(localStorage.getItem('soapbox:auth:user')!)) as ImmutableMap<string, any>;
|
2021-03-25 15:12:31 -07:00
|
|
|
if (!user) return;
|
2021-03-24 14:18:14 -07:00
|
|
|
state.set('me', '_legacy'); // Placeholder account ID
|
|
|
|
state.set('app', app);
|
|
|
|
state.set('tokens', ImmutableMap({
|
2022-12-25 15:31:07 -08:00
|
|
|
[user.get('access_token')]: AuthTokenRecord(user.set('account', '_legacy')),
|
2021-03-24 14:18:14 -07:00
|
|
|
}));
|
|
|
|
state.set('users', ImmutableMap({
|
2022-12-25 15:31:07 -08:00
|
|
|
'_legacy': AuthUserRecord({
|
2021-03-24 14:18:14 -07:00
|
|
|
id: '_legacy',
|
|
|
|
access_token: user.get('access_token'),
|
|
|
|
}),
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
const isUpgradingUrlId = (state: State) => {
|
|
|
|
const me = state.me;
|
|
|
|
const user = state.users.get(me!);
|
2021-08-21 14:54:53 -07:00
|
|
|
return validId(me) && user && !isURL(me);
|
|
|
|
};
|
|
|
|
|
2021-07-09 14:24:18 -07:00
|
|
|
// Checks the state and makes it valid
|
2022-12-25 15:31:07 -08:00
|
|
|
const sanitizeState = (state: State) => {
|
2021-08-21 14:54:53 -07:00
|
|
|
// Skip sanitation during ID to URL upgrade
|
|
|
|
if (isUpgradingUrlId(state)) return state;
|
|
|
|
|
2021-07-09 14:24:18 -07:00
|
|
|
return state.withMutations(state => {
|
|
|
|
// Remove invalid users, ensure ID match
|
2022-12-25 15:31:07 -08:00
|
|
|
state.update('users', users => (
|
2021-08-21 14:54:53 -07:00
|
|
|
users.filter((user, url) => (
|
|
|
|
validUser(user) && user.get('url') === url
|
2021-07-09 14:24:18 -07:00
|
|
|
))
|
|
|
|
));
|
|
|
|
// Remove mismatched tokens
|
2022-12-25 15:31:07 -08:00
|
|
|
state.update('tokens', tokens => (
|
2021-07-09 14:24:18 -07:00
|
|
|
tokens.filter((token, id) => (
|
|
|
|
validId(id) && token.get('access_token') === id
|
|
|
|
))
|
|
|
|
));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
const persistAuth = (state: State) => localStorage.setItem(STORAGE_KEY, JSON.stringify(state.toJS()));
|
2021-04-10 15:15:52 -07:00
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
const persistSession = (state: State) => {
|
|
|
|
const me = state.me;
|
2021-04-10 15:15:52 -07:00
|
|
|
if (me && typeof me === 'string') {
|
2021-09-04 12:18:47 -07:00
|
|
|
sessionStorage.setItem(SESSION_KEY, me);
|
2021-04-10 15:15:52 -07:00
|
|
|
}
|
|
|
|
};
|
2021-03-29 21:22:54 -07:00
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
const persistState = (state: State) => {
|
2021-03-29 21:22:54 -07:00
|
|
|
persistAuth(state);
|
|
|
|
persistSession(state);
|
2021-03-29 18:03:27 -07:00
|
|
|
};
|
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
const initialize = (state: State) => {
|
2021-03-29 17:42:14 -07:00
|
|
|
return state.withMutations(state => {
|
|
|
|
maybeShiftMe(state);
|
|
|
|
setSessionUser(state);
|
|
|
|
migrateLegacy(state);
|
2021-07-09 14:24:18 -07:00
|
|
|
sanitizeState(state);
|
2021-03-29 18:03:27 -07:00
|
|
|
persistState(state);
|
2021-03-29 17:42:14 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
const initialState = initialize(ReducerRecord().merge(localState as any));
|
2021-03-29 17:42:14 -07:00
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
const importToken = (state: State, token: APIEntity) => {
|
2022-12-26 05:09:09 -08:00
|
|
|
return state.setIn(['tokens', token.access_token], AuthTokenRecord(token));
|
2021-03-29 17:42:14 -07:00
|
|
|
};
|
|
|
|
|
2021-03-24 14:18:14 -07:00
|
|
|
// Upgrade the `_legacy` placeholder ID with a real account
|
2022-12-25 15:31:07 -08:00
|
|
|
const upgradeLegacyId = (state: State, account: APIEntity) => {
|
2021-03-24 14:18:14 -07:00
|
|
|
if (localState) return state;
|
|
|
|
return state.withMutations(state => {
|
2022-12-25 15:31:07 -08:00
|
|
|
state.update('me', me => me === '_legacy' ? account.url : me);
|
2021-03-24 14:18:14 -07:00
|
|
|
state.deleteIn(['users', '_legacy']);
|
|
|
|
});
|
|
|
|
// TODO: Delete `soapbox:auth:app` and `soapbox:auth:user` localStorage?
|
|
|
|
// By this point it's probably safe, but we'll leave it just in case.
|
|
|
|
};
|
|
|
|
|
2021-08-21 14:54:53 -07:00
|
|
|
// Users are now stored by their ActivityPub ID instead of their
|
|
|
|
// primary key to support auth against multiple hosts.
|
2022-12-25 15:31:07 -08:00
|
|
|
const upgradeNonUrlId = (state: State, account: APIEntity) => {
|
|
|
|
const me = state.me;
|
2021-08-21 14:54:53 -07:00
|
|
|
if (isURL(me)) return state;
|
|
|
|
|
|
|
|
return state.withMutations(state => {
|
2022-12-25 15:31:07 -08:00
|
|
|
state.update('me', me => me === account.id ? account.url : me);
|
2021-08-21 14:54:53 -07:00
|
|
|
state.deleteIn(['users', account.id]);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-04-13 10:03:46 -07:00
|
|
|
// Returns a predicate function for filtering a mismatched user/token
|
2022-12-25 15:31:07 -08:00
|
|
|
const userMismatch = (token: string, account: APIEntity) => {
|
|
|
|
return (user: AuthUser, url: string) => {
|
2021-04-13 10:03:46 -07:00
|
|
|
const sameToken = user.get('access_token') === token;
|
2021-08-21 14:54:53 -07:00
|
|
|
const differentUrl = url !== account.url || user.get('url') !== account.url;
|
|
|
|
const differentId = user.get('id') !== account.id;
|
2021-04-13 10:03:46 -07:00
|
|
|
|
2021-08-21 14:54:53 -07:00
|
|
|
return sameToken && (differentUrl || differentId);
|
2021-04-13 10:03:46 -07:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
const importCredentials = (state: State, token: string, account: APIEntity) => {
|
2021-03-29 17:42:14 -07:00
|
|
|
return state.withMutations(state => {
|
2022-12-26 05:09:09 -08:00
|
|
|
state.setIn(['users', account.url], AuthUserRecord({
|
2021-03-29 17:42:14 -07:00
|
|
|
id: account.id,
|
|
|
|
access_token: token,
|
2021-08-21 14:54:53 -07:00
|
|
|
url: account.url,
|
2021-03-29 17:42:14 -07:00
|
|
|
}));
|
|
|
|
state.setIn(['tokens', token, 'account'], account.id);
|
2021-08-21 14:54:53 -07:00
|
|
|
state.setIn(['tokens', token, 'me'], account.url);
|
2022-12-25 15:31:07 -08:00
|
|
|
state.update('users', users => users.filterNot(userMismatch(token, account)));
|
|
|
|
state.update('me', me => me || account.url);
|
2021-03-29 17:42:14 -07:00
|
|
|
upgradeLegacyId(state, account);
|
2021-08-21 14:54:53 -07:00
|
|
|
upgradeNonUrlId(state, account);
|
2021-03-29 17:42:14 -07:00
|
|
|
});
|
2021-03-29 11:02:11 -07:00
|
|
|
};
|
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
const deleteToken = (state: State, token: string) => {
|
2021-03-24 14:18:14 -07:00
|
|
|
return state.withMutations(state => {
|
2022-12-25 15:31:07 -08:00
|
|
|
state.update('tokens', tokens => tokens.delete(token));
|
|
|
|
state.update('users', users => users.filterNot(user => user.get('access_token') === token));
|
2021-03-29 17:42:14 -07:00
|
|
|
maybeShiftMe(state);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
const deleteUser = (state: State, account: AccountEntity) => {
|
2022-12-26 05:09:09 -08:00
|
|
|
const accountUrl = account.get('url');
|
2021-08-21 14:54:53 -07:00
|
|
|
|
2021-03-29 17:42:14 -07:00
|
|
|
return state.withMutations(state => {
|
2022-12-25 15:31:07 -08:00
|
|
|
state.update('users', users => users.delete(accountUrl));
|
|
|
|
state.update('tokens', tokens => tokens.filterNot(token => token.get('me') === accountUrl));
|
2021-03-24 14:18:14 -07:00
|
|
|
maybeShiftMe(state);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
const importMastodonPreload = (state: State, data: ImmutableMap<string, any>) => {
|
2021-09-15 11:34:22 -07:00
|
|
|
return state.withMutations(state => {
|
2022-12-26 05:09:09 -08:00
|
|
|
const accountId = data.getIn(['meta', 'me']) as string;
|
2022-12-25 15:31:07 -08:00
|
|
|
const accountUrl = data.getIn(['accounts', accountId, 'url']) as string;
|
2022-12-26 05:09:09 -08:00
|
|
|
const accessToken = data.getIn(['meta', 'access_token']) as string;
|
2021-09-15 11:34:22 -07:00
|
|
|
|
|
|
|
if (validId(accessToken) && validId(accountId) && isURL(accountUrl)) {
|
2022-12-26 05:09:09 -08:00
|
|
|
state.setIn(['tokens', accessToken], AuthTokenRecord({
|
2021-09-15 11:34:22 -07:00
|
|
|
access_token: accessToken,
|
|
|
|
account: accountId,
|
|
|
|
me: accountUrl,
|
|
|
|
scope: 'read write follow push',
|
|
|
|
token_type: 'Bearer',
|
2022-12-26 05:09:09 -08:00
|
|
|
}));
|
2021-09-15 11:34:22 -07:00
|
|
|
|
2022-12-26 05:09:09 -08:00
|
|
|
state.setIn(['users', accountUrl], AuthUserRecord({
|
2021-09-15 11:34:22 -07:00
|
|
|
id: accountId,
|
|
|
|
access_token: accessToken,
|
|
|
|
url: accountUrl,
|
2022-12-26 05:09:09 -08:00
|
|
|
}));
|
2021-09-15 11:34:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
maybeShiftMe(state);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
const persistAuthAccount = (account: APIEntity) => {
|
2021-10-20 13:17:02 -07:00
|
|
|
if (account && account.url) {
|
2022-04-29 21:10:59 -07:00
|
|
|
const key = `authAccount:${account.url}`;
|
|
|
|
if (!account.pleroma) account.pleroma = {};
|
2022-12-25 15:31:07 -08:00
|
|
|
KVStore.getItem(key).then((oldAccount: any) => {
|
2022-04-29 21:10:59 -07:00
|
|
|
const settings = oldAccount?.pleroma?.settings_store || {};
|
|
|
|
if (!account.pleroma.settings_store) {
|
|
|
|
account.pleroma.settings_store = settings;
|
|
|
|
}
|
|
|
|
KVStore.setItem(key, account);
|
|
|
|
})
|
|
|
|
.catch(console.error);
|
2021-10-20 13:17:02 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
const deleteForbiddenToken = (state: State, error: AxiosError, token: string) => {
|
|
|
|
if ([401, 403].includes(error.response?.status!)) {
|
2021-10-20 15:50:35 -07:00
|
|
|
return deleteToken(state, token);
|
|
|
|
} else {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
const reducer = (state: State, action: AnyAction) => {
|
2022-05-11 10:40:34 -07:00
|
|
|
switch (action.type) {
|
2022-05-11 14:06:35 -07:00
|
|
|
case AUTH_APP_CREATED:
|
2022-12-25 15:31:07 -08:00
|
|
|
return state.set('app', AuthAppRecord(action.app));
|
2022-05-11 14:06:35 -07:00
|
|
|
case AUTH_APP_AUTHORIZED:
|
2022-12-25 15:31:07 -08:00
|
|
|
return state.update('app', app => app.merge(action.token));
|
2022-05-11 14:06:35 -07:00
|
|
|
case AUTH_LOGGED_IN:
|
|
|
|
return importToken(state, action.token);
|
|
|
|
case AUTH_LOGGED_OUT:
|
|
|
|
return deleteUser(state, action.account);
|
|
|
|
case VERIFY_CREDENTIALS_SUCCESS:
|
|
|
|
persistAuthAccount(action.account);
|
|
|
|
return importCredentials(state, action.token, action.account);
|
|
|
|
case VERIFY_CREDENTIALS_FAIL:
|
|
|
|
return deleteForbiddenToken(state, action.error, action.token);
|
|
|
|
case SWITCH_ACCOUNT:
|
|
|
|
return state.set('me', action.account.get('url'));
|
|
|
|
case ME_FETCH_SKIP:
|
|
|
|
return state.set('me', null);
|
|
|
|
case MASTODON_PRELOAD_IMPORT:
|
2022-12-25 15:31:07 -08:00
|
|
|
return importMastodonPreload(state, fromJS(action.data) as ImmutableMap<string, any>);
|
2022-05-11 14:06:35 -07:00
|
|
|
default:
|
|
|
|
return state;
|
2020-04-05 14:54:51 -07:00
|
|
|
}
|
|
|
|
};
|
2021-03-23 19:52:08 -07:00
|
|
|
|
2021-03-27 12:37:17 -07:00
|
|
|
const reload = () => location.replace('/');
|
2021-03-24 14:49:24 -07:00
|
|
|
|
2021-03-25 21:03:58 -07:00
|
|
|
// `me` is a user ID string
|
2022-12-25 15:31:07 -08:00
|
|
|
const validMe = (state: State) => {
|
|
|
|
const me = state.me;
|
2021-03-25 21:03:58 -07:00
|
|
|
return typeof me === 'string' && me !== '_legacy';
|
|
|
|
};
|
|
|
|
|
2021-03-25 13:59:09 -07:00
|
|
|
// `me` has changed from one valid ID to another
|
2022-12-25 15:31:07 -08:00
|
|
|
const userSwitched = (oldState: State, state: State) => {
|
|
|
|
const me = state.me;
|
|
|
|
const oldMe = oldState.me;
|
2021-08-21 14:54:53 -07:00
|
|
|
|
2021-03-25 13:59:09 -07:00
|
|
|
const stillValid = validMe(oldState) && validMe(state);
|
2021-08-21 14:54:53 -07:00
|
|
|
const didChange = oldMe !== me;
|
2022-12-25 15:31:07 -08:00
|
|
|
const userUpgradedUrl = state.users.get(me!)?.id === oldMe;
|
2021-03-25 13:59:09 -07:00
|
|
|
|
2021-08-21 14:54:53 -07:00
|
|
|
return stillValid && didChange && !userUpgradedUrl;
|
2021-03-25 13:59:09 -07:00
|
|
|
};
|
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
const maybeReload = (oldState: State, state: State, action: AnyAction) => {
|
2021-08-30 16:41:05 -07:00
|
|
|
const loggedOutStandalone = action.type === AUTH_LOGGED_OUT && action.standalone;
|
|
|
|
const switched = userSwitched(oldState, state);
|
|
|
|
|
|
|
|
if (switched || loggedOutStandalone) {
|
2022-12-25 15:31:07 -08:00
|
|
|
reload();
|
2021-03-24 14:49:24 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-25 15:31:07 -08:00
|
|
|
export default function auth(oldState: State = initialState, action: AnyAction) {
|
2021-03-24 12:15:36 -07:00
|
|
|
const state = reducer(oldState, action);
|
2021-03-24 14:49:24 -07:00
|
|
|
|
2021-03-25 18:32:40 -07:00
|
|
|
if (!state.equals(oldState)) {
|
2021-03-29 21:22:54 -07:00
|
|
|
// Persist the state in localStorage
|
|
|
|
persistAuth(state);
|
|
|
|
|
2021-03-29 22:45:23 -07:00
|
|
|
// When middle-clicking a profile, we want to save the
|
|
|
|
// user in localStorage, but not update the reducer
|
|
|
|
if (action.background === true) {
|
2021-03-29 22:35:27 -07:00
|
|
|
return oldState;
|
2021-03-29 21:22:54 -07:00
|
|
|
}
|
2021-03-23 19:52:08 -07:00
|
|
|
|
2021-03-29 22:35:27 -07:00
|
|
|
// Persist the session
|
|
|
|
persistSession(state);
|
|
|
|
|
2021-03-25 18:32:40 -07:00
|
|
|
// Reload the page under some conditions
|
|
|
|
maybeReload(oldState, state, action);
|
|
|
|
}
|
2021-03-23 19:52:08 -07:00
|
|
|
|
|
|
|
return state;
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|