PHP code example of eril / auth
1. Go to this page and download the library: Download eril/auth library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
eril / auth example snippets
use Eril\Auth\Auth;
Auth::loadConfig(
__DIR__ . '/config/auth.php',
createIfMissing: true
);
$user = Auth::attempt(
$_POST['login'],
$_POST['password']
);
if (!$user) {
die(Auth::error());
}
echo "Welcome {$user->name()}";
use PDO;
return [
'db' => fn (): PDO => new PDO(
'mysql:host=localhost;dbname=app;charset=utf8mb4',
'root',
''
),
'login_field' => [
'email',
'username',
],
'remember_enabled' => true,
'permissions' =>
$user = Auth::attempt(
$login,
$password
);
if (!$user) {
echo Auth::error();
return;
}
'login_field' => [
'email',
'username',
'phone',
],
Auth::attempt('[email protected] ', 'secret');
Auth::attempt('john', 'secret');
Auth::attempt('+351999999999', 'secret');
Auth::login([
'id' => 1,
'name' => 'Administrator',
'email' => '[email protected] ',
'role' => 'admin',
]);
$user = Auth::loginWithProvider(
'google',
$googleUserId
);
'remember_enabled' => true,
if (!empty($_POST['remember'])) {
Auth::rememberUser();
}
'rate_limit' => [
'enabled' => true,
'max_attempts' => 5,
'decay_seconds' => 300,
'key' => 'login_ip',
],
Auth::logout();
if (Auth::check()) {
}
if (Auth::guest()) {
}
$user = Auth::user();
$id = Auth::id();
$profile = Auth::profile();
$user = Auth::user();
$user->id();
$user->name();
$user->login();
$user->role();
$user->is('admin');
$user->hasRole(
'admin',
'manager'
);
Auth::is('admin');
Auth::hasRole(
'admin',
'manager'
);
$user->can('appointments.create');
$user->cannot('appointments.delete');
Auth::can('appointments.create');
Auth::cannot('appointments.delete');
echo $user->email;
echo $user->phone;
echo $user->created_at;
echo $user['email'];
echo $user['phone'];
$array = $user->toArray();
$user->only(
'id',
'name',
'email'
);
$user->except(
'raw'
);
$row = $user->raw();
'profiles' => [
'patient' => [
'table' => 'patients',
'foreign_key' => 'user_id',
],
'professional' => [
'table' => 'professionals',
'foreign_key' => 'user_id',
],
],
$profile = Auth::profile();
$profile = Auth::user()?->profile();
echo $profile->bio;
echo $profile['bio'];
echo $profile->get('bio', default: 'no bio');
return [
'admin' => [
'*',
],
'professional' => [
'appointments.*',
'patients.view',
'activities.*',
],
'patient' => [
'appointments.view',
'appointments.create',
'appointments.cancel',
'profile.view',
'profile.update',
],
];
// structure example
'role' => [
'resource.action',
...
]
if (Auth::can('appointments.create')) {
}
if (Auth::cannot('appointments.delete')) {
}
Auth::authorize(
'appointments.create'
);
'admin' => [
'*',
],
'professional' => [
'appointments.*',
],
$result = Auth::diagnose();
[
'pdo_connection' => [
'ok' => true,
'message' => 'PDO connection is working.',
],
'user_table' => [
'ok' => true,
'message' => 'Table [users] exists.',
],
'login_field_email' => [
'ok' => true,
'message' => 'Column [email] exists.',
],
'provider_table' => [
'ok' => true,
'message' => 'Table [user_providers] exists.',
],
]
Eril\Auth\Exceptions\AuthException
Eril\Auth\Exceptions\AuthenticationException
Eril\Auth\Exceptions\AuthorizationException
Eril\Auth\Exceptions\ConfigurationException
try {
Auth::authorize('admin.panel');
} catch (AuthorizationException $e) {
echo $e->getMessage();
}
Auth::configure()
Auth::loadConfig()
Auth::attempt()
Auth::login()
Auth::loginWithProvider()
Auth::logout()
Auth::rememberUser()
Auth::check()
Auth::guest()
Auth::user()
Auth::profile()
Auth::id()
Auth::hasRole()
Auth::is()
Auth::can()
Auth::cannot()
Auth::authorize()
Auth::error()
Auth::diagnose()
text
config/
├── auth.php
└── permissions.php