Remember checkbox (#1075)

* Add session option to Rememberer class

* Update session login function to allow send additional data

* Add Remember me checkbox

* Cleanup login modal
This commit is contained in:
Sajjad Hashemian 2016-11-29 11:02:12 +03:30 committed by Toby Zerner
parent 7af4b8d45f
commit 06c32b668d
7 changed files with 77 additions and 45 deletions

View File

@ -22552,13 +22552,13 @@ System.register('flarum/Session', [], function (_export, _context) {
babelHelpers.createClass(Session, [{
key: 'login',
value: function login(identification, password) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
value: function login(data) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return app.request(babelHelpers.extends({
method: 'POST',
url: app.forum.attribute('baseUrl') + '/login',
data: { identification: identification, password: password }
data: data
}, options));
}
}, {

42
js/forum/dist/app.js vendored
View File

@ -22983,6 +22983,12 @@ System.register('flarum/components/ForgotPasswordModal', ['flarum/components/Mod
onchange: m.withAttr('value', this.email),
disabled: this.loading })
),
m(
'label',
{ className: 'checkbox' },
m('input', { name: 'remember', type: 'checkbox', bidi: this.remember, disabled: this.loading }),
app.translator.trans('core.forum.log_in.remember_me_text')
),
m(
'div',
{ className: 'Form-group' },
@ -23876,11 +23882,11 @@ System.register('flarum/components/LogInModal', ['flarum/components/Modal', 'fla
babelHelpers.get(LogInModal.prototype.__proto__ || Object.getPrototypeOf(LogInModal.prototype), 'init', this).call(this);
/**
* The value of the email input.
* The value of the identification input.
*
* @type {Function}
*/
this.email = m.prop(this.props.email || '');
this.identification = m.prop(this.props.identification || '');
/**
* The value of the password input.
@ -23888,6 +23894,13 @@ System.register('flarum/components/LogInModal', ['flarum/components/Modal', 'fla
* @type {Function}
*/
this.password = m.prop(this.props.password || '');
/**
* The value of the remember me input.
*
* @type {Function}
*/
this.remember = m.prop(this.props.remember && true);
}
}, {
key: 'className',
@ -23912,8 +23925,8 @@ System.register('flarum/components/LogInModal', ['flarum/components/Modal', 'fla
m(
'div',
{ className: 'Form-group' },
m('input', { className: 'FormControl', name: 'email', type: 'text', placeholder: extractText(app.translator.trans('core.forum.log_in.username_or_email_placeholder')),
bidi: this.email,
m('input', { className: 'FormControl', name: 'identification', type: 'text', placeholder: extractText(app.translator.trans('core.forum.log_in.username_or_email_placeholder')),
bidi: this.identification,
disabled: this.loading })
),
m(
@ -23956,7 +23969,7 @@ System.register('flarum/components/LogInModal', ['flarum/components/Modal', 'fla
}, {
key: 'forgotPassword',
value: function forgotPassword() {
var email = this.email();
var email = this.identification();
var props = email.indexOf('@') !== -1 ? { email: email } : undefined;
app.modal.show(new ForgotPasswordModal(props));
@ -23965,15 +23978,15 @@ System.register('flarum/components/LogInModal', ['flarum/components/Modal', 'fla
key: 'signUp',
value: function signUp() {
var props = { password: this.password() };
var email = this.email();
props[email.indexOf('@') !== -1 ? 'email' : 'username'] = email;
var identification = this.identification();
props[identification.indexOf('@') !== -1 ? 'email' : 'username'] = identification;
app.modal.show(new SignUpModal(props));
}
}, {
key: 'onready',
value: function onready() {
this.$('[name=' + (this.email() ? 'password' : 'email') + ']').select();
this.$('[name=' + (this.identification() ? 'password' : 'identification') + ']').select();
}
}, {
key: 'onsubmit',
@ -23982,10 +23995,11 @@ System.register('flarum/components/LogInModal', ['flarum/components/Modal', 'fla
this.loading = true;
var email = this.email();
var identification = this.identification();
var password = this.password();
var remember = this.remember();
app.session.login(email, password, { errorHandler: this.onerror.bind(this) }).then(function () {
app.session.login({ identification: identification, password: password, remember: remember }, { errorHandler: this.onerror.bind(this) }).then(function () {
return window.location.reload();
}, this.loaded.bind(this));
}
@ -27947,7 +27961,7 @@ System.register('flarum/components/SignUpModal', ['flarum/components/Modal', 'fl
key: 'logIn',
value: function logIn() {
var props = {
email: this.email() || this.username(),
identification: this.email() || this.username(),
password: this.password()
};
@ -30494,13 +30508,13 @@ System.register('flarum/Session', [], function (_export, _context) {
babelHelpers.createClass(Session, [{
key: 'login',
value: function login(identification, password) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
value: function login(data) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return app.request(babelHelpers.extends({
method: 'POST',
url: app.forum.attribute('baseUrl') + '/login',
data: { identification: identification, password: password }
data: data
}, options));
}
}, {

View File

@ -11,7 +11,7 @@ import extractText from 'flarum/utils/extractText';
*
* ### Props
*
* - `email`
* - `identification`
* - `password`
*/
export default class LogInModal extends Modal {
@ -19,11 +19,11 @@ export default class LogInModal extends Modal {
super.init();
/**
* The value of the email input.
* The value of the identification input.
*
* @type {Function}
*/
this.email = m.prop(this.props.email || '');
this.identification = m.prop(this.props.identification || '');
/**
* The value of the password input.
@ -31,6 +31,13 @@ export default class LogInModal extends Modal {
* @type {Function}
*/
this.password = m.prop(this.props.password || '');
/**
* The value of the remember me input.
*
* @type {Function}
*/
this.remember = m.prop(this.props.remember && true);
}
className() {
@ -48,8 +55,8 @@ export default class LogInModal extends Modal {
<div className="Form Form--centered">
<div className="Form-group">
<input className="FormControl" name="email" type="text" placeholder={extractText(app.translator.trans('core.forum.log_in.username_or_email_placeholder'))}
bidi={this.email}
<input className="FormControl" name="identification" type="text" placeholder={extractText(app.translator.trans('core.forum.log_in.username_or_email_placeholder'))}
bidi={this.identification}
disabled={this.loading} />
</div>
@ -59,6 +66,11 @@ export default class LogInModal extends Modal {
disabled={this.loading} />
</div>
<label className="checkbox">
<input name="remember" type="checkbox" bidi={this.remember} disabled={this.loading} />
{app.translator.trans('core.forum.log_in.remember_me_label')}
</label>
<div className="Form-group">
{Button.component({
className: 'Button Button--primary Button--block',
@ -90,7 +102,7 @@ export default class LogInModal extends Modal {
* @public
*/
forgotPassword() {
const email = this.email();
const email = this.identification();
const props = email.indexOf('@') !== -1 ? {email} : undefined;
app.modal.show(new ForgotPasswordModal(props));
@ -104,14 +116,14 @@ export default class LogInModal extends Modal {
*/
signUp() {
const props = {password: this.password()};
const email = this.email();
props[email.indexOf('@') !== -1 ? 'email' : 'username'] = email;
const identification = this.identification();
props[identification.indexOf('@') !== -1 ? 'email' : 'username'] = identification;
app.modal.show(new SignUpModal(props));
}
onready() {
this.$('[name=' + (this.email() ? 'password' : 'email') + ']').select();
this.$('[name=' + (this.identification() ? 'password' : 'identification') + ']').select();
}
onsubmit(e) {
@ -119,13 +131,15 @@ export default class LogInModal extends Modal {
this.loading = true;
const email = this.email();
const identification = this.identification();
const password = this.password();
const remember = this.remember();
app.session.login(email, password, {errorHandler: this.onerror.bind(this)}).then(
() => window.location.reload(),
this.loaded.bind(this)
);
app.session.login({identification, password, remember}, {errorHandler: this.onerror.bind(this)})
.then(
() => window.location.reload(),
this.loaded.bind(this)
);
}
onerror(error) {

View File

@ -116,7 +116,7 @@ export default class SignUpModal extends Modal {
*/
logIn() {
const props = {
email: this.email() || this.username(),
identification: this.email() || this.username(),
password: this.password()
};

View File

@ -30,11 +30,11 @@ export default class Session {
* @return {Promise}
* @public
*/
login(identification, password, options = {}) {
login(data, options = {}) {
return app.request(Object.assign({
method: 'POST',
url: app.forum.attribute('baseUrl') + '/login',
data: {identification, password}
data
}, options));
}

View File

@ -66,7 +66,8 @@ class LogInController implements ControllerInterface
public function handle(Request $request)
{
$actor = $request->getAttribute('actor');
$params = array_only($request->getParsedBody(), ['identification', 'password']);
$body = $request->getParsedBody();
$params = array_only($body, ['identification', 'password']);
$response = $this->apiClient->send(TokenController::class, $actor, [], $params);
@ -80,7 +81,7 @@ class LogInController implements ControllerInterface
event(new UserLoggedIn($this->users->findOrFail($data->userId), $token));
$response = $this->rememberer->remember($response, $token);
$response = $this->rememberer->remember($response, $token, ! array_get($body, 'remember'));
}
return $response;

View File

@ -19,17 +19,20 @@ class Rememberer
{
protected $cookieName = 'flarum_remember';
public function remember(ResponseInterface $response, AccessToken $token)
public function remember(ResponseInterface $response, AccessToken $token, $session = false)
{
$token->lifetime = 60 * 60 * 24 * 14;
$token->save();
$cookie = $this->createCookie()->withValue($token->id);
return FigResponseCookies::set(
$response,
$this->createCookie()
->withValue($token->id)
->withMaxAge(14 * 24 * 60 * 60)
);
if (! $session) {
$lifetime = 60 * 60 * 24 * 14;
$token->lifetime = $lifetime;
$token->save();
$cookie = $cookie->withMaxAge($lifetime);
}
return FigResponseCookies::set($response, $cookie);
}
public function rememberUser(ResponseInterface $response, $userId)