The index.html file that contains the HTML code for the frontend application
The main.css file that contains the CSS code for the frontend application
The main.js file that contains the JavaScript code
The client-package.json configuration file
We will be coding index.html and main.js. We will also be making a change in the client-package.json to ensure that the homepage of the application is the authenticated page.
We will discuss the code at the end of this section.
Note: Please go through the code provided in this section to make sure you fully understand it.
Copy the code below and paste it into the respective files located in the client/directory of your project using an IDE and save the files.
function showProfile() {
debugger;
//The catalyst.auth.isUserAuthenticated() method allows only authenticated users, i.e., the users who are logged in, to access the pages
//You can load this method in the body of your page to allow only authenticated users to access a particular page
catalyst.auth.isUserAuthenticated().then(result => {
//If the user is logged in, these contents of the page will be displayed to the user
document.body.style.visibility = "visible";
const first_name = "First Name: " + result.content.first_name;
document.getElementById("fname").innerHTML = first_name;
const last_name = "Last Name: " + result.content.last_name;
document.getElementById("lname").innerHTML = last_name;
const mailid = "Email Address: " + result.content.email_id;
document.getElementById("mailid").innerHTML = mailid;
const tzone = "Time Zone: " + result.content.time_zone;
document.getElementById("tzone").innerHTML = tzone;
const created_time = " Joined On: " + result.content.created_time;
document.getElementById("ctime").innerHTML = created_time;
}).catch(err => {
//If the user is not logged in, this will be displayed to the user and they will be redirected to the login page
document.body.style.visibility = "visible";
document.body.innerHTML = 'You are not logged in. Please log in to continue. Redirecting you to the login page..';
setTimeout(function () {
window.location.href = "/__catalyst/auth/login";
}, 5000);
});
}
function logout() {
//The signOut method is used to sign the user out of the application
const redirectURL = location.protocol +"//"+ location.hostname + “/__catalyst/auth/login”;
catalyst.auth.signOut(redirectURL);
}
Note: The home page URL can be copied from the Access URL generated by Catalyst in the Authentication Types section of the console.
The client directory is now configured.
Let’s go over how the function and the client code works:
The end-user will first encounter the Login Page you have configured and styled in the console.
To create an account, they will click the Sign Up option and be directed to the Sign Up page.
Note: End-users can either sign up to your application using their Social Logins or use the conventional method of signing up to the application using their email ID.
Once the information is inputted and they click Sign Up, or if they attempt to sign up using a Social Login of their choice, their details will be provided as a JSON input to the Custom User Validation function (authorization_portal_function/main.py).
The function will only authenticate users if the condition you have coded is satisfied. For this tutorial, the users will only be authenticated if they signed up to the application using a Zylker registered email address.
Upon successful authentication, an Email Verification email will be sent to the account they used to sign up to the application.
Note: Because we are using Hosted Login, a native Catalyst authentication type, no additional logic to trigger the appropriate emails is required. It will be handled by Catalyst automatically.
Once the user verifies their email address by clicking the link provided in the email, they will be signed up to the application. Their details can be viewed in the User Management section of the Authentication component.
The end-user can now log in to the application using their authenticated credentials.
The end-user can also reset their password by clicking the Forgot Password option. In this event, the Password Reset email will be sent to their registered email. Following the steps mentioned in the email, they can easily reset their credentials.
The root directory of the client contains a client-package.json file, which is a configuration file defining the name, version, default home page, and redirect page of the client component.
The zylkertechnologies client directory contains two subfolders, as per the default project structure of a React app:
The public folder is generally used to hold files that can be openly accessed by browsers through public URLs, such as icon files of the web app, the index.html file, and the 404.html file.
The src folder contains the application’s source files that will be included in the build folder when we compile the React app.
The client directory also contains the package.json dependency file and a hidden .gitignore file.
The files present in the public folder include:
Native React files such as setup Tests.js, index.js, reportWebVitals.js, logo.svg, and App.test.js.
index.html - Contains Catalyst web sdk that allows you to access Catalyst components.
We will be creating an additional 404.html file that will contain the error handling elements.
The files present in the src folder include:
Native React files such as setupTests.js, reportWebVitals.js, logo.svg, and App.test.js.
App.js - Contains the hash routes to your application
App.css - Contains the overall styling elements of your application
index.js - The entry point of the react application.
index.css - Contains the styling of the elements present in the index.js file.
You will be creating the following files in the src folder:
Button.css - Contains the styling elements for the Logout button that will be present in the application.
Button.js - Contains the React component for the Logout present in the application.
Layout.js - Contains the React component for the application’s logic
LoginPage.js - Contains the React component for the login pages you generated in the Catalyst console.
UserProfile.css - Contains the styling elements for the user information page that will be displayed once you log in to the application.
UserProfile.js - Contains the React component to render the user details after you login.
We will be coding in the index.html, 404.html, App.js, App.css, index.css, index.js, Button.css, Button.js, Layout.js, LoginPage.js, UserProfile.css, and UserProfile.js files. We will also be making a change in the client-package.json. to ensure that the homepage of the application is the authenticated page.
Note: You can delete the native React files as they not required for building this application.
Install additional React packages
The react-router-dom package needs to be installed in the AuthorizationPortal/zylkertechnologies client directory using the CLI command given below:
copy
$
npm install react-router-dom
This command will install the react-router-dom module, save the dependencies, and allow us to perform the required hash routing in our application.
This is the client directory after you have created the required files and deleted the unnecessary files.
Note: Please go through the all code given in this section to ensure you fully understand it.
Copy the code given below and paste it in index.html and 404.html, respectively, located in the client directory (zylkertechnologies/public/) using an IDE and save the file.
index.html
copy
<!DOCTYPE html>
<html lang=“en”>
<head>
<meta charset=“utf-8” />
<meta name=“viewport” content=“width=device-width, initial-scale=1” />
<meta name=“theme-color” content="#000000" />
<meta
name=“description”
content=“Web site created using create-react-app”
/>
<link rel=“manifest” href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
<script src=“https://static.zohocdn.com/catalyst/sdk/js/4.0.0/catalystWebSDK.js"> </script>
<script src=”/__catalyst/sdk/init.js"></script>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id=“root”></div>
</body>
</html>
View more
404.html
copy
<!DOCTYPE html>
<html lang=“en”>
<head>
<h2>You are viewing this 404 page since you tried to access a page which does not exist.</h2>
</head>
</html>
View more
Copy the code given below and paste it in App.css, App.js, index.js, index.css, Button.css, Button.js, Layout.js, LoginPage.js, UserProfile.css, and UserProfile.js, respectively, located in client directory (zylkertechnologies/src/) using an IDE and save the file.
/* Add a background color when the inputs get focus */
input[type=“text”]:focus,
input[type=“password”]:focus {
background-color: #ddd;
outline: none;
}
/* Set a style for all buttons */
/* Float cancel and signup buttons and add an equal width */
.seperatebtn {
width: 10%;
padding: 8px;
border-radius: 8px;
}
/* The Modal (background) /
.modal {
display: none;
/ Hidden by default /
position: fixed;
/ Stay in place /
z-index: 1;
/ Sit on top /
left: 0;
top: 0;
width: 100%;
/ Full width /
height: 100%;
/ Full height /
overflow: auto;
/ Enable scroll if needed */
background-color: #474e5d;
}
/* Modal Content/Box /
.modal-content {
background-color: #c0c0c0;
margin: 5% auto 15% auto;
/ 5% from the top, 15% from the bottom and centered /
width: 25%;
/ Could be more or less, depending on screen size */
}
Note: The home page URL can be copied from the Access URL generated by Catalyst in the Authentication Types section of the console.
The client directory is now configured.
Let’s go over the working of the function and the client code:
The end-user will first encounter the Login Page that you configured and styled in the console.
To create an account, they will click the Sign Up option and be directed to the Sign Up page.
Note: End-users can either sign up to your application using their Social Logins or use the conventional method of signing up to the application using their email ID.
Once the information is inputted and they click Sign Up, or if they attempt to sign up using a Social Login of their choice, their details will be provided as a JSON input to the Custom User Validation function (authorization_portal_function/main.py).
The function will only authenticate users if the condition you have coded is satisfied. For this tutorial, the users will only be authenticated if they signed up to the application using a Zylker registered email address.
Upon successful authentication, an Email Verification email will be sent to the account they used to sign up to the application.
Note: Because we are using Hosted Login, a native Catalyst authentication type, no additional logic to trigger the appropriate emails is required. It will be handled by Catalyst automatically.
Once the user verifies their email address by clicking the link provided in the email, they will be signed up to the application. Their details can be viewed in the User Management section of the Authentication component.
The end-user can now log in to the application using their authenticated credentials.
The end-user can also reset their password by clicking the Forgot Password option. In this event, the Password Reset email will be sent to their registered email. Following the steps mentioned in the email, they can easily reset their credentials.
The root directory of the client contains a client-package.json file, which is a configuration file defining the name, version, default home page, and redirect page of the client component. We will be modifying this file after we have coded the client component.
Native Angular files such as angular.json, tsconfig.app.json, tsconfig.json, and tsconfig.spec.json files.
The root directory of the client also contains the package.json dependency file, and a .gitignorefile.
The src folder contains the following files and directories as per the default project structure of the Angular app:
Native Angular files such as favicon.ico, main.ts, polyfills.ts, and test.ts files along with the assets, and environment directories.
index.html: The default entry point of the application.
style.css: Will contain any styling elements required by the overall application.
The files present in the src/app/ directory are:
Native Angular files such as app.component.css and app.component.spec.ts.
app.component.html: Contains the HTML component that renders the routing that happens in the application.
app.component.ts: Contains the global interface of the application where a Catalyst variable is declared to use Catalyst Web SDK methods.
app.module.ts: Contains the routing logic involved in the application
We will be coding the index.html, app.component.html, app.component.ts, and the app.module.ts files.
Create a Layout Component
You need to create a layout component in the src/app/ directory path. This component will contain the the styling elements, client logic and redirect logic to provide appropriate responses to authenticated and non-authenticated users.
Execute the following command in the src/app/ directory using the Catalyst CLI:
copy
$
ng generate component layout
This will create a folder named layout in the src/app/ directory.
This layout folder will contain:
Native Angular file called layout.component.pec.ts.
layout.component.html: Will contain the HTML component required during user validation.
layout.component.css: Will contain the required styling elements.
layout.component.ts: Will contain the routing and redirect logic.
We will be coding the layout.component.html, layout.component.css, and the layout.component.ts files.
Create a Login Component
You need to create a login component in the src/app/ directory path. This component will contain the redirect logic to provide appropriate responses to authenticated and non-authenticated users.
Execute the following command in the src/app/ directory using the Catalyst CLI:
copy
$
ng generate component login
This will create a folder named login in the src/app/ directory.
This login folder will contain:
Native Angular file called login.component.pec.ts.
login.component.html: Will contain the HTML component required during user validation.
login.component.css: Will contain the required styling elements.
login.component.ts: Will contain the routing and redirect logic.
We will be coding the login.component.css, and the login.component.ts files.
The client directory should now look like the one depicted in the following image.
You can now begin coding your client.
Note: Please go through the code given in this section to ensure you fully understand it.
Copy the below code and paste it in the index.html file present in the src/ directory.
Copy the following code and paste it in the layout.component.html, layout.component.css, and the layout.component.ts files present in the src/app/layout directory.
Let’s go over the working of the function and client code.
The end-user will first encounter the Login Page that you configured and styled in the console.
To create an account, they will click the Sign Up option and be directed to the Sign Up page.
Note: End-users can either sign up to your application using the Social Logins or use the conventional method of signing up to the application using their email address
After the information has been entered and they click Sign Up, or if they attempt to sign up using a Social Login of their preference, their details will be populated and provided as a JSON input to the Custom User Validation function —authorization_portal_function/index.js.
The function will only authenticate users that have signed up to the application using a Zylker email address.
Upon successful authentication, an Email Verification email will be sent to the account they used to sign up to the application.
Note: Because we are using Hosted Login, no additional logic to trigger the appropriate emails is required. It will be handled by Catalyst automatically.
After the user verifies their email address by clicking the link provided in the email, they will be signed up for the application. Their details can be viewed in the User Management section of the Authentication component.
The end-user can now log in to application using their authenticated credentials.
The end-user can also reset their password by clicking the Forgot Password option.
In this event, the Password Reset email will be sent to their registered email. Following the steps mentioned in the email, they can easily reset their credentials.