在Laravel 5.3中覆盖Auth忘记密码

时间:2016-11-03 08:54:45

标签: php laravel laravel-5.2 laravel-5.3

我正在使用内置的laravel auth功能。它工作正常。我试图覆盖以下两个功能。

1.使用mandrill发送忘记密码的电子邮件。

2.在注册帐户时发送验证电子邮件。

任何人都可以帮我解决这个问题

我的目标是使用mandril而不是默认电子邮件

我可以看到auth内置方法,但我不知道如何覆盖

trait ResetsPasswords
{
    use RedirectsUsers;

    /**
     * Display the password reset view for the given token.
     *
     * If no token is present, display the link request form.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  string|null  $token
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showResetForm(Request $request, $token = null)
    {
        return view('auth.passwords.reset')->with(
            ['token' => $token, 'email' => $request->email]
        );
    }

    /**
     * Reset the given user's password.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function reset(Request $request)
    {
        $this->validate($request, $this->rules(), $this->validationErrorMessages());

        // Here we will attempt to reset the user's password. If it is successful we
        // will update the password on an actual user model and persist it to the
        // database. Otherwise we will parse the error and return the response.
        $response = $this->broker()->reset(
            $this->credentials($request), function ($user, $password) {
                $this->resetPassword($user, $password);
            }
        );

        // If the password was successfully reset, we will redirect the user back to
        // the application's home authenticated view. If there is an error we can
        // redirect them back to where they came from with their error message.
        return $response == Password::PASSWORD_RESET
                    ? $this->sendResetResponse($response)
                    : $this->sendResetFailedResponse($request, $response);
    }

3 个答案:

答案 0 :(得分:2)

正如Mahfuzal所说,Laravel带有一堆开箱即用的邮件驱动程序。因此,只需更新您的.env文件即可使用正确的驱动程序。

至于在创建帐户时发送验证邮件,您只需覆盖postRegister()内的Auth/AuthController功能,如下所示:

public function postRegister(Request $request)
    {

        $validator = $this->validator($request->all());

        if ($validator->fails()) {
            $this->throwValidationException(
                $request, $validator
            );
        }

        $confirmation_code = str_random(30);
        $newUser = new User;
        $newUser->username = $request->username;
        $newUser->email = $request->email;
        $newUser->password = bcrypt($request->password);
        $newUser->confirmation_code = $confirmation_code;

        $newUser->save();

        $data = array('confirmation_code' => $confirmation_code, 'username' => $request->username);

        Mail::send('emails.verify', $data, function ($message) use ($newUser){
            $message->to($newUser->email, $newUser->username);
                $message->subject('Please verify your email address');
        });

        return redirect('/auth/login');
    }

这将在注册用户时执行上述代码,而不是Laravel开箱即用的默认代码,因此只需根据您的需要进行调整。

然后,您只需创建一个功能,在点击链接时检查令牌并验证其帐户。为此,我使用类似于here解释的内容。

答案 1 :(得分:1)

  

Laravel为SMTP,Mailgun, Mandrill ,Amazon SES提供驱动程序,   PHP的邮件功能和sendmail,让您快速入门   通过您选择的本地或云端服务发送邮件。

打开您的.env文件并通过您的Mandrill凭据更改以下内容,然后您就可以开始使用了。

  

MAIL_DRIVER =山魈

     

MAIL_HOST =

     

MAIL_PORT = 2525

     

MAIL_USERNAME =空

     

MAIL_PASSWORD =空

     

MAIL_ENCRYPTION =空

答案 2 :(得分:0)

您可以在控制器中创建自己的reset方法,该方法使用特征覆盖特征中的方法。

相关问题