fitness-web/mobileapi/controllers/LoginController.php
2023-05-20 22:27:27 +02:00

75 lines
2.2 KiB
PHP

<?php
/**
* Created by IntelliJ IDEA.
* User: rocho
* Date: 2018.08.29.
* Time: 21:58
*/
namespace mobileapi\controllers;
use common\models\Card;
use mobileapi\models\LoginForm;
use sizeg\jwt\Jwt;
use Yii;
use yii\web\BadRequestHttpException;
use yii\web\UnauthorizedHttpException;
/** @noinspection PhpUnused */
class LoginController extends RestController
{
/**
* hash for password test is:
*
*/
/** @noinspection PhpUnused */
public function actionLogin()
{
$form = new LoginForm();
$form->load(\Yii::$app->request->post(), '');
if (!$form->validate()) {
throw new BadRequestHttpException("Hibás bejelentkezés " . print_r($form->getErrors( ),true));
}
/** @var Jwt $jwt */
$jwt = Yii::$app->jwt;
$signer = $jwt->getSigner('HS256');
$key = $jwt->getKey();
$time = time();
$validFor = 60 * 60 * 24 * 7 * 4; // 4 weeks
// Adoption for lcobucci/jwt ^4.0 version
$token = $jwt->getBuilder()
->issuedBy('mobileapi')// Configures the issuer (iss claim)
->permittedFor('customer')// Configures the audience (aud claim)
->identifiedBy('A989C57D19E2AF756BA9585AC4CFAF7974AE3D2BCA7CCA7307B39AB28CC7C2C8', true)// Configures the id (jti claim), replicating as a header item
->issuedAt($time)// Configures the time that the token was issue (iat claim)
// ->expiresAt($time + $validFor)// Configures the expiration time of the token (exp claim)
->withClaim('uid', $form->getMobileDevice()->getId())// Configures a new claim, called "uid"
->withClaim('cardId', $form->getMobileDevice()->card->number)// Configures a new claim, called "uid"
->getToken($signer, $key); // Retrieves the generated token
return $this->asJson([
'token' => (string)$token,
]);
}
protected function getOptionalActions()
{
// user must not be logged in to call this actions
return ['login'];
}
protected function getOptionalActivatedActions()
{
// user must not be activated to call this actions
return ['login'];
}
}