add customer api

This commit is contained in:
2019-10-08 22:33:25 +02:00
committed by Roland Schneider
parent 9aee187d11
commit 1300bfc752
33 changed files with 1164 additions and 246 deletions

View File

@@ -8,21 +8,61 @@
namespace customerapi\controllers;
use common\models\Customer;
use customerapi\models\LoginForm;
use sizeg\jwt\Jwt;
use sizeg\jwt\JwtHttpBearerAuth;
use Yii;
use common\components\Helper;
use common\models\Card;
use common\models\Ticket;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
/** @noinspection PhpUnused */
class UserController extends RestController
{
/**
*/
/** @noinspection PhpUnused */
public function actionLogin( )
{
\Yii::$app->response->setStatusCode(204);
$form = new LoginForm();
$form->load(\Yii::$app->request->post( ), '');
if ( $form->validate() ){
/** @var Jwt $jwt */
$jwt = Yii::$app->jwt;
$signer = $jwt->getSigner('HS256');
$key = $jwt->getKey();
$time = time();
// Adoption for lcobucci/jwt ^4.0 version
$token = $jwt->getBuilder()
->issuedBy('customerapi')// 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 + 3600)// Configures the expiration time of the token (exp claim)
->withClaim('uid', $form->getCustomer()->getId())// Configures a new claim, called "uid"
->getToken($signer, $key); // Retrieves the generated token
return $this->asJson([
'token' => (string)$token,
]);
} else {
return $this->asJson(
[
'errors' => $form->getErrors()
]
);
}
}
protected function getOptionalActions()
{
return ['login'];
}