Overview

A simple, flexible authentication class that is easy to set up and understand.

Source is at https://bitbucket.org/vespula/vespula.auth

Installation

Installation is easiest via composer.

composer require vespula/auth

Example Usage

<?php

use Vespula\Auth\Session\Session;
use Vespula\Auth\Auth;
use Vespula\Auth\Adapter\Text;

$session = new Session();

// Optionally pass a maximum idle time and a time until the session expires (in seconds)
$max_idle = 1200;
$expire = 3600;
$session = new Session($max_idle, $expire);

$adapter = new Text(...);

$auth = new Auth($adapter, $session);

Check for valid credentials

<?php

$credentials = [
    'username'=>filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING),
    'password'=>filter_input(INPUT_POST, 'username') // use some filter here
];

$auth->login($credentials);
if ($auth->isValid()) {
    // Yay....
    echo "Welome " . $auth->getUsername();

    // Get userdata
    $userdata = $auth->getUserdata();
    echo $userdata['fullname'];

    // Shortcut to userdata
    echo $auth->getUserdata('fullname');
} else {
    // Nay....
    // Wonder why? Any errors?
    $error = $adapter->getError(); // may be no errors. Just bad credentials
    echo "Please try again, if you dare";
}

Perform logout

<?php

$auth->logout();

Check Authentication State

<?php

// Check if the user is valid (authenticated)
if ($auth->isValid()) {
    // Access some part of site
}

// Has the person been sitting idle too long?
if ($auth->isIdle()) {
    // Sitting around for too long
    // User is automatically logged out and status set to ANON
}

// Did the expire time get reached?
// Note that if the session actually expires, this won't show as being expired.
// This is because there is no status in a non-existent session.

if ($auth->isExpired()) {
    // Sitting around way too long!
    // User is automatically logged out and status set to ANON
}

Access to user data

<?php
$username = $auth->getUsername();

$userdata = $auth->getUserdata(); // varies by adapter

// get a specific key from the $userdata array (assuming it exists in the array)
$email = $auth->getUserdata('email');