Frontend
Change to the 'frontend/react' folder.
Copy the '.env.example' file to a new file called '.env.development' and paste the following code into it:
These are react variables we'll use later.
Next, you'll need to install the cerberus library. Run:
Now, in the 'src/index.js' file, change the code to the following:
This sets up the cerberus context for the rest of the app.
In the 'src/hooks/useFetch.js' file, change the code to the following:
This will ensure the cerberus access token and refresh token is added to every request.
But we haven't yet populated the context, so the tokens are empty.
To do this, change the 'src/context/AuthContext.js' file:
Now, every time the user logs in, the cerberusTokenPair passed from the backend is stored in the AuthContext, and passed along in the headers with every request.
At this stage we have a fully working and permissioned app, and you can run it.
However, as a bonus feature, the cerberus react library also includes a websocket link with the cerberus API for realtime permission checks on the frontend.
This allows the frontend to pre-emptively respond to the permissions of a user and only show the elements they have access to.
To this end, there are two components available
an AccessGuard component that shows child elements if the user has the required permissions
a useAccess hook that returns a boolean for the required permissions
Next, we'll implement just that.
Last updated