auth
This package offers 2 different parts, a Auth
(React Functional Component) and a Client
(Util Class). The Auth
component places a authentication wrapper around the whole app which exposes a user state to all components of the app via the Context api. The Client
could be used to wrapper you API call and return to you an object or throw an error if there is any.
#
Installation#
Props#
AuthName | Type | Required | Default |
---|---|---|---|
children | ReactNode | yes | undefined |
#
Default state#
Interfaces#
Examples#
AuthYou just need to wrap your component or entire application with the <Auth>
tag to use the information from this component in any place through the context API.
Following you can see an example of how to use the context information in your application. Remember that the context API is a react hook and you can use it only on function components.
#
Auth ClientThis client wraps API requests with the user's auth token and transforms the response. If the response is a JSON string, it will return the object generated from the JSON.
#
Wrapping your API Requests#
Usage#
Error handlingThe client transforms errors, converting them to a FailedAuthClientResponseError object.
The error seen in .catch(e)
is the message from the server, which can also be accessed using e.message
. However, some APIs have more information in the response body. You can check the entire response from the server using e.response
, a Response object.