PHP code example of windwalker / authenticate

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

    

windwalker / authenticate example snippets

 php
public function login($username, $password)
{
    $auth = new Authenticate;

    // Attach methods
    $auth->addMethod(new LocalMethod);
    $auth->addMethod(new MyMethod);

    $credential = new Credential;

    $credential->username = $username;
    $credential->password = $password;

    // Do authenticate
    $result = $auth->authenticate($credential);

    // False means login fail
    if (!$result)
    {
        // Print results to know what happened
        print_r($auth->getResults());

        throw new Exception('Username or password not matched');
    }

    $user = $auth->getCredential();

    return $user;
}
 php
use Windwalker\Authenticate\Method\AbstractMethod;

class MyMethod extends AbstractMethod
{
    public function authenticate(Credential $credential)
    {
        $username = $credential->username;
        $password = $credential->password;

        if (!$username || !$password)
        {
            $this->status = Authenticate::EMPTY_CREDENTIAL;

            return false;
        }

        $user = Database::loadOne(array('username' => $username));

        if (!$user)
        {
            $this->status = Authenticate::USER_NOT_FOUND;

            return false;
        }

        if (!password_verify($password, $user->password))
        {
            $this->status = Authenticate::INVALID_CREDENTIAL;

            return false;
        }

        // Success
        $this->status = Authenticate::SUCCESS;

        // Set some data to Credential
        $credential->bind($user);

        unset($credential->password);

        return true;
    }
}