Improvements to change/forgot password

This commit is contained in:
Toby Zerner 2015-05-27 16:25:44 +09:30
parent b6a8416daf
commit 87f84f0614
16 changed files with 96 additions and 34 deletions

View File

@ -8,7 +8,7 @@ export default class ChangePasswordModal extends FormModal {
body: [ body: [
m('p.help-text', 'Click the button below and check your email for a link to change your password.'), m('p.help-text', 'Click the button below and check your email for a link to change your password.'),
m('div.form-group', [ m('div.form-group', [
m('button.btn.btn-primary.btn-block[type=submit]', 'Send Password Reset Email') m('button.btn.btn-primary.btn-block[type=submit]', {disabled: this.loading()}, 'Send Password Reset Email')
]) ])
] ]
}); });

View File

@ -21,13 +21,13 @@ export default class ForgotPasswordModal extends FormModal {
title: 'Forgot Password', title: 'Forgot Password',
body: this.success() body: this.success()
? [ ? [
m('p.help-text', 'OK, we\'ve sent you an email containing a link to reset your password. Check your spam folder if you don\'t receive it within the next minute or two. Yeah, sometimes we get put through to spam - can you believe it?!'), m('p.help-text', 'We\'ve sent you an email containing a link to reset your password. Check your spam folder if you don\'t receive it within the next minute or two.'),
m('div.form-group', [ m('div.form-group', [
m('a.btn.btn-primary.btn-block', {href: 'http://'+emailProviderName}, 'Go to '+emailProviderName) m('a.btn.btn-primary.btn-block', {href: 'http://'+emailProviderName}, 'Go to '+emailProviderName)
]) ])
] ]
: [ : [
m('p.help-text', 'Forgot your password? Don\'t worry, it happens all the time. Simply enter your email address and we\'ll send you instructions on how to set up a new one.'), m('p.help-text', 'Enter your email address and we\'ll send you a link to reset your password.'),
m('div.form-group', [ m('div.form-group', [
m('input.form-control[name=email][placeholder=Email]', {value: this.email(), onchange: m.withAttr('value', this.email), disabled: this.loading()}) m('input.form-control[name=email][placeholder=Email]', {value: this.email(), onchange: m.withAttr('value', this.email), disabled: this.loading()})
]), ]),
@ -57,12 +57,11 @@ export default class ForgotPasswordModal extends FormModal {
}).then(response => { }).then(response => {
this.loading(false); this.loading(false);
this.success(true); this.success(true);
this.alert = null; this.alert(null);
m.redraw(); m.redraw();
}, response => { }, response => {
this.loading(false); this.loading(false);
m.redraw(); this.handleErrors(response.errors);
this.ready();
}); });
} }
} }

View File

@ -7,13 +7,14 @@ export default class FormModal extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.alert = null; this.alert = m.prop();
this.loading = m.prop(false); this.loading = m.prop(false);
} }
view(options) { view(options) {
if (this.alert) { var alert = this.alert();
this.alert.props.dismissible = false; if (alert) {
alert.props.dismissible = false;
} }
return m('div.modal-dialog', {className: options.className, config: this.element}, [ return m('div.modal-dialog', {className: options.className, config: this.element}, [
@ -21,7 +22,7 @@ export default class FormModal extends Component {
m('a[href=javascript:;].btn.btn-icon.btn-link.close.back-control', {onclick: this.hide.bind(this)}, icon('times')), m('a[href=javascript:;].btn.btn-icon.btn-link.close.back-control', {onclick: this.hide.bind(this)}, icon('times')),
m('form', {onsubmit: this.onsubmit.bind(this)}, [ m('form', {onsubmit: this.onsubmit.bind(this)}, [
m('div.modal-header', m('h3.title-control', options.title)), m('div.modal-header', m('h3.title-control', options.title)),
this.alert ? m('div.modal-alert', this.alert.view()) : '', alert ? m('div.modal-alert', alert) : '',
m('div.modal-body', [ m('div.modal-body', [
m('div.form-centered', options.body) m('div.form-centered', options.body)
]), ]),
@ -39,4 +40,19 @@ export default class FormModal extends Component {
hide() { hide() {
app.modal.close(); app.modal.close();
} }
handleErrors(errors) {
if (errors) {
this.alert(new Alert({
type: 'warning',
message: errors.map((error, k) => [error.detail, k < errors.length - 1 ? m('br') : ''])
}));
}
m.redraw();
if (errors) {
this.$('[name='+errors[0].path+']').select();
}
}
} }

View File

@ -67,6 +67,14 @@ export default class SignupModal extends FormModal {
return vdom; return vdom;
} }
ready() {
if (this.props.username) {
this.$('[name=email]').select();
} else {
super.ready();
}
}
fadeIn(element, isInitialized) { fadeIn(element, isInitialized) {
if (isInitialized) { return; } if (isInitialized) { return; }
$(element).hide().fadeIn(); $(element).hide().fadeIn();
@ -86,9 +94,7 @@ export default class SignupModal extends FormModal {
m.redraw(); m.redraw();
}, response => { }, response => {
this.loading(false); this.loading(false);
this.alert = new Alert({ type: 'warning', message: response.errors.map((error, k) => [error.detail, k < response.errors.length - 1 ? m('br') : '']) }); this.handleErrors(response.errors);
m.redraw();
this.$('[name='+response.errors[0].path+']').select();
}); });
} }
} }

View File

@ -34,6 +34,21 @@ export default class Model {
} }
} }
// clone the relevant parts of the model's old data so that we can revert
// back if the save fails
var oldData = {};
var currentData = this.data();
for (var i in data) {
if (i === 'links') {
oldData[i] = oldData[i] || {};
for (var j in newData[i]) {
oldData[i][j] = currentData[i][j];
}
} else {
oldData[i] = currentData[i];
}
}
this.pushData(data); this.pushData(data);
return app.request({ return app.request({
@ -45,6 +60,9 @@ export default class Model {
}).then(payload => { }).then(payload => {
this.store.data[payload.data.type][payload.data.id] = this; this.store.data[payload.data.type][payload.data.id] = this;
return this.store.pushPayload(payload); return this.store.pushPayload(payload);
}, response => {
this.pushData(oldData);
throw response;
}); });
} }

