mirror of
https://github.com/flarum/framework.git
synced 2025-02-21 11:37:56 +08:00
Update TokenAction for new architecture
This commit is contained in:
parent
c43d1c6335
commit
2e80cbd8a7
@ -1,15 +1,17 @@
|
||||
<?php namespace Flarum\Api\Actions;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Contracts\Bus\Dispatcher;
|
||||
use Flarum\Api\Request;
|
||||
use Flarum\Core\Commands\GenerateAccessTokenCommand;
|
||||
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 $bus;
|
||||
|
||||
public function __construct(UserRepositoryInterface $users, Dispatcher $bus)
|
||||
{
|
||||
$this->users = $users;
|
||||
@ -19,21 +21,25 @@ class TokenAction extends BaseAction
|
||||
/**
|
||||
* 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');
|
||||
$password = $params->get('password');
|
||||
$identification = $request->get('identification');
|
||||
$password = $request->get('password');
|
||||
|
||||
$user = $this->users->findByIdentification($identification);
|
||||
|
||||
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->dispatch($command, $params);
|
||||
$token = $this->bus->dispatch(
|
||||
new GenerateAccessTokenCommand($user->id)
|
||||
);
|
||||
|
||||
return new JsonResponse([
|
||||
'token' => $token->id,
|
||||
|
@ -11,7 +11,7 @@ class Request
|
||||
|
||||
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->actor = $actor;
|
||||
|
@ -3,6 +3,7 @@
|
||||
use Illuminate\Http\Request;
|
||||
use Flarum\Forum\Events\UserLoggedIn;
|
||||
use Flarum\Core\Repositories\UserRepositoryInterface;
|
||||
use Flarum\Api\Request as ApiRequest;
|
||||
|
||||
class LoginAction extends BaseAction
|
||||
{
|
||||
@ -17,7 +18,8 @@ class LoginAction extends BaseAction
|
||||
|
||||
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();
|
||||
if (! empty($data->token)) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user