Building a Login System

One of the most frequently requested tutorials is how to make a login system. We’re going to start off this ongoing tutorial by building just that. Please note that is only one way of creating a login system. There are many other viable options. This is simply meant to show you an approach to creating an application with login/logout functionality.

Disclaimer: This tutorial is meant to show users one way in which they can design an application to contain a “logged in” and “logged out” view. This tutorial does not attempt to achieve secure data connections, session management, true authentication, etc. It is for educational purposes only.

Step 1 - Generate your Application

The first thing we want to do is generate an application using Sencha Cmd . You can do so by issuing the following command from your command line interface, or CLI:

sencha -sdk extjs/5.0.1 generate app TutorialApp ./TutorialApp

The resulting TutorialApp is a fully functional Cmd generated application that we will use as a foundation for our login/logout application. You can view this application by navigating to the application’s location on your webserver. For instance:

`http://localhost/TutorialApp`

Or by starting “sencha app watch” from within the newly generated TutorialApp folder. This will spin up a Jetty server, which removes the need for a traditional web server. If you use “sencha app watch”, you can find the application here:

`http://localhost:1841/`

Note: If you do not have Sencha Cmd, or are unfamiliar with using it, please read the Cmd Introduction guide before proceeding.

Step 2 - Create the Login View Components

Next, we’re going to navigate to our newly generated TutorialApp. Drill down into your generated application’s “app/view” folder. You should see the default generated “main” folder. This folder contains the Main.js , MainController.js , and MainModel.js .

Let’s go ahead and prepare our project for the login functionality by creating a new app/view folder called “login”. After creating the “login” folder, add the following files to said folder:

- Login.js
- LoginController.js

The resulting file structure should look like this:

Step 3 - Disable autoCreateViewport

The autoCreateViewport config of Ext.application is a very handy way to automatically load and instantiate “TutorialApp.view.main.Main” by utilizing the Viewport Plugin . However, we have some evaluations to perform in Ext.application’s launch function prior to picking our initial view. For instance, we do not want to create the Main view if the user is not logged in.

For the purposes of this exercise, let’s remove the autoCreateViewport config from the Ext.application config in app.js . app.js is located in the root of your project.

Note: Since we have removed autoCreateviewport, refreshing your application will result in a blank page as no classes are being instantiated.

Step 4 - Create the Login Window

Next, let’s create a login view. To do this, we’ll open our blank Login.js file and begin defining the Login Window.

First, let’s define our Class and extend the base Window class . That looks like so:

Ext.define("TutorialApp.view.login.Login",{
    extend: 'Ext.window.Window'
    xtype: 'login'
});

Now, we’ve defined our Login Class as an extension of Ext.Window , which can be accessed by the name login . Let’s start giving our class some unique properties. We’ll start by adding some configurations to the window itself.

Ext.define("TutorialApp.view.login.Login",{
    extend: 'Ext.window.Window'
    xtype: 'login'

    requires: [
        'TutorialApp.view.login.LoginController',
        'Ext.form.Panel'
    ],

    controller: 'login',
    bodyPadding: 10,
    title: 'Login Window',
    closable: false,
    autoShow: true
});

Let’s walk through what these configurations mean.

requires

The requires block makes sure that we’re including any classes that may be relied upon before instantiating this class. We need to include LoginController.js because we’re about to designate it as our controller on the next line. We also need to include Ext.form.Panel since our view contains a form panel.

controller

The controller config designates a viewController , which will then be attached to specific instances of the view. This controller provides a place to include all logic relating to this view or its child components. Here, we designate controller to be login , which will be our controller’s alias.

bodyPadding

The bodyPadding config is purely aesthetic. This config applies “10px” of padding around the exterior of the window’s body content.

title

Designating a truthful value for the title config creates a header and adds our value as its title.

closable

Closable determines whether or not the window can be closed. Windows have a closable button by default. However, since this is a login window, we don’t want the user closing it. If they DID close it, they would be left with a blank page.

