Module

useScopeCheck

Custom hook for checking whether an OIDC user has the provided scopes.

The hook can accept zero or more strings that represent the requested scopes, and returns a set of booleans representing the results.

The three returned booleans are: loggedIn, anyScope, allScope

  • loggedIn: Whether there is a user currently logged in (if no user is present, all return values are false)
  • anyScope: Whether the logged in user has ONE OR MORE of the provided scopes
  • allScope: Whether the logged in user has ALL of the provided scopes

The hook uses the ApiManager's templating engine to match provided scopes to the current environment. This means you can provide generic scopes (e.g. an API interface scope) and it will templated to the correct environment equivalent.

Hook Usage

Checking if a user has a specific scope

const {loggedIn, anyScope, allScope} = useScopeCheck('scope1')

A single scope string can be checked against the scopes of the logged in user. In this use case anyScope and allScope are equivalent, a single scope will be true for both.

Checking if a user has multiple scopes

const {loggedIn, anyScope, allScope} = useScopeCheck(['scope1','scope2'])

Multiple scope strings can be checked at once. In this use case anyScope will be true if the user has at least one and allScope will only be true if the user has all the provided scopes.

Checking if a user is logged in

const {loggedIn} = useScopeCheck()

The hook can also be called without any scopes being provided. This can be used to check if a user is currently logged in or not.

Parameters:
Name Type Attributes Description
scopes string | Array.<string> <optional>

The scope(s) to check against the current user

View Source tools/hooks/useScopeCheck.ts, line 5

The results of checking the scope(s)