Customizing authentication and response

Yii allows us to quickly create a custom authentication method for our application. This is useful because in some cases, the previously mentioned authentications are not sufficient.

A custom authentication model can be made by extending the yii\filters\auth\AuthMethod class, which implements yii\filters\auth\AuthInterface that requires overriding the authenticate ($user, $request, and $response) method:

<?php

namespace api\components;

use yii\filters\auth\AuthMethod;
use Yii;

class CustomAuthMethod extends AuthMethod {

    public function authenticate($user, $request, $response) {
    …
    …
    …
}
…
…
…
}

Even though the REST API should be stateless, or rather should not save session data, it could be necessary to store ...

Get Yii2 By Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.