user2540463
user2540463

Reputation: 112

Zend Framework 2 Authentication without DbTable check

i'm trying to make a Zend 2 classical Authentication, without using a DbTable check.

I'll explain better

The problem is using that code when in the next page call i perform:

if($authService->hasIdentity())

it response false.

So, how can i do to save identity using custom authentication? i think i can implement Zend\Authentication\Adapter\AdapterInterface but i don't known exacly how...

any help is appreciated, thanks a lot :)

Upvotes: 1

Views: 793

Answers (1)

Tomdarkness
Tomdarkness

Reputation: 3820

You can create an adapter like so:

use Zend\Authentication\Adapter\AdapterInterface;
use Zend\Authentication\Result as AuthResult;

class MyAdapter implements AdapterInterface
{

    /**
     * @return \Zend\Authentication\Result
     */
    public function authenticate()
    {
        /* Return if can't find user */
        return new AuthResult(AuthResult::FAILURE_IDENTITY_NOT_FOUND, null);
        /* Return if success, second parameter is the identity, e.g user. */
        return new AuthResult(AuthResult::SUCCESS, $identity);
        /* Return if user found, but credentials were invalid */
        return new AuthResult(AuthResult::FAILURE_CREDENTIAL_INVALID, null);
    }
}

You can use this adapter with the ZF2 AuthenticationService like so:

$auth_service = new \Zend\Authentication\AuthenticationService();
/* Where $myAdapter is a instance of the MyAdapter class above */
$auth_service->setAdapter($myAdapter);

Upvotes: 3

Related Questions