View File

@ -5,7 +5,6 @@ use Illuminate\Database\Migrations\Migration;
class CreateAccessTokensTable extends Migration class CreateAccessTokensTable extends Migration
{ {
/** /**
* Run the migrations. * Run the migrations.
* *
@ -14,9 +13,10 @@ class CreateAccessTokensTable extends Migration
public function up() public function up()
{ {
Schema::create('access_tokens', function (Blueprint $table) { Schema::create('access_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary(); $table->string('id', 100)->primary();
$table->integer('user_id')->unsigned(); $table->integer('user_id')->unsigned();
$table->timestamp('created_at');
$table->timestamp('expires_at');
}); });
} }

View File

@ -3,7 +3,7 @@
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
class CreateResetTokensTable extends Migration class CreatePasswordTokensTable extends Migration
{ {
/** /**
* Run the migrations. * Run the migrations.
@ -12,9 +12,10 @@ class CreateResetTokensTable extends Migration
*/ */
public function up() public function up()
{ {
Schema::create('reset_tokens', function (Blueprint $table) { Schema::create('password_tokens', function (Blueprint $table) {
$table->string('id'); $table->string('id', 100)->primary();
$table->integer('user_id')->unsigned(); $table->integer('user_id')->unsigned();
$table->timestamp('created_at');
}); });
} }
@ -25,6 +26,6 @@ class CreateResetTokensTable extends Migration
*/ */
public function down() public function down()
{ {
Schema::drop('reset_tokens'); Schema::drop('password_tokens');
} }
} }

View File

