hatCluster – The HAT's cluster for the login. In this case we are using '.hubat.net' for testing purposes only. For production you'd use a real non-dev-sandbox PDA
hatApiVersion – The API version of the HAT
secure – If you want to run the HAT locally, you have to modify this field to 'false'
For better user experience, it's good practice to validate the user's input and navigate them to their PDA to Login only if their username is valid and exists. Otherwise we'll display an error message to inform the user about the issue. You can use the HAT-JS to check if a PDA with this username exists.
Let's create the redirectValidUser method to redirect an existing user. We can use the hat-js to check if the user exists:
// src/features/Login/LoginPage.jsimport React, { useState } from'react';import { HatClient } from'@dataswift/hat-js';import { isHatName } from"../../utils/validations";import { appConfig } from"../../app.config";functionLoginPage() {const [username,setUsername] =useState('');const [errorMsg,setErrorMsg] =useState('');consterrorMessages= { usernameNotValid:'Sorry, this username is not valid', usernameNotRecognised:'Sorry, this username is not recognised' };constredirectValidUser=async (username) => {try {consthat=newHatClient({});consthatDomain= username +appConfig.hatCluster;// Use the hat-js's helper method to check if the user existsconstres=awaithat.auth().isDomainRegistered(hatDomain);// if the res is true, navigate the user to their PDAif (res) {consthatUrl=`https://${hatDomain}`;constredirectUrl=`http://${window.location.host}/authentication`;constfallback=`http://${window.location.host}/authentication`;constapplicationId=appConfig.applicationId;window.location.href =hat.auth().generateHatLoginUrl(hatUrl, applicationId, redirectUrl, fallback); } else {// Display an error message when the username is not recognisedsetErrorMsg(errorMessages.usernameNotRecognised); } } catch (e) {console.log(e); } };constvalidateLoginDetails= () => {if (isHatName(username)) {// Use the new method when the username is validredirectValidUser(username); } else {setErrorMsg(errorMessages.usernameNotValid); } };return ( <form> . . </form> );}
Upon successful login, the HAT will verify if the application has been granted all the permissions it needs. If not, the HAT user will need to provide these permissions for the app to access their HAT.
Lastly, the user will be redirected back to our application with the authentication token attached. This should be stored for subsequent API calls:
To collect the token parameter, we will need to create a new component in the Login feature
Create the authentication handler
In this example we will store the token value in the Session Storage. Upon successful login we will navigate the user back to the HomePage where we will need to add a different screen to display to the user when they are authenticated. In case of error, we will redirect the user back to the Login page.