Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow additional login params, Introduce LogInValidator #3670

Merged
merged 4 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = [];
}