diff --git a/src/app/Auth/Concerns/CanResetPassword.php b/src/app/Auth/Concerns/CanResetPassword.php index 84f0e1e..2bbf75c 100644 --- a/src/app/Auth/Concerns/CanResetPassword.php +++ b/src/app/Auth/Concerns/CanResetPassword.php @@ -24,6 +24,6 @@ public function getEmailForPasswordReset() */ public function sendPasswordResetNotification($token) { - $this->user->notify(new ResetPasswordNotification($token)); + $this->notify(new ResetPasswordNotification($token)); } } diff --git a/src/app/Auth/Concerns/MustVerifyEmail.php b/src/app/Auth/Concerns/MustVerifyEmail.php index c7f2e5d..48b9a27 100644 --- a/src/app/Auth/Concerns/MustVerifyEmail.php +++ b/src/app/Auth/Concerns/MustVerifyEmail.php @@ -2,14 +2,13 @@ namespace Bluewing\Auth\Concerns; -use Carbon\Carbon; +use Bluewing\Eloquent\Model; use Illuminate\Auth\Notifications\VerifyEmail; /** * Trait BluewingMustVerifyEmail * - * @property string email - This property exists on all models that this trait traits. - * @property Carbon emailVerifiedAt - This property exists on all models that this trait traits. + * @property Model user - The `User` model that is related to the model that traits the `MustVerifyEmail` functionality. * * @package Bluewing * @@ -28,7 +27,7 @@ trait MustVerifyEmail { */ public function hasVerifiedEmail() { - return ! is_null($this->emailVerifiedAt); + return ! is_null($this->user->emailVerifiedAt); } /** @@ -38,7 +37,7 @@ public function hasVerifiedEmail() */ public function markEmailAsVerified() { - return $this->forceFill([ + return $this->user->forceFill([ 'emailVerifiedAt' => $this->freshTimestamp(), ])->save(); } @@ -60,6 +59,6 @@ public function sendEmailVerificationNotification() */ public function getEmailForVerification() { - return $this->email; + return $this->user->email; } } diff --git a/src/app/Auth/Concerns/ResetsPasswords.php b/src/app/Auth/Concerns/ResetsPasswords.php new file mode 100644 index 0000000..e10c38b --- /dev/null +++ b/src/app/Auth/Concerns/ResetsPasswords.php @@ -0,0 +1,97 @@ +json($this->guard()->user()); + } + + /** + * Set the user's password. This overrides the `setUserPassword` method in the `ResetsPasswords` trait to remove + * the hashing mechanism, as this is performed automatically in the `User` class mutator for this property. + * + * @param CanResetPassword $member + * @param string $password + * @return void + */ + protected function setUserPassword($member, $password) + { + $member->user->password = $password; + } + + /** + * Override the credentials needed to perform a password reset. We do not require a password confirmation to + * process the request. + * + * @param Request $request + * @return array + */ + protected function credentials(Request $request) + { + return $request->only( + 'password', 'email', 'token' + ); + } + + /** + * Override the password reset rules to remove the need for a password confirmation. + * + * @return array + */ + protected function rules() + { + return [ + 'token' => 'required', + 'email' => 'required|email', + 'password' => 'required|min:6', + ]; + } + + /** + * Reset the given user's password. This removes the creation of a `rememberToken` property that is not used, and + * replaces the `guard()->login()` call with a `guard()->setUser()` call. Additionally, ensure we are saving the + * `User`, and not the `Member`, as the password is not stored on the `Member` instance. + * + * @param CanResetPassword $member + * @param string $password + * + * @return void + */ + protected function resetPassword($member, $password) + { + $this->setUserPassword($member, $password); + $member->user->save(); + + event(new PasswordReset($member)); + + $this->guard()->setUser($member); + } +} diff --git a/src/app/Auth/Member.php b/src/app/Auth/Member.php index e9de7b3..18070c1 100644 --- a/src/app/Auth/Member.php +++ b/src/app/Auth/Member.php @@ -9,13 +9,17 @@ use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; +use Illuminate\Contracts\Auth\MustVerifyEmail as MustVerifyEmailContract; use Bluewing\Auth\Concerns\Authenticatable as BluewingAuthenticatable; -use Illuminate\Foundation\Auth\Access\Authorizable; use Bluewing\Auth\Concerns\CanResetPassword as BluewingCanResetPassword; use Bluewing\Auth\Concerns\MustVerifyEmail as BluewingMustVerifyEmail; +use Illuminate\Foundation\Auth\Access\Authorizable; +use Illuminate\Notifications\Notifiable; -class Member extends BluewingPivot implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract +class Member extends BluewingPivot implements + AuthenticatableContract, AuthorizableContract, + CanResetPasswordContract, MustVerifyEmailContract { - use BluewingAuthenticatable, Authorizable, BluewingCanResetPassword, BluewingMustVerifyEmail; + use BluewingAuthenticatable, BluewingCanResetPassword, BluewingMustVerifyEmail, Authorizable, Notifiable; } diff --git a/src/app/Http/Controllers/ForgotPasswordController.php b/src/app/Http/Controllers/ForgotPasswordController.php new file mode 100644 index 0000000..a385cf0 --- /dev/null +++ b/src/app/Http/Controllers/ForgotPasswordController.php @@ -0,0 +1,41 @@ +sendResetLinkEmail($request); + } + + /** + * Override the default reset link response to return `204 No Content` always, if the reset link was successfully + * sent. + * + * @param Request $request + * @param $response + * + * @return JsonResponse + */ + protected function sendResetLinkResponse(Request $request, $response) + { + return response()->json(null, 204); + } +} + diff --git a/src/app/Http/Controllers/ResetPasswordController.php b/src/app/Http/Controllers/ResetPasswordController.php new file mode 100644 index 0000000..5c5a959 --- /dev/null +++ b/src/app/Http/Controllers/ResetPasswordController.php @@ -0,0 +1,36 @@ +middleware(AppendTokensToResponse::class); + } + + /** + * This provides an invokable shortcut to the `reset` method contained in the `ResetsPasswords` trait. + * + * @param Request $request - + * + * @return JsonResponse - + */ + public function __invoke(Request $request) + { + return $this->reset($request); + } +}