[READ ONLY] Authentication package to support multiple login methods.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Method
Test
.gitignore
.travis.yml
Authentication.php
AuthenticationInterface.php
Credential.php
README.md
composer.json
phpunit.travis.xml
phpunit.xml.dist

README.md

Windwalker Authentication

Installation via Composer

Add this to the require block in your composer.json.

{
    "require": {
        "windwalker/authentication": "~3.0"
    }
}

Getting Started

This is a simple login auth process.

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

    // 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;
}

Create Custom Methods

use Windwalker\Authentication\Method\AbstractMethod;

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

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

            return false;
        }

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

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

            return false;
        }

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

            return false;
        }

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

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

        unset($credential->password);

        return true;
    }
}