move login action to LoginController.php

This commit is contained in:
Roland Schneider 2021-09-28 18:32:48 +02:00
parent 83ed1bd24f
commit 858f7b03fc
3 changed files with 61 additions and 54 deletions

View File

@ -6,7 +6,7 @@ export class Endpoints {
private static apiUrl: string = environment.apiUrl; private static apiUrl: string = environment.apiUrl;
public static POST_USERS_AUTHENTICATE(){ public static POST_USERS_AUTHENTICATE(){
return `${this.apiUrl}user/login`; return `${this.apiUrl}login/login`;
} }
public static POST_USER_PASSWORD_CHANGE(){ public static POST_USER_PASSWORD_CHANGE(){

View File

@ -0,0 +1,60 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: rocho
* Date: 2018.08.29.
* Time: 21:58
*/
namespace customerapi\controllers;
use customerapi\models\LoginForm;
use sizeg\jwt\Jwt;
use Yii;
use yii\rest\Controller;
use yii\web\UnauthorizedHttpException;
/** @noinspection PhpUnused */
class LoginController extends Controller
{
/**
* hash for password test is:
* $2y$13$D2BauYE2nhCdVDNatT9BMeWGxOvi5t5V6W2OUjr6sj2FRpb317Cpq
*
*/
/** @noinspection PhpUnused */
public function actionLogin()
{
$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 {
throw new UnauthorizedHttpException("Hibás e-mail cím vagy jelszó!");
}
}
}

View File

@ -9,61 +9,14 @@
namespace customerapi\controllers; namespace customerapi\controllers;
use common\models\Customer; use common\models\Customer;
use customerapi\models\LoginForm;
use customerapi\models\PasswordChangeForm; use customerapi\models\PasswordChangeForm;
use sizeg\jwt\Jwt;
use Yii;
use yii\web\BadRequestHttpException; use yii\web\BadRequestHttpException;
use yii\web\UnauthorizedHttpException;
/** @noinspection PhpUnused */ /** @noinspection PhpUnused */
class UserController extends RestController class UserController extends RestController
{ {
/**
* hash for password test is:
* $2y$13$D2BauYE2nhCdVDNatT9BMeWGxOvi5t5V6W2OUjr6sj2FRpb317Cpq
*
*/
/** @noinspection PhpUnused */
public function actionLogin()
{
// $customer = new Customer();
// $customer->setPassword("test");
$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 {
throw new UnauthorizedHttpException("Hibás e-mail cím vagy jelszó!");
}
}
/** /**
* @throws \yii\base\InvalidConfigException * @throws \yii\base\InvalidConfigException
* @throws \yii\base\Exception * @throws \yii\base\Exception
@ -91,10 +44,4 @@ class UserController extends RestController
$customer->save(); $customer->save();
} }
protected function getOptionalActions()
{
return ['login'];
}
} }