PHP code example of mmdm / sim-auth

1. Go to this page and download the library: Download mmdm/sim-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/ */

    

mmdm / sim-auth example snippets

 
composer 



$host = '127.0.0.1';
$db = 'database name';
$user = 'username';
$pass = 'password';
// this is very nice collation to use
$charset = 'utf8mb4';

$dsn = "mysql:host={$host};dbname={$db};charset={$charset}";
$options = [
    // add this option to show exception on any bad condition
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
];
try {
    $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
    throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

__construct(PDO $pdo_instance, ?array $config = null);

// an array of arrays 
[
  // this array is columns of resource table and their values
  [
    column1 => value1,
    column2 => value2,
  ],
  [
    column1 => value3,
    column2 => value4,
  ],
  ...
]

[
  // this array is columns of role table and their values
  [
    column1 => value1,
    column2 => value2,
  ],
  [
    column1 => value3,
    column2 => value4,
  ],
  ...
]

__construct(
    PDO $pdo_instance,
    string $namespace = 'default',
    array $crypt_keys = [],
    int $storage_type = IAuth::STORAGE_DB,
    ?array $config = null
);

// this is constructor
$auth = new DBAuth (
    PDO $pdo_instance,
    string $namespace = 'default',
    array $crypt_keys = [],
    $algo = PASSWORD_BCRYPT,
    int $storage_type = IAuth::STORAGE_DB,
    ?array $config = null
);

// keys MUST be same as below
[
  'username' => provided username,
  'password' => provided password,
]

// for example check if user is activated
$auth->login([
    'username' => provided username,
    'password' => provided password,
], 'users.is_active=:active', [
    'active' => 1,
]);

// to see if login has succeed
$isLoggedIn = $auth->isLoggedIn();

try {
    $auth->login([
        'username' => provided username,
        'password' => provided password,
    ]);
} catch (\Sim\Auth\Exceptions\IncorrectPasswordException $e) {
    // do something according to error
    // eg.
    echo 'Username or password is incorrect!';
} catch (\Sim\Auth\Exceptions\InvalidUserException $e) {
    // do something according to error
    // eg.
    echo 'Username or password is incorrect!';
} catch (\Sim\Auth\Interfaces\IDBException $e) {
    // do something according to error
    // eg.
    echo 'Failed database connection!';
}

// for example check if user is activated
$auth->login(
  'provided username',
  'users.is_active=:active', 
  [
    'active' => 1,
  ]
);

// to see if login has succeed
$isLoggedIn = $auth->isLoggedIn();

try {
    $auth->login('provided username');
} catch (\Sim\Auth\Exceptions\InvalidUserException $e) {
    // do something according to error
    // eg.
    echo 'Username or password is incorrect!';
} catch (\Sim\Auth\Interfaces\IDBException $e) {
    // do something according to error
    // eg.
    echo 'Failed database connection!';
}

__construct(
    PDO $pdo_instance,
    ?array $config = null
);

// keys MUST be same as below
[
  'username' => provided username,
  'api_key' => provided api key,
]

// for example check if user is allowed or something
$auth->validate([
    'username' => provided username,
    'api_key' => provided api key,
], 'api_keys.allowed=:allow', [
    'allow' => 1,
]);

try {
    $auth->validate([
        'username' => provided username,
        'api_key' => provided api key,
    ]);
} catch (\Sim\Auth\Exceptions\IncorrectAPIKeyException $e) {
    // do something according to error
    // eg.
    echo 'Username or api key is incorrect!';
} catch (\Sim\Auth\Interfaces\IDBException $e) {
    // do something according to error
    // eg.
    echo 'Failed database connection!';
}

// for example check if user is allowed or something
$auth->validateAPI('provided api key', 'api_keys.allowed=:allow', [
    'allow' => 1,
]);

try {
    $auth->validateAPI('provided api key');
} catch (\Sim\Auth\Exceptions\IncorrectAPIKeyException $e) {
    // do something according to error
    // eg.
    echo 'Api key is invalid!';
} catch (\Sim\Auth\Interfaces\IDBException $e) {
    // do something according to error
    // eg.
    echo 'Failed database connection!';
}