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的用户怎么修改密码和绑定邮箱"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
邮箱
密码
验证
更新
用户
控制器
邮件
控制
测试
命令
效果
路由
运行
内容
字符
方法
模版
队列
学习
输入
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
php设置定时访问数据库
深圳冰川网络技术有限公司
可以使用的rdf数据库
价格适中的即时通讯软件开发
博客代码片段如何存进数据库
税控服务器管理系统介绍
社交 app 服务器
软件开发高层平均年薪
网络技术升本
日本的皮肤管理服务器
青少年网络安全教育演讲稿
软件开发工程师个人述职报告范文
汽车控制策略软件开发流程
数据库连接不上arcgis
三得商城软件开发
在数据库中创建的表格
数据库定义联系
本地连接远程数据库关闭
为什么要学习智能机器人软件开发
珠海网络安全建设
mxview服务器参数设定
网宿科技互联网销售
宁波慈溪市浪潮机架服务器
电厂网络安全防范
大连小小网络技术有限公司
美团买家数据库
吉林数据网络技术服务询问报价
2019年专业技术人员继续教育网络安全
唐盛科技大学互联网项目
软件开发新人入职