Sanji Auth UI
Sanji Auth UI is a core component of the Sanji UI framework, specifically designed for seamless integration within AngularJS 1.x applications. It provides robust authentication and authorization services, leveraging JSON Web Tokens (JWT) for secure user identification and session management. The package includes an `auth` service for handling login requests and a `session` service for storing and retrieving authenticated user data. Its current stable version is `1.4.0`, released in September 2018, indicating an infrequent and likely discontinued release cadence. A key differentiator is its tight coupling with the AngularJS 1.x ecosystem (specifically versions 1.5.0 to 1.6.x), making it unsuitable for modern Angular or other contemporary frontend frameworks. It also relies on specific versions of `angular-http-auth`, `angular-storage`, and `sanji-rest-ui` as peer dependencies.
Common errors
-
Module 'webapp' is not available! You either misspelled the module name or forgot to load it. If this error occurs while loading a route, ensure that you have configured the route correctly.
cause The 'sanji.auth' module was not correctly registered or included as a dependency in your main AngularJS application module.fixEnsure `'sanji.auth'` is explicitly listed in your `angular.module()` dependencies: `angular.module('yourApp', ['sanji.auth'])`. -
Error: [$injector:unpr] Unknown provider: authProvider <- auth
cause The `auth` service was requested via dependency injection, but the `sanji.auth` module was either not loaded or its services were not correctly initialized.fixVerify that the `sanji-auth-ui` script is loaded *before* your AngularJS application code in your HTML, and that `sanji.auth` is correctly added as a dependency to your AngularJS application module. -
Uncaught TypeError: angular.module is not a function
cause The AngularJS library (`angular.js`) itself is not loaded or is loaded incorrectly, before any dependent modules or application code.fixEnsure `angular.js` (or `angular.min.js`) is loaded as the very first script in your HTML `<head>` or `<body>` before `sanji-auth-ui` or any other AngularJS-related scripts.
Warnings
- breaking This package is specifically built for AngularJS 1.x (versions 1.5.0 to 1.6.x) and is incompatible with newer versions of Angular (Angular 2+) or other frontend frameworks. Attempting to use it outside its intended environment will lead to runtime errors.
- gotcha The project appears to be abandoned, with its last release (v1.4.0) in September 2018. This means no further bug fixes, security updates, or feature enhancements are expected, making it a potential security risk for production applications.
- gotcha The package relies heavily on several specific peer dependencies (angular, angular-http-auth, angular-storage, sanji-rest-ui). Mismatched versions of these dependencies can lead to runtime errors or unexpected behavior due to API changes or incompatibilities.
Install
-
npm install sanji-auth-ui -
yarn add sanji-auth-ui -
pnpm add sanji-auth-ui
Imports
- 'sanji.auth'
import { sanjiAuth } from 'sanji-auth-ui'angular.module('myApp', ['sanji.auth']) - auth
import { auth } from 'sanji-auth-ui'class MyController { constructor($http, auth, session) { /* ... */ } } - session
import { session } from 'sanji-auth-ui'class MyController { constructor($http, auth, session) { /* ... */ } }
Quickstart
angular.module('webapp', ['sanji.auth'])
.controller('AppController', ['$http', 'auth', 'session', function($http, auth, session) {
this.credentials = { username: '', password: '' };
this.login = (credentials) => {
// Authenticate a user
auth.login('/auth/local', credentials)
.then((data) => {
// Return token data
return $http.get('/users/me');
})
.then((res) => {
// Return authenticated user data and save in session service
session.setUserData(res.data);
console.log('User logged in:', session.getUserData());
})
.catch((error) => {
console.error('Login failed:', error);
});
};
}]);
// Example of configuring authProvider
angular.module('webapp').config(['authProvider', function(authProvider) {
authProvider.configure({
roles: {
admin: 'admin',
guest: 'guest'
}
});
}]);
// Example of configuring sessionProvider
angular.module('webapp').config(['sessionProvider', function(sessionProvider) {
sessionProvider.configure({
tokenHeader: 'Authorization',
tokenKey: 'jwt_token'
});
}]);