Update redux-auth-wrapper to v2 (#18236)

* Update redux-auth-wrapper to version 2

* Fix linter errors

* Add redirectAction optiona to router decorator
This commit is contained in:
Karol Janyst
2017-07-20 01:41:12 -07:00
committed by Wesley Wigham
parent 2005e7e6ee
commit 76241738dc
11 changed files with 371 additions and 48 deletions
+15
View File
@@ -0,0 +1,15 @@
import { ReactType } from "react";
import { AuthConfig, AuthWrapperDecorator } from "redux-auth-wrapper";
export interface InjectedAuthProps {
isAuthenticated: boolean;
isAuthenticating: boolean;
}
export interface AuthWrapperConfig extends AuthConfig {
FailureComponent?: ReactType;
}
export function authWrapper<OwnProps = {}>(
config: AuthWrapperConfig
): AuthWrapperDecorator<OwnProps & InjectedAuthProps>;
+9
View File
@@ -0,0 +1,9 @@
import { ReactType } from "react";
import { AuthBaseConfig, AuthWrapperDecorator } from "redux-auth-wrapper";
import { AuthWrapperConfig, InjectedAuthProps } from "redux-auth-wrapper/authWrapper";
export type ConnectedAuthWrapperConfig<OwnProps = {}, State = {}> = AuthWrapperConfig & AuthBaseConfig<OwnProps, State>;
export function connectedAuthWrapper<OwnProps = {}, State = {}>(
config: ConnectedAuthWrapperConfig<OwnProps, State>
): AuthWrapperDecorator<OwnProps & InjectedAuthProps>;
+55
View File
@@ -0,0 +1,55 @@
import { Action, Store } from "redux";
import { Location, LocationDescriptorObject } from "history";
import { AuthBaseConfig, AuthConfig, AuthWrapperDecorator, StateSelector } from "redux-auth-wrapper";
import { InjectedAuthProps } from "redux-auth-wrapper/authWrapper";
import { ConnectedAuthWrapperConfig } from "redux-auth-wrapper/connectedAuthWrapper";
export interface InjectedAuthReduxProps extends InjectedAuthProps {
redirectPath: string;
}
export interface InjectedAuthRouterProps<Redirect = (...args: any[]) => Action> extends InjectedAuthReduxProps {
redirect: Redirect;
}
export interface ConnectedRouterRedirectConfig<OwnProps = {}, State = {}> extends AuthBaseConfig<OwnProps, State> {
redirectPath: string | StateSelector<State, OwnProps, string>;
redirectAction?(location: Location): Action;
allowRedirectBack?: boolean | StateSelector<State, OwnProps, boolean>;
redirectQueryParamName?: string;
}
export function connectedRouterRedirect<OwnProps = {}, State = {}>(
config: ConnectedRouterRedirectConfig<OwnProps, State>
): AuthWrapperDecorator<OwnProps & InjectedAuthRouterProps>;
export type ConnectedReduxRedirectConfig<OwnProps = {}, State = {}> = ConnectedRouterRedirectConfig<OwnProps, State>;
export function connectedReduxRedirect<OwnProps = {}, State = {}>(
config: ConnectedReduxRedirectConfig<OwnProps, State>
): AuthWrapperDecorator<OwnProps & InjectedAuthReduxProps>;
export type StateMutateSelector<State, R> = (state: State, nextState: State) => R;
export interface CreateOnEnterConfig<State> extends AuthConfig {
redirectPath: string | StateMutateSelector<State, string>;
authenticatedSelector: StateMutateSelector<State, boolean>;
authenticatingSelector?: StateMutateSelector<State, boolean>;
allowRedirectBack?: boolean | StateMutateSelector<State, boolean>;
redirectQueryParamName?: string;
}
export function createOnEnter<State = {}>(config: CreateOnEnterConfig<State>):
(store: Store<any>, nextState: State, redirect: (location: LocationDescriptorObject) => void) => void;
export interface LocationHelperConfig<Props> {
redirectQueryParamName?: string;
locationSelector?(props: Props): LocationDescriptorObject;
}
export interface LocationHelper<Props> {
getRedirectQueryParam(props: Props): string;
createRedirectLoc(props: Props, redirectPath: string): LocationDescriptorObject;
}
export function locationHelperBuilder<Props = {}>(config: LocationHelperConfig<Props>): LocationHelper<Props>;
+25
View File
@@ -0,0 +1,25 @@
import { Action } from "redux";
import { Location } from "history";
import { AuthWrapperDecorator } from "redux-auth-wrapper";
import {
ConnectedRouterRedirectConfig,
InjectedAuthReduxProps
} from "redux-auth-wrapper/history3/redirect";
export {
connectedRouterRedirect,
ConnectedRouterRedirectConfig,
InjectedAuthReduxProps,
InjectedAuthRouterProps,
locationHelperBuilder,
LocationHelper,
LocationHelperConfig
} from "redux-auth-wrapper/history3/redirect";
export interface ConnectedReduxRedirectConfig<OwnProps = {}, State = {}> extends ConnectedRouterRedirectConfig<OwnProps, State> {
redirectAction(location: Location): Action;
}
export function connectedReduxRedirect<OwnProps = {}, State = {}>(
config: ConnectedReduxRedirectConfig<OwnProps, State>
): AuthWrapperDecorator<OwnProps & InjectedAuthReduxProps>;
+10 -22
View File
@@ -1,33 +1,21 @@
// Type definitions for redux-auth-wrapper 1.0
// Type definitions for redux-auth-wrapper 2.0
// Project: https://github.com/mjrussell/redux-auth-wrapper
// Definitions by: Karol Janyst <https://github.com/LKay>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import { ComponentClass, StatelessComponent, ReactType } from "react";
import { Action } from "redux";
import { Location } from "history";
import { ComponentClass, StatelessComponent, ComponentType, ReactType } from "react";
export type ComponentConstructor<P> = ComponentClass<P> | StatelessComponent<P>;
export type StateSelector<State, OwnProps, R> = (state: State, props: OwnProps) => R;
export interface InjectedProps<AuthData> {
authData?: AuthData;
}
export type AuthWrapperDecorator<Props> = (component: ComponentType<Props>) => ComponentClass<Props>;
export interface AuthWrapperConfig<State, Props, AuthData> {
allowRedirectBack?: boolean | ((location: Location, redirectPath: string) => boolean);
authenticatingSelector?(state: State, ownProps?: Props): boolean;
authSelector(state: State, ownProps?: Props): AuthData;
FailureComponent?: ReactType;
failureRedirectPath?: string | ((state: State, ownProps?: Props) => string);
LoadingComponent?: ReactType;
redirectQueryParamName?: string;
export interface AuthConfig {
AuthenticatingComponent?: ReactType;
wrapperDisplayName?: string;
predicate?(authData: AuthData): boolean;
propMapper?(ownProps: Props): InjectedProps<AuthData> & Props;
redirectAction?(...args: any[]): Action;
}
export type AuthDecorator<Props> = (component: ComponentConstructor<Props>) => ComponentClass<Props>;
export function UserAuthWrapper<State, Props, AuthData>(config: AuthWrapperConfig<State, Props, AuthData>): AuthDecorator<Props>;
export interface AuthBaseConfig<OwnProps = {}, State = {}> extends AuthConfig {
authenticatedSelector: StateSelector<State, OwnProps, boolean>;
authenticatingSelector?: StateSelector<State, OwnProps, boolean>;
}
@@ -1,28 +1,168 @@
import * as React from "react";
import { StatelessComponent } from "react";
import { UserAuthWrapper } from "redux-auth-wrapper";
import { Component, StatelessComponent } from "react";
import { createStore } from "redux";
import { push } from "react-router-redux";
import { LocationDescriptorObject } from "history";
const Auth = UserAuthWrapper<any, any, any>({
import {
connectedRouterRedirect as H3connectedRouterRedirect,
connectedReduxRedirect as H3connectedReduxRedirect,
createOnEnter,
locationHelperBuilder as H3locationHelperBuilder
} from "redux-auth-wrapper/history3/redirect";
import {
connectedRouterRedirect,
connectedReduxRedirect,
locationHelperBuilder
} from "redux-auth-wrapper/history4/redirect";
import { authWrapper } from "redux-auth-wrapper/authWrapper";
import { connectedAuthWrapper } from "redux-auth-wrapper/connectedAuthWrapper";
interface TestReduxState {
foo: string;
}
interface TestComponentProps {
foo: string;
}
const stringSelector = (state: TestReduxState, ownProps: TestComponentProps) => "fizz";
const boolSelector = (state: TestReduxState, ownProps: TestComponentProps) => true;
const stringStateSelector = (state: TestReduxState, nextState: TestReduxState) => "buzz";
const boolStateSelector = (state: TestReduxState, nextState: TestReduxState) => true;
/* History 3 */
H3connectedRouterRedirect({
redirectPath: "/login",
authenticatedSelector: boolSelector,
authenticatingSelector: boolSelector,
AuthenticatingComponent: "div",
wrapperDisplayName: "Auth",
allowRedirectBack: true,
authenticatingSelector(state: any) {
return state.auth.loading;
},
authSelector(state: any) {
return state.auth;
},
FailureComponent: () => (<div />),
failureRedirectPath: "/401",
LoadingComponent: () => (<div />),
redirectAction: () => ({ type : "redirect" }),
redirectQueryParamName: "next",
predicate(authData: any) {
return authData.authorized;
},
wrapperDisplayName: "TestAuth"
redirectQueryParamName: "redirect"
})(
({
foo,
isAuthenticated,
isAuthenticating,
redirect,
redirectPath
}) => null
);
H3connectedReduxRedirect({
redirectPath: stringSelector,
authenticatedSelector: boolSelector,
authenticatingSelector: boolSelector,
AuthenticatingComponent: "div",
wrapperDisplayName: "Auth",
allowRedirectBack: boolSelector,
redirectQueryParamName: "redirect"
})(
({
foo,
isAuthenticated,
isAuthenticating,
redirectPath
}) => null
);
const store = createStore(() => ({}));
const enter = createOnEnter({
redirectPath: "/login",
authenticatedSelector: boolStateSelector,
authenticatingSelector: boolStateSelector,
allowRedirectBack: true,
redirectQueryParamName: "redirect"
});
enter(store, { foo : "bar" }, (location: LocationDescriptorObject) => {});
export const TestAuthComponent: StatelessComponent = () => {
return (<div />);
};
const H3helper = H3locationHelperBuilder<TestComponentProps>({
redirectQueryParamName: "redirect",
locationSelector: ({ foo }) => ({ pathname : "foo" })
});
H3helper.getRedirectQueryParam({ foo : "bar" });
H3helper.createRedirectLoc({ foo : "bar" }, "redirect");
const TestAuth = Auth(TestAuthComponent);
/* History 4 */
connectedRouterRedirect({
redirectPath: "/login",
authenticatedSelector: boolSelector,
authenticatingSelector: boolSelector,
AuthenticatingComponent: (props: any) => null,
wrapperDisplayName: "Auth",
allowRedirectBack: true,
redirectQueryParamName: "redirect"
})(
({
foo,
isAuthenticated,
isAuthenticating,
redirect,
redirectPath
}) => null
);
class Loading extends Component {
render() {
return null;
}
}
connectedReduxRedirect({
redirectPath: "/login",
redirectAction: push,
authenticatedSelector: boolSelector,
authenticatingSelector: boolSelector,
AuthenticatingComponent: Loading,
wrapperDisplayName: "Auth",
allowRedirectBack: true,
redirectQueryParamName: "redirect"
})(
({
foo,
isAuthenticated,
isAuthenticating,
redirectPath
}) => null
);
const helper = locationHelperBuilder<TestComponentProps>({
redirectQueryParamName: "redirect",
locationSelector: ({ foo }) => ({ pathname : "foo" })
});
helper.getRedirectQueryParam({ foo : "bar" });
helper.createRedirectLoc({ foo : "bar" }, "redirect");
/* Other Wrappers */
authWrapper<TestComponentProps>({
AuthenticatingComponent: "div",
FailureComponent: "div",
wrapperDisplayName: "Auth"
})(
({
foo,
isAuthenticated,
isAuthenticating
}) => null
);
connectedAuthWrapper({
authenticatedSelector: boolSelector,
authenticatingSelector: boolSelector,
AuthenticatingComponent: "div",
FailureComponent: "div",
wrapperDisplayName: "Auth"
})(
({
foo,
isAuthenticated,
isAuthenticating
}) => null
);
+4 -4
View File
@@ -10,16 +10,16 @@
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": ["../"],
"paths": {
"history": ["history/v3"],
"history/*": ["history/v3/*"]
},
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"jsx": "react"
},
"files": [
"history3/redirect.d.ts",
"history4/redirect.d.ts",
"authWrapper.d.ts",
"connectedAuthWrapper.d.ts",
"index.d.ts",
"redux-auth-wrapper-tests.tsx"
]
+33
View File
@@ -0,0 +1,33 @@
// Type definitions for redux-auth-wrapper 1.0
// Project: https://github.com/mjrussell/redux-auth-wrapper
// Definitions by: Karol Janyst <https://github.com/LKay>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import { ComponentClass, StatelessComponent, ReactType } from "react";
import { Action } from "redux";
import { Location } from "history";
export type ComponentConstructor<P> = ComponentClass<P> | StatelessComponent<P>;
export interface InjectedProps<AuthData> {
authData?: AuthData;
}
export interface AuthWrapperConfig<State, Props, AuthData> {
allowRedirectBack?: boolean | ((location: Location, redirectPath: string) => boolean);
authenticatingSelector?(state: State, ownProps?: Props): boolean;
authSelector(state: State, ownProps?: Props): AuthData;
FailureComponent?: ReactType;
failureRedirectPath?: string | ((state: State, ownProps?: Props) => string);
LoadingComponent?: ReactType;
redirectQueryParamName?: string;
wrapperDisplayName?: string;
predicate?(authData: AuthData): boolean;
propMapper?(ownProps: Props): InjectedProps<AuthData> & Props;
redirectAction?(...args: any[]): Action;
}
export type AuthDecorator<Props> = (component: ComponentConstructor<Props>) => ComponentClass<Props>;
export function UserAuthWrapper<State, Props, AuthData>(config: AuthWrapperConfig<State, Props, AuthData>): AuthDecorator<Props>;
@@ -0,0 +1,28 @@
import * as React from "react";
import { StatelessComponent } from "react";
import { UserAuthWrapper } from "redux-auth-wrapper";
const Auth = UserAuthWrapper<any, any, any>({
allowRedirectBack: true,
authenticatingSelector(state: any) {
return state.auth.loading;
},
authSelector(state: any) {
return state.auth;
},
FailureComponent: () => (<div />),
failureRedirectPath: "/401",
LoadingComponent: () => (<div />),
redirectAction: () => ({ type : "redirect" }),
redirectQueryParamName: "next",
predicate(authData: any) {
return authData.authorized;
},
wrapperDisplayName: "TestAuth"
});
export const TestAuthComponent: StatelessComponent = () => {
return (<div />);
};
const TestAuth = Auth(TestAuthComponent);
+29
View File
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../../",
"typeRoots": [
"../../"
],
"types": [],
"paths": {
"history": ["history/v3"],
"history/*": ["history/v3/*"],
"redux-auth-wrapper": ["redux-auth-wrapper/v1"]
},
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"jsx": "react"
},
"files": [
"index.d.ts",
"redux-auth-wrapper-tests.tsx"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }