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安全错误
数据库的锁怎样保障安全
网络安全总纲
车载应用服务器ICAS1
网络安全 国标
有效数据库服务器
数据库应用技术怎么样
深资创软件开发地址
全市中小学网络安全直播课
hp服务器网卡聚合
信也科技还属于互联网公司
网络安全的弊有关问题
荣耀图片数据库在哪
贴牌业务软件开发方案模板
r语言 读取数据库
图片路径保存到数据库
蓝睿互联网科技有限公司
奥特曼系列ol1902服务器
电脑删留言服务器繁忙
联想集团数据库设计
股票历史数据库
贵州软件开发公司
守卫网络安全 建设网络强国
数据库插入时间写入时间
软件开发工作量统计与考核
中国移动入股服务器公司
计算机网络技术有java吗
天津精英网络技术咨询哪家好
成都盛世零点网络技术有限公司
华为 录播服务器
赤峰网络技术团队
预测nba技术数据库