-
Notifications
You must be signed in to change notification settings - Fork 111
3. API
maxmantz edited this page May 26, 2016
·
26 revisions
This package contains the following components:
import createOidcMiddleware, { createUserManager } from 'redux-oidc';
const config = {
// userManager config object
}
const mgr = createUserManager(config);
// a custom shouldValidate function
const shouldValidate = (state, action) => {
if (action.type === 'DONT_VALIDATE') {
return false;
}
return true;
}
const oidcMiddleware = createOidcMiddleware(userManager, shouldValidate);
-
userManager
required: aUserManager
instance, -
shouldValidate
default() => true
: a function which gets the current state & action passed in. Returnstrue
when validation should occur orfalse
if it shouldn't, - triggerAuthFlow default
true
: iftrue
, the middleware triggers a redirect to the token service when the user is expired.
import { OidcProvider } from 'redux-oidc';
import { createStore } from 'redux';
import { createUserManager, Provider } from 'react-redux';
const manager = createUserManager();
const store = createStore(/* your reducers */);
ReactDOM.render(
<Provider store={store}>
<OidcProvider store={store} userManager={userManager} />
</Provider>,
document.getElementById('app')
);
-
store
required: the redux store, -
userManager
required: TheUserManager
instance.
There are reducers avaiable for both classic JS and apps using immutable.
import { reducer } from 'redux-oidc'; // classic JS
// OR
import { immutableReducer } from 'redux-oidc'; // immutable JS
import { combineReducers } from 'redux';
import { connect } from 'react-redux';
const rootReducer = combineReducers({
oidc: reducer,
// your other reducers...
});
The callback component manages the token callback from the identity provider. A custom successCallback
function is required to handle the redirect after the callback has been completed.
import React from 'react';
import { CallbackComponent } from 'redux-oidc';
class MyCallbackPage extends React.Component {
successCallback = (user) => {
// the url before redirection was triggered is passed into the user object
// and can be accessed like this
redirect(user.state.redirectUrl);
};
render() {
return <CallbackComponent dispatch={this.props.dispatch} successCallback={this.successCallback} />;
}
}
function mapDispatchToProps(dispatch) {
return {
dispatch
};
}
export default connect(null, mapDispatchToProps)(MyCallbackPage);
-
dispatch
required: The redux store'sdispatch
, -
successCallback
required: A function which is called after the token callback was successfully processed.
A function which returns a UserManager
instance.
´´´ import { createUserManager } from 'redux-oidc';
const mgr = createUserManager(); ´´´
-
config
optional: a config object for theUserManager
. See oidc-client-js's wiki for more information.
A function which is called when a silent renew is triggered. See silent renew for more information.
Usage example:
import { processSilentRenew } from 'redux-oidc';
processSilentRenew();