mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
Configuracion inicial para usar atentificacion via JWT (requerido tocar el composer para firebase/jwt
This commit is contained in:
78
ci4/app/Controllers/Sistema/AuthAPIController.php
Normal file
78
ci4/app/Controllers/Sistema/AuthAPIController.php
Normal file
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\Sistema;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use CodeIgniter\API\ResponseTrait;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use CodeIgniter\Shield\Authentication\Authenticators\Session;
|
||||
use CodeIgniter\Shield\Authentication\JWTManager;
|
||||
use CodeIgniter\Shield\Validation\ValidationRules;
|
||||
|
||||
class AuthAPIController extends BaseController
|
||||
{
|
||||
use ResponseTrait;
|
||||
|
||||
/**
|
||||
* Authenticate Existing User and Issue JWT.
|
||||
*/
|
||||
public function jwtLogin(): ResponseInterface
|
||||
{
|
||||
// Get the validation rules
|
||||
$rules = $this->getValidationRules();
|
||||
|
||||
// Validate credentials
|
||||
if (! $this->validateData($this->request->getJSON(true), $rules, [], config('Auth')->DBGroup)) {
|
||||
return $this->fail(
|
||||
['errors' => $this->validator->getErrors()],
|
||||
$this->codes['unauthorized']
|
||||
);
|
||||
}
|
||||
|
||||
// Get the credentials for login
|
||||
$credentials = $this->request->getJsonVar(setting('Auth.validFields'));
|
||||
$credentials = array_filter($credentials);
|
||||
$credentials['password'] = $this->request->getJsonVar('password');
|
||||
|
||||
/** @var Session $authenticator */
|
||||
$authenticator = auth('session')->getAuthenticator();
|
||||
|
||||
// Check the credentials
|
||||
$result = $authenticator->check($credentials);
|
||||
|
||||
// Credentials mismatch.
|
||||
if (! $result->isOK()) {
|
||||
// @TODO Record a failed login attempt
|
||||
|
||||
return $this->failUnauthorized($result->reason());
|
||||
}
|
||||
|
||||
// Credentials match.
|
||||
// @TODO Record a successful login attempt
|
||||
|
||||
$user = $result->extraInfo();
|
||||
|
||||
/** @var JWTManager $manager */
|
||||
$manager = service('jwtmanager');
|
||||
|
||||
// Generate JWT and return to client
|
||||
$jwt = $manager->generateToken($user);
|
||||
|
||||
return $this->respond([
|
||||
'access_token' => $jwt,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the rules that should be used for validation.
|
||||
*
|
||||
* @return array<string, array<string, array<string>|string>>
|
||||
* @phpstan-return array<string, array<string, string|list<string>>>
|
||||
*/
|
||||
protected function getValidationRules(): array
|
||||
{
|
||||
$rules = new ValidationRules();
|
||||
|
||||
return $rules->getLoginRules();
|
||||
}
|
||||
}
|
||||
@ -18,6 +18,12 @@ class Test extends BaseController
|
||||
{
|
||||
}
|
||||
|
||||
public function echo(){
|
||||
|
||||
echo "echo";
|
||||
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user