Documentation SmartAdmin PHP built-in Authentication routine
Documentation
Unknown error (8192): Parsedown::blockSetextHeader(): Implicitly marking parameter $Block as nullable is deprecated, the explicit nullable type must be used instead in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/vendor/erusev/parsedown/Parsedown.php on line 715 [0] in function {closure:/var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/lib/debug.php:6} in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 444 [1] in function include in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 444 [2] in function Composer\Autoload\includeFile in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 322 [3] in function Composer\Autoload\ClassLoader->loadClass in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/lib/common/Markdown.php on line 5 [4] in function include in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 444 [5] in function Composer\Autoload\includeFile in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 322 [6] in function Composer\Autoload\ClassLoader->loadClass in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/public/php_auth_docs.php on line 68
Unknown error (8192): Parsedown::blockTable(): Implicitly marking parameter $Block as nullable is deprecated, the explicit nullable type must be used instead in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/vendor/erusev/parsedown/Parsedown.php on line 853 [0] in function {closure:/var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/lib/debug.php:6} in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 444 [1] in function include in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 444 [2] in function Composer\Autoload\includeFile in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 322 [3] in function Composer\Autoload\ClassLoader->loadClass in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/lib/common/Markdown.php on line 5 [4] in function include in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 444 [5] in function Composer\Autoload\includeFile in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/vendor/composer/ClassLoader.php on line 322 [6] in function Composer\Autoload\ClassLoader->loadClass in /var/www/vhosts/plesk.groupe-dmx.fr/dev.groupe-dmx.fr/public/php_auth_docs.php on line 68
Authentication
SmartAdmin for PHP comes with a built-in Authentication routine that you can use out of the box. The workflow follows standard security guidelines for authenticating secured pages.
To play around with this feature, head over to the Login page.
Login
There are two ways you can authenticate the user, it's via password or access_token.
With password
// username and password usually from $_POST
$auth = ['username' => $username, 'password' => $password];
$authenticated = \App\App::web_auth($auth);
if ($authenticated) {
// redirect to the secured page
redirect(APP_URL.'/dashboard.php');
} else {
throw Exception('Invalid username or password');
}
If web_auth is successful, you most probably want to redirect the user to a secured page. From there, you can verify the user if already authenticated.
With Access Token
If you want to authenticate the user by access_token, used in the REST API for example, you need to use the \App\App::token_auth method instead.
$user = \App\App::token_auth([
'username' => USERNAME,
'access_token' => ACCESS_TOKEN
]);
Secured pages
When someone opens a secure page, you'll have to check if the user is authenticated. Use can do this by requiring init.auth.php in the page. This will initiate $_SESSION and fill in the $_user global variable -- the current logged-in user.
Note that this requires init.db.php as well, see Database Installation guide.
require_once 'init.php';
// require the db and auth
require_once 'init.db.php';
require_once 'init.auth.php';
// authenticate the page
$app->authenticate_page();
The authenticate_page method will check if the user is authenticated. If the page is opened via a browser, it will redirect automatically to the login page if not authenticated. If the request is via ajax, it will set the HTTP Status to 403 Forbidden.