In the app I’m working on we use Auth0 to handle our login system and then authorize sites over to WordPress from our main dashboard. The problem is that when calling Auth0 for the user roles we have setup we don’t always get a response as soon as we need it and then our Laravel app throws errors.
Since we don’t need the roles on most pages I decided not to go with custom middleware and instead use the caching API as shown below.
protected static function getAuth0Roles()
{
$roles = Cache::remember('auth0_roles', now()->addMinutes(30), function () {
$roles = app('auth0')->management()->roles()->getAll();
return Auth0::json($roles);
});
return $roles;
}
Now my roles are cached for 30 minutes, and I could likely increase that number greatly since the number of roles we have defined is unlikely to change for months at a time.
This could be modified to cache other parts of the Auth0 API by changing the app('auth0')
call to the portion of the API you need to interact with.