@ -41,8 +41,8 @@ class SeedCommand extends Command
*/ */
public function fire() public function fire()
{ {
$this->call('db:seed', ['--class' => 'Flarum\Core\Seeders\DiscussionsTableSeeder']);
$this->call('db:seed', ['--class' => 'Flarum\Core\Seeders\UsersTableSeeder']); $this->call('db:seed', ['--class' => 'Flarum\Core\Seeders\UsersTableSeeder']);
$this->call('db:seed', ['--class' => 'Flarum\Core\Seeders\DiscussionsTableSeeder']);
} }
/** /**

View File

@ -11,6 +11,10 @@ class Core
public static function config($key, $default = null) public static function config($key, $default = null)
{ {
if (! static::isInstalled()) {
return $default;
}
if (is_null($value = DB::table('config')->where('key', $key)->pluck('value'))) { if (is_null($value = DB::table('config')->where('key', $key)->pluck('value'))) {
$value = $default; $value = $default;
} }

View File

@ -1,10 +1,11 @@
<?php namespace Flarum\Core\Handlers\Commands; <?php namespace Flarum\Core\Handlers\Commands;
use Flarum\Core\Commands\RequestPasswordResetCommand; use Flarum\Core\Commands\RequestPasswordResetCommand;
use Flarum\Core\Models\ResetToken; use Flarum\Core\Models\PasswordToken;
use Flarum\Core\Repositories\UserRepositoryInterface; use Flarum\Core\Repositories\UserRepositoryInterface;
use Illuminate\Contracts\Mail\Mailer; use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Flarum\Core;
class RequestPasswordResetCommandHandler class RequestPasswordResetCommandHandler
{ {
@ -34,15 +35,16 @@ class RequestPasswordResetCommandHandler
throw new ModelNotFoundException; throw new ModelNotFoundException;
} }
$token = ResetToken::generate($user->id); $token = PasswordToken::generate($user->id);
$token->save(); $token->save();
$data = [ $data = [
'username' => $user->username, 'username' => $user->username,
'url' => route('flarum.forum.resetPassword', ['token' => $token->id]) 'url' => route('flarum.forum.resetPassword', ['token' => $token->id]),
'forumTitle' => Core::config('forum_title')
]; ];
$this->mailer->send(['text' => 'flarum::emails.reset'], $data, function ($message) use ($user) { $this->mailer->send(['text' => 'flarum::emails.resetPassword'], $data, function ($message) use ($user) {
$message->to($user->email); $message->to($user->email);
$message->subject('Reset Your Password'); $message->subject('Reset Your Password');
}); });

View File

@ -16,18 +16,28 @@ class AccessToken extends Model
*/ */
public $incrementing = false; public $incrementing = false;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['created_at', 'expires_at'];
/** /**
* Generate an access token for the specified user. * Generate an access token for the specified user.
* *
* @param int $userId * @param int $userId
* @param int $minutes
* @return static * @return static
*/ */
public static function generate($userId) public static function generate($userId, $minutes = 60)
{ {
$token = new static; $token = new static;
$token->id = str_random(40); $token->id = str_random(40);
$token->user_id = $userId; $token->user_id = $userId;
$token->created_at = time();
$token->expires_at = time() + $minutes * 60;
return $token; return $token;
} }

View File

@ -1,13 +1,13 @@
<?php namespace Flarum\Core\Models; <?php namespace Flarum\Core\Models;
class ResetToken extends Model class PasswordToken extends Model
{ {
/** /**
* The table associated with the model. * The table associated with the model.
* *
* @var string * @var string
*/ */
protected $table = 'reset_tokens'; protected $table = 'password_tokens';
/** /**
* Use a custom primary key for this model. * Use a custom primary key for this model.
@ -28,6 +28,7 @@ class ResetToken extends Model
$token->id = str_random(40); $token->id = str_random(40);
$token->user_id = $userId; $token->user_id = $userId;
$token->created_at = time();
return $token; return $token;
} }

View File

@ -1,6 +1,6 @@
<?php namespace Flarum\Forum\Actions; <?php namespace Flarum\Forum\Actions;
use Flarum\Core\Models\ResetToken; use Flarum\Core\Models\PasswordToken;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class ResetPasswordAction extends BaseAction class ResetPasswordAction extends BaseAction
@ -9,7 +9,7 @@ class ResetPasswordAction extends BaseAction
{ {
$token = array_get($routeParams, 'token'); $token = array_get($routeParams, 'token');
$token = ResetToken::findOrFail($token); $token = PasswordToken::findOrFail($token);
return view('flarum::reset')->with('token', $token->id); return view('flarum::reset')->with('token', $token->id);
} }

View File

@ -1,6 +1,6 @@
<?php namespace Flarum\Forum\Actions; <?php namespace Flarum\Forum\Actions;
use Flarum\Core\Models\ResetToken; use Flarum\Core\Models\PasswordToken;
use Flarum\Core\Commands\EditUserCommand; use Flarum\Core\Commands\EditUserCommand;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -8,7 +8,7 @@ class SavePasswordAction extends BaseAction
{ {
public function handle(Request $request, $routeParams = []) public function handle(Request $request, $routeParams = [])
{ {
$token = ResetToken::findOrFail($request->get('token')); $token = PasswordToken::findOrFail($request->get('token'));
$password = $request->get('password'); $password = $request->get('password');
$confirmation = $request->get('password_confirmation'); $confirmation = $request->get('password_confirmation');

View File

@ -1,3 +0,0 @@
Hey {{ $username }}!
Click here to reset your password: {{ $url }}

View File

@ -0,0 +1,8 @@
Hey {{ $username }}!
Someone (hopefully you!) has submitted a forgotten password request for your account on the {{ $forumTitle }}.
If this was you, click the following link to reset your password:
{{ $url }}
If you do not wish to change your password, just ignore this email and nothing will happen.