autoShow

Windows do not show by default when you create them. Setting autoShow to true is just a convenience that allows us to display the window without further interaction.

Now that we’ve discussed the window’s configuration, let’s give it some child components. Since this will be a login form, we’ll create a form item as a child of the window. Then we’ll add two form fields, a text field, and a submission button.

The final code for this file should appear as follows:

Ext.define("TutorialApp.view.login.Login",{
    extend: 'Ext.window.Window',
        xtype: 'login',

    requires: [
        'TutorialApp.view.login.LoginController',
        'Ext.form.Panel'
    ],

    controller: 'login',
    bodyPadding: 10,
    title: 'Login Window',
    closable: false,
    autoShow: true,

    items: {
        xtype: 'form',
        reference: 'form',
        items: [{
            xtype: 'textfield',
            name: 'username',
            fieldLabel: 'Username',
            allowBlank: false
        }, {
            xtype: 'textfield',
            name: 'password',
            inputType: 'password',
            fieldLabel: 'Password',
            allowBlank: false
        }, {
            xtype: 'displayfield',
            hideEmptyLabel: false,
            value: 'Enter any non-blank password'
        }],
        buttons: [{
            text: 'Login',
            formBind: true,
            listeners: {
                click: 'onLoginClick'
            }
        }]
    }
});

Let’s discuss these child additions.

items

The first configuration you’ll note is the items array . In Containers, like the form panel, this array may hold a component or a component configuration object. These components will be displayed in the container’s body using the container’s layout.

xtype

Every component class has it’s own xtype . You can think of an xtype as a shortcut to easily create an instance of a component. In this case, we have set an xtype of ‘form’, or Ext.form.Panel to be most precise. FormPanels are special types of Panels that include conveniences for configuring and working with fields.

Form Items

Next, you’ll see the familiar face of another items array. Here we’re nesting additional items by using the items array a level deeper. We’re placing more components inside of the parent component, which is the FormPanel. In this case, our nested components are the form fields that make up the login form.

We can walk quickly through this array of components as they’re pretty self explanatory. The first item has an xtype of Ext.form.field.Text textfield , a name of username , a fieldLabel of username , and allowBlank of false . This boils down to a textfield with a name value and a field label that cannot be left blank.

The next field is almost identical besides the type being set to password. This turns your input into ’*’’ for security’s sake.

The last member of this items array is a displayfield . A display field is a text field, which is not submitted with your form. It’s useful for conveying data without the user interacting with said data. In this case, we’re alerting the user that any non-blank password will allow a successful submission.

buttons

The last bit that we’ve added here is a button's array . This is a convenience config used for adding buttons to your panel. This particular button will contain the text ‘Login’.

formBind

Our button contains a config called formBind , which is set to true. When a component has formBind set to true, it will be disabled/enabled depending on the validity state of the form. This means that the button will not be clickable until the two input fields contain values.

listeners

The listeners object exists to contain event handlers to be added to the component during initialization. In this case, we’re waiting for someone to click the button. Upon a click, we “forward” the event to a function called onLoginClick() . onLoginClick() will later be defined in our Login controller.

Note: We have not yet called the login view in any way, so refreshing the application will not show any changes.

Step 5 - Add Login Logic

Next, let’s create the login controller, which is a class that contains any logic used to handle user interactions with the login view. To do this, we’ll open our blank LoginController.js file and begin defining our Login Window’s logic.

The entirety of LoginController.js is as follows:

Ext.define('TutorialApp.view.login.LoginController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.login',

    onLoginClick: function(){        

        // This would be the ideal location to verify the user's credentials via 
        // a server-side lookup. We'll just move forward for the sake of this example.

        // Set the localStorage value to true
        localStorage.setItem("TutorialLoggedIn", true);

        // Remove Login Window
        this.getView().destroy();

        // Add the main view to the viewport
        Ext.widget('app-main');
    }
});

