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
- Login
- 👁 Preview
examples/cra-examples/src/auth/SecurePage.tsx
loading...
Parameters
Inputs
Mandatory parameters are marked with a *
Parameter | Type | Description |
---|---|---|
selector | string | If the selector is:
|
defaultValue | any | In case the selector is a string, return this value if the key doesn't exist in the security context |
Outputs
Parameter | Type | Description |
---|---|---|
securityContext | any | Return the whole security context or a specific entry based on the value of selector |
loading* | boolean | loading is true if the application is currently fetching the security context from the backend |
Advanced
Here is the logic to retrieve the security context
: