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

2025-05-29 0 30

一、修改密码

1.1 创建修改密码控制器

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

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

写入修改密码方法:

?

1

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();

}

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

1.2 创建修改密码路由

?

1

2
// 修改密码

$api->post('password/update', [passwordcontroller::class, 'updatepassword']);

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

1.3 测试效果

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

二、绑定邮箱

2.1 绑定邮箱控制器

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

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

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

?

1

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 创建对应路由

?

1

2

3

4

5
// 发送邮件验证码

$api->post('email/code', [bindcontroller::class, 'emailcode']);

// 更新邮箱

$api->post('email/update', [bindcontroller::class, 'updateemail']);

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

2.3 创建发送邮件的类

运行命令php artisan make:mail sendemailcode:

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

写入:

?

1

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]);

}

}

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

创建发送邮件的模版:

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

模版写入:

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

2.4 测试效果

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

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

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

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

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

原文链接:https://blog.csdn.net/weixin_44103733/article/details/120121853

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 laravel的用户修改密码与绑定邮箱的详细操作 https://www.kuaiidc.com/89924.html

相关文章

发表评论
暂无评论