Skip to content

Commit

Permalink
feat: Allow additional login params, Introduce LogInValidator (#3670)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Nov 7, 2022
1 parent 62a396e commit 53ab150
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
17 changes: 11 additions & 6 deletions framework/core/js/src/forum/components/LogInModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ItemList from '../../common/utils/ItemList';
import Stream from '../../common/utils/Stream';
import type Mithril from 'mithril';
import RequestError from '../../common/utils/RequestError';
import type { LoginParams } from '../../common/Session';

export interface ILoginModalAttrs extends IInternalModalAttrs {
identification?: string;
Expand Down Expand Up @@ -172,13 +173,17 @@ export default class LogInModal<CustomAttrs extends ILoginModalAttrs = ILoginMod

this.loading = true;

const identification = this.identification();
const password = this.password();
const remember = this.remember();
app.session.login(this.loginParams(), { errorHandler: this.onerror.bind(this) }).then(() => window.location.reload(), this.loaded.bind(this));
}

loginParams(): LoginParams {
const data = {
identification: this.identification(),
password: this.password(),
remember: this.remember(),
};

app.session
.login({ identification, password, remember }, { errorHandler: this.onerror.bind(this) })
.then(() => window.location.reload(), this.loaded.bind(this));
return data;
}

onerror(error: RequestError) {
Expand Down
12 changes: 11 additions & 1 deletion framework/core/src/Forum/Controller/LogInController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Flarum\Forum\Controller;

use Flarum\Api\Client;
use Flarum\Forum\LogInValidator;
use Flarum\Http\AccessToken;
use Flarum\Http\RememberAccessToken;
use Flarum\Http\Rememberer;
Expand Down Expand Up @@ -49,19 +50,26 @@ class LogInController implements RequestHandlerInterface
*/
protected $rememberer;

/**
* @var LogInValidator
*/
protected $validator;

/**
* @param \Flarum\User\UserRepository $users
* @param Client $apiClient
* @param SessionAuthenticator $authenticator
* @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->apiClient = $apiClient;
$this->authenticator = $authenticator;
$this->events = $events;
$this->rememberer = $rememberer;
$this->validator = $validator;
}

/**
Expand All @@ -72,6 +80,8 @@ public function handle(Request $request): ResponseInterface
$body = $request->getParsedBody();
$params = Arr::only($body, ['identification', 'password', 'remember']);

$this->validator->assertValid($body);

$response = $this->apiClient->withParentRequest($request)->withBody($params)->post('/token');

if ($response->getStatusCode() === 200) {
Expand Down
20 changes: 20 additions & 0 deletions framework/core/src/Forum/LogInValidator.php
Original file line number Diff line number Diff line change
@@ -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 = [];
}

0 comments on commit 53ab150

Please sign in to comment.