首页 文章

覆盖Laravel 5.3中的默认密码重置过程

提问于
浏览
1

我目前正在编写一个在后端使用Laravel 5.3的应用程序,我正在寻找一种方法来覆盖默认的密码重置行为 .

我需要更改的类是位于此处的“ResetPassword”:/ Illuminate / Auth / Notifications / ResetPassword.php

更改的原因是,此文件中生成的重置URL对于我的前端不正确 - 因为它使用url(),它将API url放在重置电子邮件中,而不是前端url .

2 回答

  • 4

    您可以在 User.php 中覆盖 CanResetPasswordsendPasswordResetNotification()方法

    use Illuminate\Notifications\Notifiable;
    use App\Notifications\CustomResetPasswordNotification;
    
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new CustomResetPasswordNotification($token));
    }
    

    并根据您的要求创建 CustomResetPasswordNotification.php .

    有关更多详细信息,请查看 Password Reset Emails section here

  • 0

    通过覆盖此处的User类,我找到了一种快速简便的方法来覆盖密码重置过程:

    /Illuminate/Foundation/Auth/User.php

    基本上,我创建了我自己的版本:

    <?php
    
    namespace App\Traits\Auth;
    
    use Illuminate\Auth\Authenticatable;
    use Illuminate\Database\Eloquent\Model;
    use App\Traits\Auth\Passwords\CanResetPassword;
    use Illuminate\Foundation\Auth\Access\Authorizable;
    use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
    use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
    use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
    
    class User extends Model implements
        AuthenticatableContract,
        AuthorizableContract,
        CanResetPasswordContract
    {
        use Authenticatable, Authorizable, CanResetPassword;
    }
    

    我将其保存到/ App / Traits / Auth,现在在我的用户模型中使用它 .

    <?php
    
    namespace App;
    
    use Illuminate\Notifications\Notifiable;
    use App\Traits\Auth\User as Authenticatable;
    
    class User extends Authenticatable
    {
        use Notifiable;
    
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'name', 'email', 'password',
        ];
    
        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            'password', 'remember_token',
        ];
    }
    

    完成后,您可以创建自己的“CanResetPassword”特征版本并替换Notification类并进行必要的调整 .

    以下是“CanResetPassword”特征的示例替换:

    namespace App\Traits\Auth\Passwords;
    
    use App\Notifications\CustomResetPassword as ResetPasswordNotification;
    
    trait CanResetPassword
    {
        /**
         * Get the e-mail address where password reset links are sent.
         *
         * @return string
         */
        public function getEmailForPasswordReset()
        {
            return $this->email;
        }
    
        /**
         * Send the password reset notification.
         *
         * @param  string  $token
         * @return void
         */
        public function sendPasswordResetNotification($token)
        {
            $this->notify(new ResetPasswordNotification($token));
        }
    }
    

相关问题