User Login And Logout

User login

Users need to be logged in; this process is explained in this section. You will learn how to create a login page, and how to check their username and password.

Identifying the user, checking passwords

In order to determine if the user can log into the application and what their roles are, you must use a directory such as AD / LDAP. This could be a database table, or an AD or LDAP service. You will also need a method, that can decide if the given username and password are correct, and if so, what data it will need to create the user object. The authentication implementer method must be implemented in a separate class, which must also implement the AuthenticationInterface. This interface requires a method to be specified, the initUser method. This method has two parameters, one of which is the user’s name and the other one is the password. The method must return a CurrentUser class (or a class that extend from the CurrentUser). If the method returns with this class, the authentication is considered successful, if it returns with null, it is unsuccessful.

Identifying a user. In this example, a mock user is logged in, that has ‘admin’ as its username and password. In this case, there is no need for a user registry, since both the username and password is directly in the code. Please note that this example is for testing purposes only, do not use it on live applications:

This newly created class must be set to the JBStrap AUTHENTICATION parameter, so the framework could use it later.

Setting the authentication method to the framework:

Creating a login page

Creating a login page is one of the first steps in managing users. This page must let the user type in their username and password, so that they can log in. First, you must create a public page, which is placed on a public UI. The page and UI must be public, so the user can access it without logging in. The login page must extend from the BaseLoginPage class. This class provides two methods, that must be used:

  • checkUser : The method checks the username and password. The authentication method specified in the JBStrap AUTHENTICATION parameter is used.
  • loginUser : The method registers the logged in user to the framework

You must place a TextItem and PasswordItem component, which are responsible for allowing the user to input their username and password respectively. You will also need a Button component, that will allow the user to begin the login process. If the user clicks on it, the checkUser method should check if the login details are correct. If they are, the checkUser method will return with a CurrentUser class. If they are incorrect, it will return with null. In this case, the user should be informed that they have inputted the wrong username/password.

If the user was successfully identified, the loginUser method should log the user in, and navigate them to the correct landing page.

Example login page:

The implemented page allows the user to log in to the application. It the login was successful, the user will be navigated to the page, that was assigned the mainPage pageId.

For the framework to know which page must log the user in, the login page must be registered. If you did this, the user, will be directed to the login page if they want to open a page that requires a certain role, or requires them to be logged in.

Assigning the login page to the loginPage pageId:

Current user cookie

The JBStrap framework offers user cookies, which help identifying the user. This cookie is enabled by default, with a timeout of 30 minutes. This gives the user plenty of time to return to the application without logging in again, even if they close the browser, as the framework will automatically identify them, if they are within 30 minutes.

The user cookie can be customized using two JBStrap parameters. The first is the USER_COOKIE_TIMEOUT, which sets the timeout. You can specify a different timeout, using minutes. The other parameter is USE_USER_COOKIE, which enables or disables the user cookies. The parameter accepts Boolean values.

Modifying the User cookie timeout to one day:
Disabling user cookies:

User logout

The framework provides a built-in method that you can use if you need to log out a previously logged in user. This method can be called anywhere. When called, the user will be logged out, and they can only access public pages, until they log back in. After the logout, the user will be navigated to the page that was specified in the ENTRY_POINT JBStrap parameter.

Logging out a user who is currently logged in the client: