Sign in from JavaScript

Using the project.login() and project.logout() function is possible automatize the user sign in from a remote device. This could be useful, e.g., to perform the sign in by reading a user badge with a badge reader device.

This chapter show an example of how configure the application to manage the sign in by a remote device.

The application must have a default user

Since the project's functions are working only when the application is active, the application must start with a default user, maybe with read only privilege. Reading the badge, the application can be switched to a user with additional privilege. Later, the logout command will reactivate the default user without any particular privileges

In the below example we are using three tags to communicate with the remote device:

The TAG_LOGIN will be the command code to execute.

The remote device has to fill the required TAG_USERNAME and TAG_PASSWORD parameters, then fill the TAG_LOGIN parameter with the required login or logout command. Engine on HMI-RUNTIME will detect the TAG_LOGIN changes and perform the required command, then reset the TAG_LOGIN to its idle status.

TAG_LOGIN Commands
0 Idle
1 Login request
2 Logout request

At the project level, we have to add a JavaScript function block to detect when TAG_LOGIN will changes. The JavaScript code attached at the OnDataUpdate Action of the JavaScript function block will execute the required login/logout command.

The JavaScript code attached at the OnDataUpdate Action

var Username; var Password; var Login_CMD; function _JSFunctBlock_onDataUpdate(me, eventInfo) { Username = ""; Password = ""; Login_CMD = ""; var state = new State(); project.getTag("TAG_USERNAME", state, 0, "plc_Login(tagName, tagState)"); project.getTag("TAG_PASSWORD", state, 0, "plc_Login(tagName, tagState)"); project.getTag("TAG_LOGIN", state, 0, "plc_Login(tagName, tagState)"); return false; } function plc_Login(tagName, tagState) { if (tagName=="TAG_USERNAME") { Username = tagState.getValue(); } if (tagName=="TAG_PASSWORD") { Password = tagState.getValue(); } if (tagName=="TAG_LOGIN") { Login_CMD = tagState.getValue(); } if (Username!="" && Password!="" && Login_CMD!=""){ if (Login_CMD==1) { Reply = project.login(Username, Password); }; if (Login_CMD==2) { Reply = project.logout(false); // Logout only if not logged as default user }; project.setTag("TAG_LOGIN", 0); project.setTag("TAG_REPLY", parseInt(Reply)); } }

See also: