I'm having trouble with the following action "/login" route action in my UsersController class
public function actionLogin(){
$data = Yii::$app->getRequest()->getBodyParams();
$model = new Usuario();
//Validamos que se hayan recibido los campos
if(empty($data['email']) || empty($data['password'])){
throw new \yii\web\BadRequestHttpException("Debe ingresar email y password");
}
//Validamos usuario y contraseña
$usuario = $model->findByUsername($data['email']);
if(empty($usuario) || !$usuario->validatePassword($data['password'])){
throw new \yii\web\UnauthorizedHttpException("Usuario y/o contraseña incorrectos");
}
return $usuario;
}
The situation is that I'm using POST method to perform login, and I'm calling this route from a different domain, so the frontend library first try to call /login route with OPTIONS method to check if it is allowed or not to call /login with POST..
The problem is that the built in functionality of yii2 rest ActiveController is only for /users and /users/{id}
If I manually add this /login route to be available in both POST and OPTIONS through actions verbFilter, then yii Is trying to actually call the login action with the OPTIONS request. I mean, it is trying to perform login. Of course it can't, because it is not sending email and passwords fields, but I can see an error in the log file.
So, my question is... Is there any way to configure correctly this "custom" routes actions and make OPTIONS performs transparently? Because I'm expecting that login action not get executed when calling it with OPTIONS, but instead to return directly the OPTIONS allowed methods headers.
Update Info: Added URL manager rules
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => true,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/users'],
'pluralize' => false,
'tokens' => [
'{id}' => '<id:\\w+>'
]
],
//Rutas usuario
'v1/login' => '/v1/users/login'
],
],
I had this same issue.
This is how I fixed it:
I added an
extraPatterns
parameter to my rule, like this:This way, the
options
action will be called for every custom action that I have in any of these controllers.You need to attach
Cors
filter in your controller'sbehaviours()
method (see how in official guide) with following conditions:Cors
filter should be defined before Authentication / Authorization filtersoption
inAccessControl
filterIn your situation,
UsersController
might have suchbehaviors()
method:I solved issue by extending Cors filter class:
and then
By default the
yii\rest\UrlRule
class will apply those patterns to any endpoint :Which means that any request holding OPTIONS verbs will be redirected to
yii\rest\OptionsAction
if yourloginAction
is written inside a class extending ActiveController.What I suggest is to override
patterns
by leaving the only used verbs as your login action doesn't need any of the other CRUD actions. this should work with your case :NOTE: the solution by @CreatoR is also a requirement here and exactly as he did it without defining keys. otherwise OPTIONS verbs will be rejected if not authenticated.
In case if your login action is defined under a class extending
yii\rest\Controller
directly instead of passing throughyii\rest\ActiveController
(which should be appropriate for authentication actions as there is no CRUD needed here) then the same rules configs should work fine but you'll need to manually add the actionOptions to your code :