Part 2: Services & Dependencies

Overview

To achieve our application's desired functionality, we'll create two services. These services will manage the access token storage and facilitate communication with the ProcessMaker API via the SDK.

Completed code: live examplearrow-up-right / download examplearrow-up-right

Step 1: Create the OAuth Client App

Follow this step-by-step guide to create your OAuth application here.

triangle-exclamation

Step 2: Set Up

Main TS

chevron-rightmain.tshashtag
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

platformBrowserDynamic()
	.bootstrapModule(AppModule)
	.catch((err) => console.error(err));

In the above file, we are starting Angular. Note line 5: this is where we bring in our environment variables which we will use to contain all our sensitive information.

circle-check

The Index

chevron-rightindex.htmlhashtag

We are utilizing getbootstrap.com, as that is what the ProcessMaker form builder is built with. Our forms utilize the 12 column grid system that bootstrap is so well known for, as well as other niceties that make our lives easier.

There are CDNs in the html file for fewer steps during installation, but if you prefer to have a local offline (if you are behind a corporate VPN perhaps), you can replace the CDN links.

circle-exclamation

AppModule

Next, we'll update the app.module.ts file from the previous section to incorporate the Angular router. This is going to tell the application what to render when a user does an action, such as opening the form.

This is the modified code.

chevron-rightapp.module.tshashtag

Router

Create the directory routing inside src/app/, and then add the file app-routing.module.ts. For now, we will just make it a very simple file since it will not be used yet, at least not until we have somewhere to route TO!

chevron-rightapp-routing.module.tshashtag

Right now, you will still see the Hello John on the webpage. We need to add a few more things before we will start seeing our application come together.

Guard

The Auth Guard is akin to a security checkpoint. Before navigating to a route (think of it as a destination within your application), this checkpoint checks if the user has the necessary permissions to access that route.

In more technical terms:

  • It's a service that implements a specific interface (CanActivate, for instance).

  • When a user tries to navigate to a route, the Auth Guard runs its logic.

  • If the logic returns true, navigation proceeds.

  • If it returns false, navigation is halted, often redirecting the user elsewhere.

For example, if you have a route that should only be accessible to logged on users, as is our case, an Auth Guard can check if the user is logged on. If they are, they can proceed to the inbox or form. If not, they will be redirected to the log on page.

Here is the code for our guard. Place it inside a new folder called guards and name is auth.guard.ts.

chevron-rightauth.guard.tshashtag

Step 3: Services

DB

This file interfaces with local storage for basic CRUD operations like saving the access token.

chevron-rightdb.service.tshashtag

Auth

circle-exclamation

There are four methods to this service:

  • login - This is the entry point for when the user will click on the login button.

  • logout - This erases the storage and the access token.

  • checkAuthStatus - This tells us if the user has an access token and is authenticated or not.

  • getAccessToken - This is the main method of the class. This is where we get our access token from ProcessMaker, and you can see we are using here the environment.ts variables.

chevron-rightauth.service.tshashtag

Step 4: The ProcessMaker SDK

We will be utilizing the ProcessMaker SDK for Angular. ProcessMaker utilizes Swagger / OpenAPI for our documentation and API SDKs. Any of the supported SDKs are available to generate via the Artisan CLI tool.

If you want to learn how to use the swagger-based SDK, you can see a video tutorial here:

SDK Video Tutorial

Below are the list of SDKs that can be generated via the command from within your root directory of your ProcessMaker application.

chevron-rightAvailable SDKshashtag

For convenience, the application already contains the SDK files that are necessary for the applications functions in the folder api which resides at the root of the application, sibling to the src directory.

Next Steps

Part 3: Componentschevron-right

Last updated