@os1-platform/aaa-web v1.0.3
React AAA SDK for WEB
Introduction
Built on top of react, oidc-react and typescipt. This sdk can be used for authentication, maintaining access token, fetching user info and appending headers to the REST API calls.
Installation and Usage
Peer dependencies:
{
"axios": ">=0.24.2",
"react": ">=17.0.2"
}Install @os1-platform/aaa-web into your project.
npm install @os1-platform/aaa-webUse
initCASAPI of the sdk to create auth instance and fetch AuthProvider component.import { initCAS } from '@os1-platform/aaa-web'; const AuthProvider = initCAS( 'CLIENTID', // clientId, '/fms/success', // success pathname (https://abc.fxtrt.io/fms/success) 'web', // device type '/fms/failure', //logoutRedirectPath 'TenantIdForDevelopmentMode' //static tenantId for development mode (accepted if the sub-domain is developer or developer2)(optional field) );Wrap your application in this single AuthProvider component. For example:
ReactDOM.render( <React.StrictMode> <BrowserRouter basename="/fms"> <AuthProvider> <Router /> </AuthProvider> </BrowserRouter> </React.StrictMode>, document.getElementById('root') );orReactDOM.render( <AuthProvider> <App /> </AuthProvider>, document.getElementById('root') );Pass
loadercomponent to the AuthProvider to override the default loader.import Loader from 'your-loader-component'; <AuthProvider loader={<Loader />}>{children}</AuthProvider>;Use
loginWithRedirectmethod to initiate login.import { loginWithRedirect } from '@os1-platform/aaa-web'; <button onClick={() => loginWithRedirect()}>Login</button>;Use
isAuthenticatedmethod to put a check on private pages:
import { isAuthenticated } from '@os1-platform/aaa-web';
const isAuth = isAuthenticated();Use
getAccessTokenSilentlymethod, to fetch access token.import { getAccessTokenSilently } from '@os1-platform/aaa-web'; const token = await getAccessTokenSilently();Use
getUserInfomethod, to fetch user info.import { getUserInfo } from '@os1-platform/aaa-web'; const userInfo = await getUserInfo();Use
HttpClientAPI to create a client for network requests.import { HttpClient as client } from '@os1-platform/aaa-web'; class NetworkClient { public readonly instance: any; constructor() { this.instance = client.createClient({ baseURL: `https://abc.preprod.fxtrt.io/core/api/v1/aaa`, }); } }Following headers are automatically configured to requests originating from the
NetworkClientadding Access token(x-coreos-access) or Tenant id(x-coreos-tid) or User info(x-coreos-userinfo) or Auth token(x-coreos-auth) headers to the actual request.
withAccesswithTidwithUserInfowithAuth
Note:
- By default all these headers are true, pass value against these headers as
falseto remove from request. - Access token is verified and regenerated (if expired), every time an api request is made.
x-coreos-userinfocontains the userId.x-coreos-authcontains the id_token.
import NetworkClient from './networkClient';
const handleClick = () => {
const client1 = new Client();
const reqHeaders: any = {
withAccess: false,
withTid: false,
withUserInfo: false,
withAuth: false,
};
client1.instance
.get('/users', {
headers: {
'X-COREOS-REQUEST-ID': '1d8cb10a-02c0-4fb9-b5e3-d4d432717c49',
...reqHeaders,
},
})
.catch((err) => {
console.log(err);
// error handling code here
});
};- Use
logoutmethod, to implement logout functionality.
import { logout } from '@os1-platform/aaa-web';
await logout();