Skip to main content

Security context

The security context represents the profile of the logged in user.
It's usually retrieved from the backend during the authentication (via useLoginService) or by calling an API like /whoami (via useSecurityContext)

Oneki.js doesn't expect a specific format for the security context. You can put in what you want. The content of the security context is usually made of profile attributes like email, name, firstname, roles, ...

Example of a security context
{
sub: "de1ff3d2-0710-4f7a-9dd1-4aadb7d94b57",
email: "john.doe@example.com",
given_name: "John",
family_name: "Doe",
picture: "https://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50",
roles: ['admin', 'user']
}

useSecurityContext

The security context is stored in the global state of Redux with the key auth.securityContext and accessible everywhere with the useSecurityContext hook

const [securityContext, loading] = useSecurityContext<T>(selector, defaultValue);

// Examples:
// const [securityContext, loading] = useSecurityContext();
// const [email, loading] = useSecurityContext("email");
// const [picture, loading] = useSecurityContext("picture", "default_avatar.jpg");

Example

Displaying the security context
examples/cra-examples/src/auth/SecurePage.tsx
loading...

Parameters

Inputs

Mandatory parameters are marked with a *

ParameterTypeDescription
selectorstringIf the selector is:
  • not present: the whole security context is returned
  • a string: the string is a key in the security context. the result is this specific entry of the security context
defaultValueanyIn case the selector is a string, return this value if the key doesn't exist in the security context

Outputs

ParameterTypeDescription
securityContextanyReturn the whole security context or a specific entry based on the value of selector
loading*booleanloading is true if the application is currently fetching the security context from the backend

Advanced

Here is the logic to retrieve the security context: