accounts-frontend/src/components/auth/reducer.js

176 lines
3.3 KiB
JavaScript
Raw Normal View History

// @flow
import { combineReducers } from 'redux';
import {
ERROR,
SET_CLIENT,
SET_OAUTH,
SET_OAUTH_RESULT,
SET_SCOPES,
SET_LOADING_STATE,
REQUIRE_PERMISSIONS_ACCEPT,
2017-08-23 00:09:08 +05:30
SET_CREDENTIALS,
2016-11-13 20:17:56 +05:30
SET_SWITCHER
} from './actions';
type Credentials = {
login?: string,
password?: string,
rememberMe?: bool,
returnUrl?: string,
isRelogin?: bool,
isTotpRequired?: bool,
};
export default combineReducers({
2017-08-23 00:09:08 +05:30
credentials,
2016-02-23 11:27:16 +05:30
error,
isLoading,
2016-11-13 20:17:56 +05:30
isSwitcherEnabled,
2016-02-27 16:23:58 +05:30
client,
oauth,
scopes
});
function error(
state = null,
{type, payload = null, error = false}
) {
switch (type) {
case ERROR:
if (!error) {
throw new Error('Expected payload with error');
}
return payload;
default:
return state;
}
}
2017-08-23 00:09:08 +05:30
function credentials(
state = {},
{type, payload}: {
type: string,
payload: ?Credentials
2017-08-23 00:09:08 +05:30
}
) {
2017-08-23 00:09:08 +05:30
if (type === SET_CREDENTIALS) {
if (payload
&& typeof payload === 'object'
) {
return {
...payload
};
}
2017-08-23 00:09:08 +05:30
return {};
}
2017-08-23 00:09:08 +05:30
return state;
}
2016-02-23 11:27:16 +05:30
2016-11-13 20:17:56 +05:30
function isSwitcherEnabled(
state = true,
{type, payload = false}
) {
switch (type) {
case SET_SWITCHER:
if (typeof payload !== 'boolean') {
throw new Error('Expected payload of boolean type');
}
return payload;
default:
return state;
}
}
function isLoading(
state = false,
{type, payload = null}
) {
switch (type) {
case SET_LOADING_STATE:
return !!payload;
default:
return state;
}
}
2016-02-23 11:27:16 +05:30
function client(
state = null,
{type, payload = {}}
) {
switch (type) {
case SET_CLIENT:
return {
id: payload.id,
name: payload.name,
description: payload.description
};
default:
return state;
}
}
2016-02-27 16:23:58 +05:30
function oauth(
state = null,
{type, payload = {}}
) {
switch (type) {
case SET_OAUTH:
return {
clientId: payload.clientId,
redirectUrl: payload.redirectUrl,
responseType: payload.responseType,
scope: payload.scope,
prompt: payload.prompt,
loginHint: payload.loginHint,
2016-02-27 16:23:58 +05:30
state: payload.state
};
case SET_OAUTH_RESULT:
return {
...state,
success: payload.success,
code: payload.code,
displayCode: payload.displayCode
};
case REQUIRE_PERMISSIONS_ACCEPT:
return {
...state,
acceptRequired: true
};
2016-02-27 16:23:58 +05:30
default:
return state;
}
}
function scopes(
state = [],
{type, payload = []}
) {
switch (type) {
case SET_SCOPES:
return payload;
default:
return state;
}
}
2017-08-23 00:09:08 +05:30
export function getLogin(state: Object): ?string {
return state.auth.credentials.login || null;
}
export function getCredentials(state: Object): Credentials {
2017-08-23 00:09:08 +05:30
return state.auth.credentials;
}