Authentication

How the built-in authentication system works: tokens, refresh, sessions.

altseven ships an authentication mechanism built on HTTP Basic auth for login and a session token for subsequent requests, with an automatic refresh cycle.

See the authentication tutorial for a step-by-step login form example; this page describes the machinery.

Configuration

export const myApp = new Application( {
	remote: {
		loginURL: "/api/auth/login",
		logoutURL: "/api/auth/logout",
		refreshURL: "/api/auth/refresh",
		useTokens: true,           // default
		tokenType: "X-Token"       // or "access_token"
	},
	security: {
		enabled: true,             // default
		userArgs: { userID: "" }   // default fields for the session user
	},
	auth: {
		sessionTimeout: 15 * 60 * 1000   // default: 15 minutes
	}
} );

With security enabled, Application.init() awaits an authentication check before your startup code proceeds: it calls the auth.refresh remote method and resolves myApp.authenticated and myApp.user.

The three endpoints

login — POST loginURL. The framework sends HTTP Basic auth (Authorization: Basic base64(username:password)) and a JSON body { rememberMe: boolean }. Your server responds with:

  • the session token in the X-Token response header (or Access_token if tokenType: "access_token"), which the framework stores in sessionStorage
  • a JSON body { success: true, user: { ... } } (or { success: false, ... })

refresh — GET refreshURL with the token attached. Your server responds { authenticated: boolean, user: { ... } }. A 401 is treated as { authenticated: false }. This endpoint is called:

  • once at application startup (the isAuthenticated check)
  • periodically, on the auth.sessionTimeout timer, to keep the session alive

logout — POST logoutURL. On { success: true } the framework invalidates the session (clears the token, resets the user).

The flow from your code

You never call these endpoints directly — publish the built-in events:

myApp.events.publish( "auth.login", {
	username, password, rememberMe,
	success: function( json ) { /* navigate into the app */ },
	failure: function( json ) { /* show the error */ }
} );

myApp.events.publish( "auth.logout", { success: ( json ) => ... } );

success/failure may be functions, event topic names, or router paths (when the router is configured). See the tutorial for the full pattern.

Session state

  • The current user is held in myApp.user, mirrored into sessionStorage and (when a model is in use) myApp.model.get( "user" ).
  • myApp.security.isAuthenticated() returns a promise of { authenticated: boolean, user }; concurrent calls share one refresh request.
  • myApp.security.invalidateSession() clears the token and resets the user — the auth.sessionTimeout and auth.invalidateSession topics do this too.
  • When a refresh call fails unexpectedly, the framework publishes auth.logout itself.

SAML / SSO variants

Set remote.credentials: "include" to rely on cookie sessions instead of tokens (the login/refresh/logout calls then send cookies cross-origin). The rest of the flow — the three endpoints and the events — is unchanged.