【发布时间】:2018-03-01 09:37:46
【问题描述】:
我目前正在开发 Laravel 5.5 中的 forgot 密码 API 和 reset 密码 API。目前,我不知道如何在单击电子邮件中的链接并显示重置密码和确认密码后验证密码。
我已将令牌与电子邮件一起发送。问题是在询问密码和确认密码之前,我需要先验证令牌是否是表中的有效令牌及其到期日期。谁能指导我如何做到这一点?
这是我忘记的密码。
public function forgotPasswordApi(Request $request){
$rawPostData = json_decode($request->getContent(), true);
if(isset($rawPostData['email'])) {
$user = UserModel::where('email',$rawPostData['email'])->first();
$token=TokenModel::where('user_id',$user->id)->first();
if($user){
if(!$token){
$token=new TokenModel;
$token->token=md5(uniqid().$token->id);
$token->token_code = TokenModel::$forgotPasswordToken;
$token->expire_date = date("F j, Y, H:i:s", strtotime('+24 hour'));
$token->user_id=$user->id;
$token->save();
}
Mail::raw($user, function ($message) use ($user){
$token=TokenModel::where('user_id',$user->id)->first();
$message->to($user['email'])
->subject('Forgot Password Subject')
->setBody('To reset password. Click on the following URL. '.url('/password/reset/?token=').$token->token);
});
}
$data['status'] = 'success';
$data['msg'] = $token;
}else{
$data['status']='fail';
$data['msg']='Missing required paramater';
}
return new JsonResponse($data);
}
注意:带有重置链接的邮件已成功发送,但我不知道通过该链接重置密码。
电子邮件示例
To reset password. Click on the following URL.
http://127.0.0.1:8000/password/reset?token=fca709c8a8ef5ff61aa5bb73e475c602
这是我的不完整的重置密码
public function resetPasswordApi(Request $request){
if(Input::has('token'))
{
$token_input = Input::get('token');
$token = DB::tables('tokens')
->where('token',$token_input)
->where('expire_date','>=',Carbon::now()->subHours(12))
->first();
}else{
dd('input value not found');
}
$rawPostData = json_decode($request->getContent(), true);
if($token)
{
if(isset($rawPostData['password'],$rawPostData['confirm_password']))
{
if($rawPostData['password'] === $rawPostData['confirm_password'])
{
$updatedUser = UserModel::where('id',$token->user_id)->first();
$updatedUser->password = bcrypt($rawPostData['password']);
$updatedUser->save();
$data['status'] = 'success';
}
else
{
$data['status']='fail';
$data['msg']='Password and Confirm Password Match failed.';
}
}
else
{
$data['status']='fail';
$data['msg']='Missing required paramater';
}
}
else
{
$data['status']='fail';
$data['msg']='Missing required paramater';
}
return new JsonResponse($data);
}
}
【问题讨论】:
-
您不使用the built in Laravel authentication 有什么原因吗? Laravel 已经处理了所有这些。
标签: php laravel email laravel-5 passwords