PHP code example of webiik / account

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

    

webiik / account example snippets


$account = new \Webiik\Account\Account();

// Add your account implementation
$account->addAccount('YourAccount', function () {
	return new \Webiik\Service\YourAccount());
});

// Use one of your account implementations
$account->useAccount('YourAccount');

// Try authenticate a user
try {
    $user = $this->account->auth([
        'email' => '[email protected]',
        'password' => 'meow!'
    ]);
    echo 'User ID: ' . $user->getId() . '<br/>';
    echo 'User status: ' . $user->getStatus() . '<br/>';
    echo 'User role: ' . $user->getRole() . '<br/>';
    
    if ($user->hasRole('admin')) {
        // Do something...
    }

} catch (AccountException $e) {
    echo $e->getMessage() . '<br/>';
    print_r($e->getValidationResult());
}

addAccount(string $name, callable $accountFactory): void

$account->addAccount('YourAccount', function () {
	return new \Webiik\Service\YourAccount());
});

useAccount(string $name): void

$account->useAccount('YourAccount');

setNamespace(string $namespace): void

// Set namespace to 'en'
$account->setNamespace('en');

// Let's say, user '[email protected]' is authenticated in
// namespace 'es', so the following authentication fails.
try {
    $user = $this->account->auth([
        'email' => '[email protected]',
        'password' => 'meow!'
    ]);   
} catch (AccountException $e) {
    echo $e->getMessage(); // e.g. Account does not exist.
}

auth(array $credentials): User

try {
    $user = $this->account->auth([
        'email' => '[email protected]',
        'password' => 'meow!'
    ]);   
} catch (AccountException $e) {
    echo $e->getMessage();
}

reAuth(string $uid): User

try {
    $user = $this->account->reAuth('d4rjke8');
} catch (AccountException $e) {
    echo $e->getMessage();
}

signup(array $credentials): User

try {
    $user = $this->account->signup([
        'email' => '[email protected]',
        'password' => 'meow!'
    ]);   
} catch (AccountException $e) {
    echo $e->getMessage();
}

update(int $uid, array $data): User

try {
    $user = $this->account->update(1, [
    	'email' => '[email protected]',
    ]);   
} catch (AccountException $e) {
    echo $e->getMessage();
}

disable(int $uid, int $reason, array $data = []): User

try {
    $user = $this->account->disable(1, \Webiik\Account\AccountBase::ACCOUNT_IS_BANNED);   
} catch (AccountException $e) {
    echo $e->getMessage();
}

delete(int $uid, array $data = []): User

try {
    $user = $this->account->delete([
        'uid' => 1,       
    ]);   
} catch (AccountException $e) {
    echo $e->getMessage();
}

createToken(): string

try {
    $token = $this->account->createToken();
    
    // Send token to user, so he can perform an action...
     
} catch (AccountException $e) {
    echo $e->getMessage();
}

activate(string $token): User

try {
    $user = $this->account->activate($token);
} catch (AccountException $e) {
    echo $e->getMessage();
}

resetPassword(string $token, string $password): User

try {
    $user = $this->account->resetPassword($token, 'new-password');
} catch (AccountException $e) {
    echo $e->getMessage();
}

__construct(int $status, $id, string $role = '', array $info = [])

$user = new User(self::ACCOUNT_IS_OK, 1, 'user');

getId()

$uid = $user->getId();

getRole(): string

$role = $user->getRole();

hasRole(string $role): bool

if ($user->hasRole('user') {
    // Do something...
}

getInfo(): array

$info = $user->getInfo();

needsActivation(): bool

if ($user->needsActivation()) {
    // Send activation to user email
}

__construct($message = '', $code = 0, \Throwable $previous = null, array $validationResult = [])

throw new AccountException(self::MSG_INVALID_CREDENTIAL, self::INVALID_CREDENTIAL, null, ['email' => ['Invalid email'], 'password' => ['Password is too short.']]);

getValidationResult(): array

try {
    $user = $this->account->auth([
        'email' => '[email protected]',
        'password' => 'meow!'
    ]);   
} catch (AccountException $e) {
    print_r($e->getValidationResult()); // e.g. ['email' => ['Invalid email'], 'password' => ['Password is too short.']]
}