fix mobileapi login

This commit is contained in:
2022-02-15 22:49:47 +01:00
parent d6caffb11c
commit aec6913000
8 changed files with 219 additions and 54 deletions

View File

@@ -44,7 +44,7 @@ return [
],
'jwt' => [
'class' => Jwt::class,
'key' => 'secret',
'key' => 'dianaveronika',
// You have to configure ValidationData informing all claims you want to validate the token.
'jwtValidationData' => JwtValidationData::class,
],

View File

@@ -27,7 +27,9 @@ class LoginController extends RestController
public function actionLogin()
{
$form = new LoginForm();
$post = \Yii::$app->request->post();
$post2 = $_POST;
$form->load(\Yii::$app->request->post(), '');
if ($form->validate()) {

View File

@@ -17,11 +17,11 @@ class RestController extends Controller
public function behaviors()
{
$behaviors = parent::behaviors();
// $behaviors['authenticator'] = [
// 'class' => JwtHttpBearerAuth::class,
// 'auth' => [$this, 'auth'],
// 'optional' => $this->getOptionalActions(),
// ];
$behaviors['authenticator'] = [
'class' => JwtHttpBearerAuth::class,
'auth' => [$this, 'auth'],
'optional' => $this->getOptionalActions(),
];
return $behaviors;
}
@@ -30,31 +30,31 @@ class RestController extends Controller
* @param Token $token
* @return Customer|null
*/
// public function auth($token)
// {
// if ( !isset($token ) ) {
// return null;
// }
// try {
// $uid = (string) $token->getClaim('uid');
// $customer = Customer::findOne(['id_customer' => $uid]);
// if (isset($customer)) {
// \Yii::$app->user->setIdentity($customer);
// return $customer;
// }
// } catch (Exception $e) {
// Yii::error('Failed to load customer: ' . $e->getMessage());
// }
// return null;
// }
//
// /**
// * @see AuthMethod::$optional
// * @return array
// */
// protected function getOptionalActions(){
// return [];
// }
public function auth($token)
{
if ( !isset($token ) ) {
return null;
}
try {
$uid = (string) $token->getClaim('uid');
$customer = Customer::findOne(['id_customer' => $uid]);
if (isset($customer)) {
\Yii::$app->user->setIdentity($customer);
return $customer;
}
} catch (Exception $e) {
Yii::error('Failed to load customer: ' . $e->getMessage());
}
return null;
}
/**
* @see AuthMethod::$optional
* @return array
*/
protected function getOptionalActions(){
return [];
}
}

View File

@@ -1,15 +1,23 @@
<?php
namespace mobileapi\models;
use common\manager\MobileDeviceManager;
use common\models\Customer;
use Yii;
use yii\base\Model;
use yii\db\ActiveRecord;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
/**
* Login form
*
* @property Customer
*/
class LoginForm extends Model
{
// cardnumber
public $username;
public $password;
@@ -27,12 +35,13 @@ class LoginForm extends Model
['password', 'validatePassword'],
];
}
public function attributeLabels(){
return [
'username' =>Yii::t('common/site', 'Username'),
'password' =>Yii::t('common/site', 'Password'),
];
public function attributeLabels()
{
return [
'username' => Yii::t('common/site', 'Username'),
'password' => Yii::t('common/site', 'Password'),
];
}
/**
@@ -45,26 +54,34 @@ class LoginForm extends Model
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
if ($this->hasErrors()) {
/** @var \common\models\Customer $user */
$customer = $this->getCustomer();
if (!$customer || !$customer->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
throw new BadRequestHttpException();
}
$customer = $this->getCustomer();
}
/**
* Finds user by [[username]]
*
* @return Customer|null
* @return Customer|null|ActiveRecord
*/
public function getCustomer()
{
if ( $this->customer === null ){
$this->customer = Customer::findIdentity( $this->username );
if ($this->customer === null) {
$mobileDeviceManager = new MobileDeviceManager();
$mobileDevice = $mobileDeviceManager->loginOrCreate($this->username, $this->password);
/** @var Customer */
$this->customer = Customer::find()->andWhere([
'id_customer_card' => $mobileDevice->id_card
])->one();
if ($this->customer == null) {
throw new NotFoundHttpException();
}
}
return $this->customer;
return $this->customer;
}
}