accounts-frontend/packages/app/components/auth/reducer.ts

159 lines
3.4 KiB
TypeScript
Raw Normal View History

import { combineReducers, Reducer } from 'redux';
import { RootState } from 'app/reducers';
import { Scope } from '../../services/api/oauth';
import {
2020-05-24 04:38:24 +05:30
ErrorAction,
CredentialsAction,
AccountSwitcherAction,
LoadingAction,
ClientAction,
OAuthAction,
ScopesAction,
} from './actions';
export interface Credentials {
2020-05-24 04:38:24 +05:30
login?: string | null; // By some reasons there is can be null value. Need to investigate.
password?: string;
rememberMe?: boolean;
returnUrl?: string;
isRelogin?: boolean;
isTotpRequired?: boolean;
}
type Error = Record<
2020-05-24 04:38:24 +05:30
string,
| string
| {
type: string;
payload: Record<string, any>;
}
> | null;
2019-12-07 16:58:52 +05:30
export interface Client {
2020-05-24 04:38:24 +05:30
id: string;
name: string;
description: string;
2019-12-07 16:58:52 +05:30
}
export interface OAuthState {
2020-05-24 04:38:24 +05:30
clientId: string;
redirectUrl: string;
responseType: string;
description?: string;
scope: string;
prompt: string;
loginHint: string;
state: string;
success?: boolean;
code?: string;
displayCode?: boolean;
acceptRequired?: boolean;
}
type Scopes = Array<Scope>;
2019-12-07 16:58:52 +05:30
export interface State {
2020-05-24 04:38:24 +05:30
credentials: Credentials;
error: Error;
isLoading: boolean;
isSwitcherEnabled: boolean;
client: Client | null;
oauth: OAuthState | null;
scopes: Scopes;
2019-12-07 16:58:52 +05:30
}
2020-05-24 04:38:24 +05:30
const error: Reducer<State['error'], ErrorAction> = (state = null, { type, payload }) => {
if (type === 'auth:error') {
return payload;
}
2020-05-24 04:38:24 +05:30
return state;
};
2020-05-24 04:38:24 +05:30
const credentials: Reducer<State['credentials'], CredentialsAction> = (state = {}, { type, payload }) => {
if (type === 'auth:setCredentials') {
if (payload) {
return {
...payload,
};
}
2017-08-23 00:09:08 +05:30
2020-05-24 04:38:24 +05:30
return {};
}
2020-05-24 04:38:24 +05:30
return state;
};
2016-11-13 20:17:56 +05:30
2020-05-24 04:38:24 +05:30
const isSwitcherEnabled: Reducer<State['isSwitcherEnabled'], AccountSwitcherAction> = (
state = true,
{ type, payload },
) => {
if (type === 'auth:setAccountSwitcher') {
return payload;
}
2020-05-24 04:38:24 +05:30
return state;
};
2020-05-24 04:38:24 +05:30
const isLoading: Reducer<State['isLoading'], LoadingAction> = (state = false, { type, payload }) => {
if (type === 'set_loading_state') {
return payload;
}
2020-05-24 04:38:24 +05:30
return state;
};
2020-05-24 04:38:24 +05:30
const client: Reducer<State['client'], ClientAction> = (state = null, { type, payload }) => {
if (type === 'set_client') {
return payload;
}
2016-02-27 16:23:58 +05:30
2020-05-24 04:38:24 +05:30
return state;
};
const oauth: Reducer<State['oauth'], OAuthAction> = (state = null, action) => {
2020-05-24 04:38:24 +05:30
switch (action.type) {
case 'set_oauth':
return action.payload;
case 'set_oauth_result':
return {
...(state as OAuthState),
...action.payload,
};
case 'require_permissions_accept':
return {
...(state as OAuthState),
acceptRequired: true,
};
default:
return state;
}
};
2020-05-24 04:38:24 +05:30
const scopes: Reducer<State['scopes'], ScopesAction> = (state = [], { type, payload }) => {
if (type === 'set_scopes') {
return payload;
}
2020-05-24 04:38:24 +05:30
return state;
};
export default combineReducers<State>({
2020-05-24 04:38:24 +05:30
credentials,
error,
isLoading,
isSwitcherEnabled,
client,
oauth,
scopes,
});
2017-08-23 00:09:08 +05:30
2020-05-24 04:38:24 +05:30
export function getLogin(state: RootState | Pick<RootState, 'auth'>): string | null {
return state.auth.credentials.login || null;
2017-08-23 00:09:08 +05:30
}
export function getCredentials(state: RootState): Credentials {
2020-05-24 04:38:24 +05:30
return state.auth.credentials;
2017-08-23 00:09:08 +05:30
}