React

This tutorial demonstrates how to add user login to an React application using BlitzWare.

circle-info

This tutorial is based on the example apparrow-up-right.

1) Configure BlitzWare

Get Your Application Keys

You will need some details about your application to communicate with BlitzWare. You can get these details from the Application Settings section in the BlitzWare dashboard.

You need the Client ID.

Configure Redirect URIs

A redirect URI is a URL in your application where BlitzWare redirects the user after they have authenticated. The redirect URI for your app must be added to the Redirect URIs list in your Application Settings under the Security tab. If this is not set, users will be unable to log in to the application and will get an error.

2) Install the BlitzWare React SDK

Run the following command within your project directory to install the BlitzWare React SDKarrow-up-right:

The SDK exposes methods and variables that help you integrate BlitzWare with your React application idiomatically using React Hooksarrow-up-right or Higher-Order Componentsarrow-up-right.

Configure the BlitzWareAuthProvider component

Under the hood, the BlitzWare React SDK uses React Contextarrow-up-right to manage the authentication state of your users. One way to integrate BlitzWare with your React app is to wrap your root component with an BlitzWareAuthProvider that you can import from the SDK.

The BlitzWareAuthProvider component takes the properties clientId and redirectUri ; the values of these properties correspond to the Client ID and Redirect URI values that you can find under Settings in the Single Page Web Application (SPA) that you registered with BlitzWare.

3) Add Login to your application

The BlitzWare React SDK gives you tools to quickly implement user authentication in your React application, such as creating a login button using the login() method from the useLogin() hook. Executing login() redirects your users to the BlitzWare Universal Login Page, where BlitzWare can authenticate them. Upon successful authentication, BlitzWare will redirect your users back to your application.

4) Show User profile info and add Logout to your application

The BlitzWare React SDK helps you retrieve the profile information associated with logged-in users quickly in whatever component you need, such as their name or profile picture, to personalize the user interface. The profile information is available through the user property exposed by the useAuthUser() hook.

You also need a way to log out. You can create a logout button using the logout() method from the useLogout() hook. Executing logout() clears the session and makes sure that the user can not access protected resources.

Take this Dashboard component as an example of how to use it:

The user property contains sensitive information and artifacts related to the user's identity. As such, its availability depends on the user's authentication status. To prevent any render errors, use the isAuthenticated property from useIsAuthenticated() to check if BlitzWare has authenticated the user before React renders any component that consumes the user property. Ensure that the SDK has completed loading before accessing the isAuthenticated property, by checking that isLoading is false.

5) Add Protection to your routes

The BlitzWare React SDK exports the ProtectedRoute component used to protect routes by preventing unauthorized users from accessing certain pages or components within your application. Here is how you can do it:

That's it!

Last updated