The above code may be a little out of context, but it will make more sense when we discuss the launch function in the next section. This class contains the onLoginClick() function that is called by clicking on the login button click.

These bits of code have annotations to describe each statement’s purpose, but let’s look at them piece by piece for further explanation.

onLoginClick()

First of all, we’re creating a function called onLoginClick() . This is the function to which we directed our login button’s click event in the Login view.

As mentioned in the comments, this is where you’d call your server to verify that the user’s credentials are valid. This would generally come in the form of an AJAX or REST request. However, for this tutorial, we will accept any non-blank input. Upon success, you’d follow through with the rest of the code. On failure, you would allow the user to re-enter their credentials. Of course, there’s no failure possibility in this case, so let’s move forward!

localStorage

We are utilizing localStorage in this tutorial to maintain user login state. After a successful credentials check, we can determine that the user has the appropriate access to view Main. Due to this entry allowance, we can then set a key/value pair in localStorage to let the application know that the user is valid. Next, we will check that the TutorialLoggedIn localStorage key is set to true in our initial launch function (next section).

getView()

ViewControllers introduce a very helpful method called getView() . getView() returns the current view associated with the viewController from which it is called. In this case, the view is the login window. We no longer want to present the login window so we are using this.getView().destroy() to get a reference to the login window and then destroy it.

Ext.widget(‘app-main’)

Now that we’ve destroyed the login window, we want to change the view to display the Main view. In this case, we use Ext.Widget(‘app-main’) to instantiate the Main.js view.

Note: ‘app-main’ refers to the xtype that is set on our Sencha Cmd generated Main.js .

Step 6 - Add Launch Logic to Application.js

Next, let’s discuss Application.js and the launch function.

Application.js is the heart of your application. You can find Application.js at the same level as your view, store, and model folders. It provides a handy function called launch(), which fires when your application has loaded all of its required classes. Here is the full code for this tutorial’s Application.js file.

/**
 * The main application class. An instance of this class is created by `app.js` when it calls
 * Ext.application(). This is the ideal place to handle application launch and initialization
 * details.
 */
Ext.define('TutorialApp.Application', {
    extend: 'Ext.app.Application',

    name: 'TutorialApp',

    stores: [
        // TODO: add global / shared stores here
    ],
    views: [
        'TutorialApp.view.login.Login',
        'TutorialApp.view.main.Main'
    ],
    launch: function () {

        // Check whether the browser supports LocalStorage
        // It's important to note that this type of application could use
        // any type of storage, i.e., Cookies, LocalStorage, etc.
        var supportsLocalStorage = Ext.supports.LocalStorage, 
            loggedIn;

        if (!supportsLocalStorage) {

            // Alert the user if the browser does not support localStorage
            Ext.Msg.alert('Your Browser Does Not Support Local Storage');
            return;
        }

        // Check to see the current value of the localStorage key
        loggedIn = localStorage.getItem("TutorialLoggedIn");

        // This ternary operator determines the value of the TutorialLoggedIn key.
        // If TutorialLoggedIn isn't true, we display the login window,
        // otherwise, we display the main view        
        Ext.widget(loggedIn ? 'app-main' : 'login');

    }
});

Let’s discuss what these pieces are doing.

requires

We’ve already described what requires does, but let’s touch on this particular array. For the purposes of Application.js , we need to prepare the application to load either the Login or Main view, depending on the results of the logged in evaluation to come. To that end, we must require TutorialApp.view.main.Main and TutorialApp.view.login.Login so that either result is available.

launch

As mentioned, the launch function is a function that fires when you’re application is has loaded everything it needs to progress. This is an ideal place to perform logic regarding user state for a login/logout application.

Ext.supports.LocalStorage

Ext.supports.LocalStorage is a handy Ext singleton function that returns true or false as to whether your browser supports localStorage. Most modern browsers do support localStorage, though, older versions of IE do not. localStorage may not be ideal for your production application, but it is a very handy way to store information without involving server side solutions like cookies or sessions. In this tutorial, we will check to see if localStorage is supported. If not, we display an error message saying that the application cannot be utilized with your browser. We then do a return, which will stop the progress of further logic.

