mirror of
https://github.com/elyby/accounts.git
synced 2024-12-23 22:00:06 +05:30
48 lines
1.1 KiB
PHP
48 lines
1.1 KiB
PHP
<?php
|
|
namespace api\controllers;
|
|
|
|
use api\models\LoginForm;
|
|
use Yii;
|
|
use yii\filters\AccessControl;
|
|
|
|
class AuthenticationController extends Controller {
|
|
|
|
public function behaviors() {
|
|
return array_merge(parent::behaviors(), [
|
|
'access' => [
|
|
'class' => AccessControl::class,
|
|
'rules' => [
|
|
[
|
|
'actions' => ['login'],
|
|
'allow' => true,
|
|
'roles' => ['?'],
|
|
],
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function verbs() {
|
|
return [
|
|
'login' => ['POST'],
|
|
];
|
|
}
|
|
|
|
public function actionLogin() {
|
|
$model = new LoginForm();
|
|
$model->load(Yii::$app->request->post());
|
|
if (($jwt = $model->login()) === false) {
|
|
return [
|
|
'success' => false,
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
|
];
|
|
}
|
|
|
|
return [
|
|
'success' => true,
|
|
'jwt' => $jwt,
|
|
];
|
|
}
|
|
|
|
}
|