Skip to main content

Secure component

Oneki.js provides an HOC to secure any component:

const SecureComponent = secure(Component, validator, options);

If the user is not yet logged in, this HOC redirects him to the login page
If the connected user doesn't have the right to display the page, it displays an error

Example

Page accessible only to authenticated users
The secure HOC is generally used to secure a page.
The example below shows how to secure a page so that only authenticated users can view it.
examples/cra-examples/src/auth/SecurePage.tsx
loading...
Page accessible only to admin users
The example below shows how the secure HOC can verify it the authenticated user has the required role

The profile of the user returned by the server looks like this:

{
"username": "John",
"roles": ["admin"]
}
examples/cra-examples/src/auth/UltraSecurePage.tsx
loading...

Parameters

Inputs

Mandatory parameters are marked with a *

ParameterTypeDescription
Component*ElementTypethe component to protect. Only authorized users will see it
validator(securityContext) => booleanBy default, a user is authorized if he's connected.

validator is a function to apply more complex logic like authorizing the user based on an attribute in the security context
optionsObjectObject with non mandatory attributes

Defaults: {}
options.
ErrorComponent
React ComponentThe component to display if the user is not authorized or not yet logged in.

By default
  • if he user is not yet logged in, he's redirected to the login page
  • if he's not authorized, a dummy component is displayed

Outputs

ParameterTypeDescription
SecureComponent*React ComponentThe component secured