localstorage.getItem()

Assuming your browser DOES support localStorage, the next step is to check for a previously set key called TutorialLoggedIn. We are simply setting the loggedIn variable to the result of that key’s value. If it doesn’t exist, loggedIn will be null. If it does exist, we’ve previously set loggedIn to be true in our LoginController’s logic.

Widget Ternary

Most programming languages contain a form of shorthand called ternary operators. Ternary operators allow you to minimize the amount of code required for a traditional if/else statement. In this case, we’re using a ternary to say, “if loggedIn exists (isn’t null), let’s load the main view, otherwise, load the logged in view”. We are then using the encapsulating Ext.widget function to generate the result of the ternary operator.

Step 7 - Add Viewport Plugin

As you may remember, we removed the autoCreateViewport config from app.js early on in this tutorial. Since we don’t have a viewport defined, your main view will not know where to render. We are going to change that by mixing in the viewport plugin so that Main.js will operate as our application’s viewport, so that the Main view takes up all available width and height within the browser.. It’s as simple as adding the following line to Main.js

plugins: 'viewport',

Our resulting Main.js file will look like this:

/**
 * This class is the main view for the application. It is specified in `app.js` as the
 * "autoCreateViewport" property. That setting automatically applies the "viewport"
 * plugin to promote that instance of this class to the body element.
 *
 * TODO - Replace this content of this view to suite the needs of your application.
 */
Ext.define('TutorialApp.view.main.Main', {
    extend: 'Ext.container.Container',
    plugins: 'viewport',
    requires: [
        'TutorialApp.view.main.MainController',
        'TutorialApp.view.main.MainModel'
    ],

    xtype: 'app-main',

    controller: 'main',
    viewModel: {
        type: 'main'
    },

    layout: {
        type: 'border'
    },

    items: [{
        xtype: 'panel',
        bind: {
            title: '{name}'
        },
        region: 'west',
        html: '<ul><li>This area is commonly used for navigation, for example, using a "tree" component.</li></ul>',
        width: 250,
        split: true,
        tbar: [{
            text: 'Button',
            handler: 'onClickButton'
        }]
    },{
        region: 'center',
        xtype: 'tabpanel',
        items:[{
            title: 'Tab 1',
            html: '<h2>Content appropriate for the current navigation.</h2>'
        }]
    }]
});

You do not need to modify any of the other pieces of Main.js since we will be using the default generated ‘onClickButton’ function for our click handler.

Step 8 - Add Main Logic

We’re almost finished! All that’s left now is to give the user some way to logout of the application, which will destroy the TutorialLoggedIn key from localStorage. This logic should take place in the MainController.js file. You can remove the other generated code if you like. This is the final definition of MainController.js for this tutorial:

Ext.define('TutorialApp.view.main.MainController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.main',

    onClickButton: function () {

        // Remove the localStorage key/value
        localStorage.removeItem('TutorialLoggedIn');

        // Remove Main View
        this.getView().destroy();

        // Add the Login Window
        Ext.widget('login');
    }
});

We shouldn’t need to go into much depth here since it is basically the inverse of our LoginController.js code.

To summarize this functionality, onClickButton is the function that is called by the button handler in our generated Main.js view. Once the click event is detected, take the following steps:

  • Remove the localStorage key that maintains the user’s logged in state.

  • Destroy the current view, which is Main.js .

  • Recreate the login view.

You should now be able to load your application in your browser and see a fully functioning login/logout application.

Wrap Up

We hope that you have enjoyed this tutorial. This is simply the foundation for an application, but hopefully we have introduced a few concepts that make your future projects simpler. Please feel free to suggest ideas for future tutorials via our forums . Also, feel free to follow up with questions via the support portal or forums .

Last updated