laravel的用户修改密码与绑定邮箱的详细操作

admin3年前PHP教程29
一、修改密码

1.1 创建修改密码控制器

运行命令php artisan make:controller auth/passwordcontroller

写入修改密码方法:

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
     * 修改密码
     */
    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 创建修改密码路由

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


1.3 测试效果

二、绑定邮箱

 2.1 绑定邮箱控制器

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

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

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
 
namespace app\http\controllers\auth;
 
use app\http\controllers\basecontroller;
use app\mail\sendemailcode;
use illuminate\http\request;
use illuminate\support\facades\mail;
 
class bindcontroller extends basecontroller
{
    /**
     * 获取邮件的验证码
     */
    public function emailcode(request $request) {
        $request->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 创建对应路由

2
3
4
5
// 发送邮件验证码
          $api->post('email/code', [bindcontroller::class, 'emailcode']);
 
          // 更新邮箱
          $api->post('email/update', [bindcontroller::class, 'updateemail']);


2.3 创建发送邮件的类

运行命令php artisan make:mail sendemailcode:

写入:

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
 
namespace app\mail;
 
use illuminate\bus\queueable;
use illuminate\mail\mailable;
use illuminate\queue\serializesmodels;
use illuminate\support\facades\cache;
 
class sendemailcode extends mailable
{
    use queueable, serializesmodels;
 
    protected $email;
    /**
     * create a new message instance.
     *
     * @return void
     */
    public function __construct($eamil)
    {
        $this->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]);
    }
}


创建发送邮件的模版:

模版写入:

<h3>邮箱验证码是:{{$code}}</h3>
<h3>验证码5分钟内有效,请及时使用!</h3>

2.4 测试效果

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

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

到此这篇关于laravel的用户修改密码与绑定邮箱的文章就介绍到这了,更多相关laravel修改密码内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

免责声明:本文内容来自用户上传并发布,站点仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。请核实广告和内容真实性,谨慎使用。

相关文章

Thinkphp6.0中间件的具体使用

目录全局中间件 应用中间件 路由中间件 控制器中间件 中间件传参 6.0中间件分为系统中间件和应用中间件,系统中间件为核心框架内置的中间件,应用中间件是在应用里面创建的中间件。中间件的主要应用场景可以...

php回溯算法计算组合总和的实例代码

给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的每个数字在每个组合中只能使用一次。说...

泉州高防服务器租用需要注意什么

泉州高防服务器租用需要注意以下几点:1.选择正规的服务提供商:选择正规的服务提供商是非常重要的,这可以确保您的服务器能够得到良好的维护和保障,同时可以避免不必要的问题和风险。2.确认高防性能:选择高防...

eval(cmd)与eval($cmd)的区别与联系

这个问题一直困扰我许久,今天终于解决清楚了问题1:eval的执行是否需要双引号包括:先看三个命令:a:<?php eval(system(dir))?>b:<?php &n...

PHP扩展之kafka安装应用案例详解

话说用了就要有点产出,要不然过段时间又忘了,所以在这里就记录一下试用Kafka的安装过程和php扩展的试用。实话说,如果用于队列的话,跟PHP比较配的,还是Redis。用的顺手,呵呵,只是Redis不...

详解PHP中的命名空间

命名空间其实早在PHP5.3就已经出现了。不过大部分同学可能在各种框架的使用中才会接触到命名空间的内容,当然,现代化的开发也都离不开这些能够快速产出的框架。这次我们不从框架的角度,仅从简单的代码角度来...