TM1 Rest API – CRUD App – 4th and the Final Chapter
In this chapter we will discuss about login screen and routing the token through the application and deleting the cookie to logout. The login screen looks like below (note: this can be extended to CAM authentication with an input field)
Once you type in the username and password and click the login button, we can see token generated in the browser application (once we inspect the browser)
Before we get into what happens in the background when we click the login button. We need to understand how the first page of our application is defaulted to log in screen. This is completed using index.js with routing as below.
The application’s first screen is “Authentication” and once the authentication is successful the /chores screen will be rendered as “App” (App.js) as this is encapsulated inside cookies provider.
Let’s now check how the authentication is completed against our TM1 login account.
Valid Username and Password
User name and password is converted to base64 format and saved in ‘authToken’ is passed as an argument to loginUser function. This function just tries to do a GET call against the TM1 database. If the get call is successful an active session will be established and then the Token will be set as a cookie and App.js will be rendered as per our routing.
?The above useEffect checks the token is saved in mr-token and renders the next page. However, it does not check if the token in valid or not (for example: if we authenticate with any username and password the chores page will be rendered at this point). The validity of the token happens in app.js.
We have three variables’ data, loading and error.?These variables are set in fetch.js as below execution cycle:
1.?????We have asynchronous call to the TM1 server
2.?????setLoading as true and error as null (before anything is retrieved)
3.?????Then call getChores and store it in “data”.
4.?????Catch the error in “error” using setError,
领英推荐
5.?????Set current data (setData (data))
6.?????setLoading to false
7.?????Repeat this process and return the data, loading, error.
8.?????If not errored setChores in app.js and use the token throughout the application.
Invalid Username and Password
The same steps happen till step 8 and If there is error set in “error” variable. Then an invalid credentials alert is popped up.?
Logout
The logout button is on the right-side top corner. When clicked it deletes the token and closes the TM1 session. Once token is deleted the second useEffect in App.js defaults to the login screen.?
This completes the series of TM1 (PA) rest API CRUD operation. This web application can be extended to any usecase within TM1 (PA) For example: Source code version control with GITHub integration. Furthermore, this application can be further optimised.
Links