2016-02-13 20:58:47 +05:30
|
|
|
import { combineReducers } from 'redux';
|
2019-12-09 12:37:07 +05:30
|
|
|
import { RootState } from 'app/reducers';
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2016-11-13 02:01:44 +05:30
|
|
|
import {
|
2019-11-27 14:33:32 +05:30
|
|
|
ERROR,
|
|
|
|
SET_CLIENT,
|
|
|
|
SET_OAUTH,
|
|
|
|
SET_OAUTH_RESULT,
|
|
|
|
SET_SCOPES,
|
|
|
|
SET_LOADING_STATE,
|
|
|
|
REQUIRE_PERMISSIONS_ACCEPT,
|
|
|
|
SET_CREDENTIALS,
|
|
|
|
SET_SWITCHER,
|
2016-11-13 02:01:44 +05:30
|
|
|
} from './actions';
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2018-02-18 01:29:35 +05:30
|
|
|
type Credentials = {
|
2019-12-07 16:58:52 +05:30
|
|
|
login?: string;
|
|
|
|
password?: string;
|
|
|
|
rememberMe?: boolean;
|
|
|
|
returnUrl?: string;
|
|
|
|
isRelogin?: boolean;
|
|
|
|
isTotpRequired?: boolean;
|
2018-02-18 01:29:35 +05:30
|
|
|
};
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export interface Client {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
}
|
|
|
|
|
2019-12-09 12:37:07 +05:30
|
|
|
interface OAuthState {
|
|
|
|
clientId: string;
|
|
|
|
redirectUrl: string;
|
|
|
|
responseType: string;
|
|
|
|
description?: string;
|
|
|
|
scope: string;
|
|
|
|
prompt: string;
|
|
|
|
loginHint: string;
|
|
|
|
state: string;
|
|
|
|
success?: boolean;
|
|
|
|
code?: string;
|
|
|
|
displayCode?: string;
|
|
|
|
acceptRequired?: boolean;
|
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export interface State {
|
|
|
|
credentials: Credentials;
|
|
|
|
error:
|
2019-12-09 12:37:07 +05:30
|
|
|
| null
|
2019-12-07 16:58:52 +05:30
|
|
|
| string
|
|
|
|
| {
|
|
|
|
type: string;
|
|
|
|
payload: { [key: string]: any };
|
|
|
|
};
|
|
|
|
isLoading: boolean;
|
|
|
|
isSwitcherEnabled: boolean;
|
|
|
|
client: Client | null;
|
2019-12-09 12:37:07 +05:30
|
|
|
oauth: OAuthState | null;
|
2019-12-07 16:58:52 +05:30
|
|
|
scopes: string[];
|
|
|
|
}
|
|
|
|
|
2019-12-09 12:37:07 +05:30
|
|
|
export default combineReducers<State>({
|
2019-11-27 14:33:32 +05:30
|
|
|
credentials,
|
|
|
|
error,
|
|
|
|
isLoading,
|
|
|
|
isSwitcherEnabled,
|
|
|
|
client,
|
|
|
|
oauth,
|
|
|
|
scopes,
|
2016-02-13 20:58:47 +05:30
|
|
|
});
|
|
|
|
|
2019-12-09 12:37:07 +05:30
|
|
|
function error(
|
|
|
|
state = null,
|
|
|
|
{ type, payload = null, error = false },
|
|
|
|
): State['error'] {
|
2019-11-27 14:33:32 +05:30
|
|
|
switch (type) {
|
|
|
|
case ERROR:
|
|
|
|
if (!error) {
|
|
|
|
throw new Error('Expected payload with error');
|
|
|
|
}
|
2016-11-13 02:01:44 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return payload;
|
2016-11-13 02:01:44 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2016-11-13 02:01:44 +05:30
|
|
|
}
|
|
|
|
|
2017-08-23 00:09:08 +05:30
|
|
|
function credentials(
|
2019-11-27 14:33:32 +05:30
|
|
|
state = {},
|
|
|
|
{
|
|
|
|
type,
|
|
|
|
payload,
|
|
|
|
}: {
|
2019-12-07 16:58:52 +05:30
|
|
|
type: string;
|
|
|
|
payload: Credentials | null;
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
2019-12-09 12:37:07 +05:30
|
|
|
): State['credentials'] {
|
2019-11-27 14:33:32 +05:30
|
|
|
if (type === SET_CREDENTIALS) {
|
|
|
|
if (payload && typeof payload === 'object') {
|
|
|
|
return {
|
|
|
|
...payload,
|
|
|
|
};
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
2017-08-23 00:09:08 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
2016-02-23 11:27:16 +05:30
|
|
|
|
2019-12-09 12:37:07 +05:30
|
|
|
function isSwitcherEnabled(
|
|
|
|
state = true,
|
|
|
|
{ type, payload = false },
|
|
|
|
): State['isSwitcherEnabled'] {
|
2019-11-27 14:33:32 +05:30
|
|
|
switch (type) {
|
|
|
|
case SET_SWITCHER:
|
|
|
|
if (typeof payload !== 'boolean') {
|
|
|
|
throw new Error('Expected payload of boolean type');
|
|
|
|
}
|
2016-11-13 20:17:56 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return payload;
|
2016-11-13 20:17:56 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2016-11-13 20:17:56 +05:30
|
|
|
}
|
2016-04-02 16:28:54 +05:30
|
|
|
|
2019-12-09 12:37:07 +05:30
|
|
|
function isLoading(
|
|
|
|
state = false,
|
|
|
|
{ type, payload = null },
|
|
|
|
): State['isLoading'] {
|
2019-11-27 14:33:32 +05:30
|
|
|
switch (type) {
|
|
|
|
case SET_LOADING_STATE:
|
|
|
|
return !!payload;
|
2016-04-02 16:28:54 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2016-04-02 16:28:54 +05:30
|
|
|
}
|
|
|
|
|
2019-12-09 12:37:07 +05:30
|
|
|
function client(state = null, { type, payload }): State['client'] {
|
2019-11-27 14:33:32 +05:30
|
|
|
switch (type) {
|
|
|
|
case SET_CLIENT:
|
|
|
|
return {
|
|
|
|
id: payload.id,
|
|
|
|
name: payload.name,
|
|
|
|
description: payload.description,
|
|
|
|
};
|
|
|
|
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2016-02-23 11:27:16 +05:30
|
|
|
}
|
2016-02-27 16:23:58 +05:30
|
|
|
|
2019-12-09 12:37:07 +05:30
|
|
|
function oauth(
|
|
|
|
state: State['oauth'] = null,
|
|
|
|
{ type, payload },
|
|
|
|
): State['oauth'] {
|
2019-11-27 14:33:32 +05:30
|
|
|
switch (type) {
|
|
|
|
case SET_OAUTH:
|
|
|
|
return {
|
|
|
|
clientId: payload.clientId,
|
|
|
|
redirectUrl: payload.redirectUrl,
|
|
|
|
responseType: payload.responseType,
|
|
|
|
scope: payload.scope,
|
|
|
|
prompt: payload.prompt,
|
|
|
|
loginHint: payload.loginHint,
|
|
|
|
state: payload.state,
|
|
|
|
};
|
|
|
|
|
|
|
|
case SET_OAUTH_RESULT:
|
|
|
|
return {
|
2019-12-09 12:37:07 +05:30
|
|
|
...(state as OAuthState),
|
2019-11-27 14:33:32 +05:30
|
|
|
success: payload.success,
|
|
|
|
code: payload.code,
|
|
|
|
displayCode: payload.displayCode,
|
|
|
|
};
|
|
|
|
|
|
|
|
case REQUIRE_PERMISSIONS_ACCEPT:
|
|
|
|
return {
|
2019-12-09 12:37:07 +05:30
|
|
|
...(state as OAuthState),
|
2019-11-27 14:33:32 +05:30
|
|
|
acceptRequired: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2016-02-27 16:23:58 +05:30
|
|
|
}
|
2016-02-29 23:46:33 +05:30
|
|
|
|
2019-12-09 12:37:07 +05:30
|
|
|
function scopes(state = [], { type, payload = [] }): State['scopes'] {
|
2019-11-27 14:33:32 +05:30
|
|
|
switch (type) {
|
|
|
|
case SET_SCOPES:
|
|
|
|
return payload;
|
2016-02-29 23:46:33 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2016-02-29 23:46:33 +05:30
|
|
|
}
|
2017-08-23 00:09:08 +05:30
|
|
|
|
2019-12-09 12:37:07 +05:30
|
|
|
export function getLogin(state: RootState): string | null {
|
2019-11-27 14:33:32 +05:30
|
|
|
return state.auth.credentials.login || null;
|
2017-08-23 00:09:08 +05:30
|
|
|
}
|
|
|
|
|
2019-12-09 12:37:07 +05:30
|
|
|
export function getCredentials(state: RootState): Credentials {
|
2019-11-27 14:33:32 +05:30
|
|
|
return state.auth.credentials;
|
2017-08-23 00:09:08 +05:30
|
|
|
}
|