如何在 laravel 中创建 api 忘记密码和更改密码? [复制]
Posted
技术标签:
【中文标题】如何在 laravel 中创建 api 忘记密码和更改密码? [复制]【英文标题】:How can I create api forgot password and change password in laravel? [duplicate] 【发布时间】:2020-03-15 18:29:24 【问题描述】:如何在 laravel 使用护照中创建忘记密码和更改密码的 API?
**路线:**
**邮递员图片:**
** api中的参数传递:**
【问题讨论】:
希望此链接对您和其他人有所帮助***.com/a/64046941/9077019 【参考方案1】:api.php
Route::post('forgot-password', 'Api\AuthController@forgot_password');
Route::group(['middleware' => 'auth:api'], function ()
Route::post('change-password', 'Api\AuthController@change_password');
);
忘记密码 api
public function forgot_password(Request $request)
$input = $request->all();
$rules = array(
'email' => "required|email",
);
$validator = Validator::make($input, $rules);
if ($validator->fails())
$arr = array("status" => 400, "message" => $validator->errors()->first(), "data" => array());
else
try
$response = Password::sendResetLink($request->only('email'), function (Message $message)
$message->subject($this->getEmailSubject());
);
switch ($response)
case Password::RESET_LINK_SENT:
return \Response::json(array("status" => 200, "message" => trans($response), "data" => array()));
case Password::INVALID_USER:
return \Response::json(array("status" => 400, "message" => trans($response), "data" => array()));
catch (\Swift_TransportException $ex)
$arr = array("status" => 400, "message" => $ex->getMessage(), "data" => []);
catch (Exception $ex)
$arr = array("status" => 400, "message" => $ex->getMessage(), "data" => []);
return \Response::json($arr);
修改密码api
public function change_password(Request $request)
$input = $request->all();
$userid = Auth::guard('api')->user()->id;
$rules = array(
'old_password' => 'required',
'new_password' => 'required|min:6',
'confirm_password' => 'required|same:new_password',
);
$validator = Validator::make($input, $rules);
if ($validator->fails())
$arr = array("status" => 400, "message" => $validator->errors()->first(), "data" => array());
else
try
if ((Hash::check(request('old_password'), Auth::user()->password)) == false)
$arr = array("status" => 400, "message" => "Check your old password.", "data" => array());
else if ((Hash::check(request('new_password'), Auth::user()->password)) == true)
$arr = array("status" => 400, "message" => "Please enter a password which is not similar then current password.", "data" => array());
else
User::where('id', $userid)->update(['password' => Hash::make($input['new_password'])]);
$arr = array("status" => 200, "message" => "Password updated successfully.", "data" => array());
catch (\Exception $ex)
if (isset($ex->errorInfo[2]))
$msg = $ex->errorInfo[2];
else
$msg = $ex->getMessage();
$arr = array("status" => 400, "message" => $msg, "data" => array());
return \Response::json($arr);
【讨论】:
评论不用于扩展讨论;这个对话是moved to chat。 请使用上面的聊天链接。 你好@SamuelLiew 要遵守 REST API 中的 URI 最佳实践,请使用连字符而不是下划线。 ***.com/a/64046941/9077019以上是关于如何在 laravel 中创建 api 忘记密码和更改密码? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Laravel 中创建 RESTful API 以在我的 BackboneJS 应用程序中使用
Laravel JSON API: 如何从数据库中创建自定义的虚拟资源(用于聚合值)?