Skip to content

Instantly share code, notes, and snippets.

@nasrulhazim
Last active April 28, 2021 07:17
Show Gist options
  • Save nasrulhazim/c9b5e2ae414ff3c004c388c485e4cb80 to your computer and use it in GitHub Desktop.
Save nasrulhazim/c9b5e2ae414ff3c004c388c485e4cb80 to your computer and use it in GitHub Desktop.
Reset and Update Password from API

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);
}
}
@matiazar
Copy link

I have a problem now when i made the Reset process.. it returns passwords.user (User invalid)
I have changed the Guard to
protected function guard()
{
return Auth::guard('api');
}

but no luck !

@ashok2009it
Copy link

ashok2009it commented Sep 1, 2017

How to send reset link mail using api?

@abrarkhalil
Copy link

For Password Reset Api Simply add Route
Route::post('/UserPasswordReset','Auth\ForgotPasswordController@sendResetLink');
and make function (sendResetLink) in Auth\ForgotPasswordController
public function sendResetLink(Request $request)
{
$this->validateEmail($request);

    // We will send the password reset link to this user. Once we have attempted
    // to send the link, we will examine the response then see the message we
    // need to show to the user. Finally, we'll send out a proper response.
    $response = $this->broker()->sendResetLink(
        $request->only('email')
    );



     $response == Password::RESET_LINK_SENT
                ? $this->sendResetLinkResponse($response)
                : $this->sendResetLinkFailedResponse($request, $response);

            if($response == "passwords.sent"){

                $response1["error"]= false;
                $response1["message"]="reset link sent to email";
                
                return response()->json($response1);
            }

            $response1["error"]= true;
                $response1["message"]="user not found";
               
                return response()->json($response1);

}

Good Luck working fine for me!

@hardikpatil3333
Copy link

is this an API call for github account password reset..? @pravintamil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment