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.
This newly created class must be set to the JBStrap AUTHENTICATION parameter, so the framework could use it later.
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.
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.
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.