laravel的用户修改密码与绑定邮箱的详细操作
作者:陆小天奥利给 发布时间:2023-06-17 09:55:06
标签:python,绑定邮箱,修改密码
一、修改密码
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
创建绑定邮箱的控制器:
写入发送邮箱验证码和更新邮箱的处理函数:
<?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 创建对应路由
// 发送邮件验证码
$api->post('email/code', [BindController::class, 'emailCode']);
// 更新邮箱
$api->post('email/update', [BindController::class, 'updateEmail']);
2.3 创建发送邮件的类
运行命令php artisan make:mail SendEmailCode
:
写入:
<?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 测试效果
可以看到这边收到邮箱验证码。
测试更新的输入邮箱不正确或者验证码不正确:
输入正确的邮箱和验证码就会修改了。
来源:https://blog.csdn.net/weixin_44103733/article/details/120121853
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- Python django中我们经常用的response有django中的 JsonResponse, HttpResponse,还有DRF
- 前言给新的环境安装pip install tensorflow,结果报错了。跟着我分析解决一波。报错原因这个红字已经说的很清楚了。ERROR
- 检查所使用的语句是否标准 /* 标准SQL和T-SQL之间有很多区别——太多了,这里就不说了。还有,如果你在SQL Server上工作, 那
- python数组和矩阵先创建一个一维数组直接定义一个数组:a = [1,2,3,4,5]b = ['a','c
- 本文实例讲述了Python线程threading模块用法。分享给大家供大家参考,具体如下:threading-更高级别的线程接口源代码:Li
- 在python代码编写过程中,养成注释的习惯非常有用,可以让自己或别人后续在阅读代码时,轻松理解代码的含义。如果只是简单的单行注释,可直接用
- 1.认证与授权1.验证:身份验证是验证个人或设备标识的过程。身份验证过程之一是登录过程。注册网站后,您的信息(ID,密码,名称,电子邮件等)
- 主备同步,也叫主从复制,是MySQL提供的一种高可用的解决方案,保证主备数据一致性的解决方案。在生产环境中,会有很多不可控因素,例如数据库服
- 一、mysqlcheck简介mysqlcheck客户端可以检查和修复MyISAM表。它还可以优化和分析表。mysqlcheck的功能类似my
- 本文实例为大家分享了python实现五子棋游戏的具体代码,供大家参考,具体内容如下话不多说,直接上代码:全部工程文件,在GitHub:五子棋
- 复合主键:create table index_test( a int not null, &nbs
- asp程序运行速度试验实验:1。作相同的分支条件判断:IF 比 SELECT慢。用以下程序测试:
- numpy库是Python进行数据分析和矩阵运算的一个非常重要的库,可以说numpy让Python有了matlab的味道本文主要介绍几个nu
- 实例如下所示:<?php 加载类//include("./Ren.class.php");//include &q
- 导语承载童年的纸飞机你还会叠嘛?如果你是个80后或者90后,那你应该记得小时候玩的纸飞机。叠好后,哈口仙气,飞出去,感觉棒棒哒。虽然是一个极
- 这里需要用到一个Django插件:django-pagination安装打开控制台 输入pip install dj-pagination实
- 从这篇开始,用一个新的系列文章去介绍和学习MySQL。为什么要学习MySQL,从产品角度来说,大部分软件的数据库服务还是采取关系型数据库,主
- 有的时候,我们本来写得好好的爬虫代码,之前还运行得Ok, 一下子突然报错了。报错信息如下:Http 800 Internal interne
- 假设现在有如下N条记录 表明叫book id author title 1 aaa AAA 2 bbb BBB 3 ccc CCC 4 dd
- 我们在爬虫时,经常会遇见中文乱码问题,之前都是编码格式的问题,很少遇见由于压缩格式造成的编码混乱问题,特记录下。先看下混乱的编码样式。b