bigbuffet-rw/app/soapbox/utils/auth.js

65 lines
1.5 KiB
JavaScript
Raw Normal View History

import { List as ImmutableList } from 'immutable';
export const validId = id => typeof id === 'string' && id !== 'null' && id !== 'undefined';
export const isURL = url => {
try {
new URL(url);
return true;
} catch {
return false;
}
};
2021-08-21 18:41:29 -07:00
export const parseBaseURL = url => {
try {
return new URL(url).origin;
} catch {
return '';
}
};
export const getLoggedInAccount = state => {
const me = state.get('me');
return state.getIn(['accounts', me]);
};
2021-03-25 10:25:45 -07:00
export const isLoggedIn = getState => {
return validId(getState().get('me'));
2021-03-25 10:25:45 -07:00
};
2021-03-31 12:47:54 -07:00
export const getAppToken = state => state.getIn(['auth', 'app', 'access_token']);
export const getUserToken = (state, accountId) => {
const accountUrl = state.getIn(['accounts', accountId, 'url']);
return state.getIn(['auth', 'users', accountUrl, 'access_token']);
2021-03-31 12:47:54 -07:00
};
2021-03-24 15:53:09 -07:00
export const getAccessToken = state => {
2021-03-29 22:23:29 -07:00
const me = state.get('me');
2021-03-31 12:47:54 -07:00
return getUserToken(state, me);
2021-03-24 15:53:09 -07:00
};
export const getAuthUserId = state => {
const me = state.getIn(['auth', 'me']);
return ImmutableList([
state.getIn(['auth', 'users', me, 'id']),
me,
]).find(validId);
};
export const getAuthUserUrl = state => {
const me = state.getIn(['auth', 'me']);
return ImmutableList([
state.getIn(['auth', 'users', me, 'url']),
me,
]).find(isURL);
};
2021-11-18 13:11:50 -08:00
/** Get the VAPID public key. */
export const getVapidKey = state => {
return state.getIn(['auth', 'app', 'vapid_key']) || state.getIn(['instance', 'pleroma', 'vapid_public_key']);
};