Authentication

A short tutorial that demonstrates how to use the built-in authentication system in alt-seven.

alt-seven authentication tutorial

In order to enable built-in authentication, you must configure your application with the correct options. Here is a sample of the authentication options to be passed to the framework:

	remote: {
		modules: app.remote,
		loginURL: "/api/auth/login",
		logoutURL: "/api/auth/logout",
		refreshURL: "api/auth/refresh"
	},

In addition to the authentication methods, the a7 model must be enabled for authentication to work. See the page on configuring your application to understand all the options available to you.

In the login form, the programmer can then specify success and failure parameters, which can be methods, event names, or router destinations. In the below example, the loginform sends the username and password and then specifies success and failure methods to run after the authentication method returns from the server.

handleClick: function(event) {
        let state = loginform.getState();
        loginform.showMessage("");
        a7.events.publish('auth.login', {
          username: loginform.state.username,
          password: loginform.state.password,
          success: function( json ){
            main.runApp();
		  },
          failure: function( json ){
            state.message = json.message;
            state.username = "";
            state.password = "";
            loginform.setState( state );
            loginform.fireEvent("mustRender");
		  }
        });
      }

Success and failure could also be specified as events, such as:

  success: "auth.loginSuccess",
  failure: "auth.loginFailure"

or as router destinations:

  success: "auth/loginSuccess",
  failure: "auth/loginFailure"

The programmer must then create the events or router destinations named:

  a7.events.subscribe( "auth.loginSuccess", function( obj ){
    // handle login success tasks 
  });

Note that routes are simply URL destinations that are mapped to events. The purpose of routes is to give the user a URL in the browser’s location bar.

Routes mapped in an application may look like this:

export var routes = [
	[ '/auth/loginSuccess', 'auth.loginSuccess' ],
	[ '/', 'main.home' ]
];

If routes are enabled in the application, the strings will be assumed to be route destinations, otherwise they will be treated as events.


Last modified February 12, 2024: minor updates (18263f5)