https://blog.nasrulhazim.com/2017/01/reset-your-password-from-api/?utm_source=learninglaravel.net
Reset and Update Password from API
Setup API endpoints
Route::post('password/email', 'Auth\ForgotPasswordController@getResetToken');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');
Get Reset Token
You may see the details of the controller
here
API Endpoint
protocol:://domain/password/email
API Headers
Accept application/json
API Method
POST
API Parameters
email [your-email-address]
API Response
{
"data": {
"token": "770f6138f62a5da086881a4bf8799e7d7fc3fc60d30e6f35ad58e4f9496fd597"
},
"message": ""
}
Update Password
You may see the controller details
here
API Endpoint
protocol:://domain/password/reset
API Headers
Accept application/json
API Method
POST
API Parameters
email [your-email-address]
password [new-password]
password_confirmation [retype-new-password]
token [the-token-you-get-from-previous-one]
API Response
{
"data": null,
"message": "Your password has been reset!"
}
| <?php |
| |
| Route::post('password/email', 'Auth\ForgotPasswordController@getResetToken'); |
| Route::post('password/reset', 'Auth\ResetPasswordController@reset'); |
| <?php |
| |
| namespace App\Http\Controllers\Auth; |
| |
| use App\Http\Controllers\Controller; |
| use App\Transformers\Json; |
| use App\User; |
| use Illuminate\Foundation\Auth\SendsPasswordResetEmails; |
| use Illuminate\Http\Request; |
| |
| class ForgotPasswordController extends Controller |
| { |
| /* |
| |-------------------------------------------------------------------------- |
| | Password Reset Controller |
| |-------------------------------------------------------------------------- |
| | |
| | This controller is responsible for handling password reset emails and |
| | includes a trait which assists in sending these notifications from |
| | your application to your users. Feel free to explore this trait. |
| | |
| */ |
| |
| use SendsPasswordResetEmails; |
| |
| /** |
| * Create a new controller instance. |
| * |
| * @return void |
| */ |
| public function __construct() |
| { |
| $this->middleware('guest'); |
| } |
| |
| /** |
| * Send a reset link to the given user. |
| * |
| * @param \Illuminate\Http\Request $request |
| * @return \Illuminate\Http\Response |
| */ |
| public function getResetToken(Request $request) |
| { |
| $this->validate($request, ['email' => 'required|email']); |
| if ($request->wantsJson()) { |
| $user = User::where('email', $request->input('email'))->first(); |
| if (!$user) { |
| return response()->json(Json::response(null, trans('passwords.user')), 400); |
| } |
| $token = $this->broker()->createToken($user); |
| return response()->json(Json::response(['token' => $token])); |
| } |
| } |
| } |
| <?php |
| |
| namespace App\Transformers; |
| |
| class Json |
| { |
| public static function response($data = null, $message = null) |
| { |
| return [ |
| 'data' => $data, |
| 'message' => $message, |
| ]; |
| } |
| } |
| <?php |
| |
| namespace App\Http\Controllers\Auth; |
| |
| use App\Http\Controllers\Controller; |
| use App\Transformers\Json; |
| use Illuminate\Foundation\Auth\ResetsPasswords; |
| use Illuminate\Http\Request; |
| use Illuminate\Support\Facades\Password; |
| |
| class ResetPasswordController extends Controller |
| { |
| /* |
| |-------------------------------------------------------------------------- |
| | Password Reset Controller |
| |-------------------------------------------------------------------------- |
| | |
| | This controller is responsible for handling password reset requests |
| | and uses a simple trait to include this behavior. You're free to |
| | explore this trait and override any methods you wish to tweak. |
| | |
| */ |
| |
| use ResetsPasswords; |
| |
| /** |
| * Create a new controller instance. |
| * |
| * @return void |
| */ |
| public function __construct() |
| { |
| $this->middleware('guest'); |
| } |
| |
| /** |
| * Reset the given user's password. |
| * |
| * @param \Illuminate\Http\Request $request |
| * @return \Illuminate\Http\Response |
| */ |
| public function reset(Request $request) |
| { |
| $this->validate($request, $this->rules(), $this->validationErrorMessages()); |
| |
| // Here we will attempt to reset the user's password. If it is successful we |
| // will update the password on an actual user model and persist it to the |
| // database. Otherwise we will parse the error and return the response. |
| $response = $this->broker()->reset( |
| $this->credentials($request), function ($user, $password) { |
| $this->resetPassword($user, $password); |
| } |
| ); |
| |
| if ($request->wantsJson()) { |
| if ($response == Password::PASSWORD_RESET) { |
| return response()->json(Json::response(null, trans('passwords.reset'))); |
| } else { |
| return response()->json(Json::response($request->input('email'), trans($response), 202)); |
| } |
| } |
| |
| // If the password was successfully reset, we will redirect the user back to |
| // the application's home authenticated view. If there is an error we can |
| // redirect them back to where they came from with their error message. |
| return $response == Password::PASSWORD_RESET |
| ? $this->sendResetResponse($response) |
| : $this->sendResetFailedResponse($request, $response); |
| } |
| } |
No comments:
Post a Comment