mirror of
https://github.com/flarum/core.git
synced 2025-07-26 03:01:22 +02:00
Update TokenAction for new architecture
This commit is contained in:
@@ -1,15 +1,17 @@
|
|||||||
<?php namespace Flarum\Api\Actions;
|
<?php namespace Flarum\Api\Actions;
|
||||||
|
|
||||||
use Illuminate\Http\JsonResponse;
|
use Flarum\Api\Request;
|
||||||
use Illuminate\Contracts\Bus\Dispatcher;
|
|
||||||
use Flarum\Core\Commands\GenerateAccessTokenCommand;
|
use Flarum\Core\Commands\GenerateAccessTokenCommand;
|
||||||
use Flarum\Core\Repositories\UserRepositoryInterface;
|
use Flarum\Core\Repositories\UserRepositoryInterface;
|
||||||
use Flarum\Api\Actions\BaseAction;
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Contracts\Bus\Dispatcher;
|
||||||
|
|
||||||
class TokenAction extends BaseAction
|
class TokenAction implements ActionInterface
|
||||||
{
|
{
|
||||||
protected $users;
|
protected $users;
|
||||||
|
|
||||||
|
protected $bus;
|
||||||
|
|
||||||
public function __construct(UserRepositoryInterface $users, Dispatcher $bus)
|
public function __construct(UserRepositoryInterface $users, Dispatcher $bus)
|
||||||
{
|
{
|
||||||
$this->users = $users;
|
$this->users = $users;
|
||||||
@@ -19,21 +21,25 @@ class TokenAction extends BaseAction
|
|||||||
/**
|
/**
|
||||||
* Log in and return a token.
|
* Log in and return a token.
|
||||||
*
|
*
|
||||||
* @return Response
|
* @param \Flarum\Api\Request $request
|
||||||
|
* @return \Flarum\Api\Response
|
||||||
*/
|
*/
|
||||||
public function run(ApiParams $params)
|
public function handle(Request $request)
|
||||||
{
|
{
|
||||||
$identification = $params->get('identification');
|
$identification = $request->get('identification');
|
||||||
$password = $params->get('password');
|
$password = $request->get('password');
|
||||||
|
|
||||||
$user = $this->users->findByIdentification($identification);
|
$user = $this->users->findByIdentification($identification);
|
||||||
|
|
||||||
if (! $user || ! $user->checkPassword($password)) {
|
if (! $user || ! $user->checkPassword($password)) {
|
||||||
return $this->respondWithError('invalidCredentials', 401);
|
return;
|
||||||
|
// throw an exception
|
||||||
|
// return $this->respondWithError('invalidCredentials', 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
$command = new GenerateAccessTokenCommand($user->id);
|
$token = $this->bus->dispatch(
|
||||||
$token = $this->dispatch($command, $params);
|
new GenerateAccessTokenCommand($user->id)
|
||||||
|
);
|
||||||
|
|
||||||
return new JsonResponse([
|
return new JsonResponse([
|
||||||
'token' => $token->id,
|
'token' => $token->id,
|
||||||
|
@@ -11,7 +11,7 @@ class Request
|
|||||||
|
|
||||||
public $http;
|
public $http;
|
||||||
|
|
||||||
public function __construct(array $input, Actor $actor, IlluminateRequest $http = null)
|
public function __construct(array $input, Actor $actor = null, IlluminateRequest $http = null)
|
||||||
{
|
{
|
||||||
$this->input = $input;
|
$this->input = $input;
|
||||||
$this->actor = $actor;
|
$this->actor = $actor;
|
||||||
|
@@ -3,6 +3,7 @@
|
|||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Flarum\Forum\Events\UserLoggedIn;
|
use Flarum\Forum\Events\UserLoggedIn;
|
||||||
use Flarum\Core\Repositories\UserRepositoryInterface;
|
use Flarum\Core\Repositories\UserRepositoryInterface;
|
||||||
|
use Flarum\Api\Request as ApiRequest;
|
||||||
|
|
||||||
class LoginAction extends BaseAction
|
class LoginAction extends BaseAction
|
||||||
{
|
{
|
||||||
@@ -17,7 +18,8 @@ class LoginAction extends BaseAction
|
|||||||
|
|
||||||
public function handle(Request $request, $routeParams = [])
|
public function handle(Request $request, $routeParams = [])
|
||||||
{
|
{
|
||||||
$response = $this->callAction('Flarum\Api\Actions\TokenAction', $request->only('identification', 'password'));
|
$response = app('Flarum\Api\Actions\TokenAction')
|
||||||
|
->handle(new ApiRequest($request->only('identification', 'password')));
|
||||||
|
|
||||||
$data = $response->getData();
|
$data = $response->getData();
|
||||||
if (! empty($data->token)) {
|
if (! empty($data->token)) {
|
||||||
|
Reference in New Issue
Block a user