1
0
mirror of https://github.com/flarum/core.git synced 2025-07-17 14:51:19 +02:00

feat: Allow additional login params, Introduce LogInValidator (#3670)

* Allow additional login params, dispatch 'LoggingIn' event

* Update framework/core/js/src/forum/components/LogInModal.tsx

Co-authored-by: David Wheatley <hi@davwheat.dev>

* Introduce 'LogInValidator'

* Apply fixes from StyleCI

Co-authored-by: David Wheatley <hi@davwheat.dev>
Co-authored-by: StyleCI Bot <bot@styleci.io>
This commit is contained in:
Ian Morland
2022-11-07 13:47:04 +00:00
committed by GitHub
parent 62a396e434
commit 53ab1503e4
3 changed files with 42 additions and 7 deletions

View File

@@ -9,6 +9,7 @@ import ItemList from '../../common/utils/ItemList';
import Stream from '../../common/utils/Stream'; import Stream from '../../common/utils/Stream';
import type Mithril from 'mithril'; import type Mithril from 'mithril';
import RequestError from '../../common/utils/RequestError'; import RequestError from '../../common/utils/RequestError';
import type { LoginParams } from '../../common/Session';
export interface ILoginModalAttrs extends IInternalModalAttrs { export interface ILoginModalAttrs extends IInternalModalAttrs {
identification?: string; identification?: string;
@@ -172,13 +173,17 @@ export default class LogInModal<CustomAttrs extends ILoginModalAttrs = ILoginMod
this.loading = true; this.loading = true;
const identification = this.identification(); app.session.login(this.loginParams(), { errorHandler: this.onerror.bind(this) }).then(() => window.location.reload(), this.loaded.bind(this));
const password = this.password(); }
const remember = this.remember();
app.session loginParams(): LoginParams {
.login({ identification, password, remember }, { errorHandler: this.onerror.bind(this) }) const data = {
.then(() => window.location.reload(), this.loaded.bind(this)); identification: this.identification(),
password: this.password(),
remember: this.remember(),
};
return data;
} }
onerror(error: RequestError) { onerror(error: RequestError) {

View File

@@ -10,6 +10,7 @@
namespace Flarum\Forum\Controller; namespace Flarum\Forum\Controller;
use Flarum\Api\Client; use Flarum\Api\Client;
use Flarum\Forum\LogInValidator;
use Flarum\Http\AccessToken; use Flarum\Http\AccessToken;
use Flarum\Http\RememberAccessToken; use Flarum\Http\RememberAccessToken;
use Flarum\Http\Rememberer; use Flarum\Http\Rememberer;
@@ -49,19 +50,26 @@ class LogInController implements RequestHandlerInterface
*/ */
protected $rememberer; protected $rememberer;
/**
* @var LogInValidator
*/
protected $validator;
/** /**
* @param \Flarum\User\UserRepository $users * @param \Flarum\User\UserRepository $users
* @param Client $apiClient * @param Client $apiClient
* @param SessionAuthenticator $authenticator * @param SessionAuthenticator $authenticator
* @param Rememberer $rememberer * @param Rememberer $rememberer
* @param LogInValidator $validator
*/ */
public function __construct(UserRepository $users, Client $apiClient, SessionAuthenticator $authenticator, Dispatcher $events, Rememberer $rememberer) public function __construct(UserRepository $users, Client $apiClient, SessionAuthenticator $authenticator, Dispatcher $events, Rememberer $rememberer, LogInValidator $validator)
{ {
$this->users = $users; $this->users = $users;
$this->apiClient = $apiClient; $this->apiClient = $apiClient;
$this->authenticator = $authenticator; $this->authenticator = $authenticator;
$this->events = $events; $this->events = $events;
$this->rememberer = $rememberer; $this->rememberer = $rememberer;
$this->validator = $validator;
} }
/** /**
@@ -72,6 +80,8 @@ class LogInController implements RequestHandlerInterface
$body = $request->getParsedBody(); $body = $request->getParsedBody();
$params = Arr::only($body, ['identification', 'password', 'remember']); $params = Arr::only($body, ['identification', 'password', 'remember']);
$this->validator->assertValid($body);
$response = $this->apiClient->withParentRequest($request)->withBody($params)->post('/token'); $response = $this->apiClient->withParentRequest($request)->withBody($params)->post('/token');
if ($response->getStatusCode() === 200) { if ($response->getStatusCode() === 200) {

View File

@@ -0,0 +1,20 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Forum;
use Flarum\Foundation\AbstractValidator;
class LogInValidator extends AbstractValidator
{
/**
* {@inheritdoc}
*/
protected $rules = [];
}