千家信息网

laravel的用户怎么修改密码和绑定邮箱

发表于:2024-10-23 作者:千家信息网编辑
千家信息网最后更新 2024年10月23日,本篇内容主要讲解"laravel的用户怎么修改密码和绑定邮箱",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"laravel的用户怎么修改密码和绑定邮箱"吧!
千家信息网最后更新 2024年10月23日laravel的用户怎么修改密码和绑定邮箱

本篇内容主要讲解"laravel的用户怎么修改密码和绑定邮箱",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"laravel的用户怎么修改密码和绑定邮箱"吧!

目录
  • 一、修改密码

    • 1.1 创建修改密码控制器

    • 1.2 创建修改密码路由

    • 1.3 测试效果

  • 二、绑定邮箱

    • 2.1 绑定邮箱控制器

    • 2.2 创建对应路由

    • 2.3 创建发送邮件的类

    • 2.4 测试效果

一、修改密码

1.1 创建修改密码控制器

运行命令php artisan make:controller Auth/PasswordController

写入修改密码方法:

/**     * 修改密码     */    public function updatePassword(Request $request) {        $request->validate([            'old_password' => 'required|min:6|max:16',            'password' => 'required|min:6|max:16|confirmed',        ], [            'old_password.required' => '旧密码不能为空',            'old_password.min' => '旧密码最少6个字符',            'old_password.max' => '旧密码最多16个字符',        ]);        // 旧密码        $old_password = $request->input('old_password');        // 用户实例        $user = auth('api')->user();        // 验证旧密码是否正确        if (!password_verify($old_password, $user->password)) {            return $this->response->errorBadRequest('旧密码不正确');        }         // 更新用户密码          $user->password = bcrypt($request->input('password'));        $user->save();        return $this->response->noContent();    }

1.2 创建修改密码路由

 // 修改密码            $api->post('password/update', [PasswordController::class, 'updatePassword']);

1.3 测试效果

二、绑定邮箱

2.1 绑定邮箱控制器

运行命令php artisan make:controller Auth/BindController创建绑定邮箱的控制器:

写入发送邮箱验证码和更新邮箱的处理函数:

validate([            'email' => 'required|email'        ]);        // 发送验证码到邮件        Mail::to($request->input('email'))->queue(new SendEmailCode($request->input('email')));        return $this->response->noContent();    }    /**     * 更新邮箱     */    public function updateEmail(Request $request) {        $request->validate([            'email' => 'required|email',            'code' => 'required'        ], [            'code.required' => "验证码不能为空",        ]);        // 验证code是否正确        if (cache($request->input('email')) != $request->input('code')) {            return $this->response->errorBadRequest('验证码或邮箱错误!');        }        // 更新邮箱        $user = auth('api')->user();         $user->email = $request->input('email');        $user->save();        return $this->response->noContent();    } }

如果修改了队列了,就要重启队列,命令sudo supervisorctl restart all

2.2 创建对应路由

  // 发送邮件验证码            $api->post('email/code', [BindController::class, 'emailCode']);            // 更新邮箱            $api->post('email/update', [BindController::class, 'updateEmail']);

2.3 创建发送邮件的类

运行命令php artisan make:mail SendEmailCode:

写入:

email = $eamil;    }    /**     * Build the message.     *     * @return $this     */    public function build()    {        // 生成code        $code = rand(1000, 9999);        // 获取邮箱        // 使用缓存邮箱对应的code        Cache::put($this->email, $code, now()->addMinute(5)); // 5分钟过期        return $this->view('emails.send-email-code', ['code' => $code]);    }}

创建发送邮件的模版:

模版写入:

邮箱验证码是:{{$code}}


验证码5分钟内有效,请及时使用!

2.4 测试效果

可以看到这边收到邮箱验证码。
测试更新的输入邮箱不正确或者验证码不正确:

输入正确的邮箱和验证码就会修改了。

到此,相信大家对"laravel的用户怎么修改密码和绑定邮箱"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0