From 06c32b668d87e88eddd928b470299e308b46fa42 Mon Sep 17 00:00:00 2001 From: Sajjad Hashemian Date: Tue, 29 Nov 2016 11:02:12 +0330 Subject: [PATCH] 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 --- js/admin/dist/app.js | 6 ++-- js/forum/dist/app.js | 42 ++++++++++++++++-------- js/forum/src/components/LogInModal.js | 42 ++++++++++++++++-------- js/forum/src/components/SignUpModal.js | 2 +- js/lib/Session.js | 4 +-- src/Forum/Controller/LogInController.php | 5 +-- src/Http/Rememberer.php | 21 +++++++----- 7 files changed, 77 insertions(+), 45 deletions(-) diff --git a/js/admin/dist/app.js b/js/admin/dist/app.js index 62ca44abf..a7567a058 100644 --- a/js/admin/dist/app.js +++ b/js/admin/dist/app.js @@ -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)); } }, { diff --git a/js/forum/dist/app.js b/js/forum/dist/app.js index 1e7d517b6..5d1184ef4 100644 --- a/js/forum/dist/app.js +++ b/js/forum/dist/app.js @@ -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)); } }, { diff --git a/js/forum/src/components/LogInModal.js b/js/forum/src/components/LogInModal.js index b5fd805a1..eb9b361c3 100644 --- a/js/forum/src/components/LogInModal.js +++ b/js/forum/src/components/LogInModal.js @@ -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 {
-
@@ -59,6 +66,11 @@ export default class LogInModal extends Modal { disabled={this.loading} />
+ +
{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) { diff --git a/js/forum/src/components/SignUpModal.js b/js/forum/src/components/SignUpModal.js index a3eac5771..ed0c1362e 100644 --- a/js/forum/src/components/SignUpModal.js +++ b/js/forum/src/components/SignUpModal.js @@ -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() }; diff --git a/js/lib/Session.js b/js/lib/Session.js index 19f900fda..aa5630852 100644 --- a/js/lib/Session.js +++ b/js/lib/Session.js @@ -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)); } diff --git a/src/Forum/Controller/LogInController.php b/src/Forum/Controller/LogInController.php index 7cf454dc9..f131150f9 100644 --- a/src/Forum/Controller/LogInController.php +++ b/src/Forum/Controller/LogInController.php @@ -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; diff --git a/src/Http/Rememberer.php b/src/Http/Rememberer.php index 1290355d0..4d664b0eb 100644 --- a/src/Http/Rememberer.php +++ b/src/Http/Rememberer.php @@ -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)