Authentication
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-Tokenresponse header (orAccess_tokeniftokenType: "access_token"), which the framework stores insessionStorage - 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
isAuthenticatedcheck) - periodically, on the
auth.sessionTimeouttimer, 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 intosessionStorageand (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 — theauth.sessionTimeoutandauth.invalidateSessiontopics do this too.- When a refresh call fails unexpectedly, the framework publishes
auth.logoutitself.
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.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.