forked from francescomalatesta/laravel-api-boilerplate-jwt
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ae517a
commit 4f511db
Showing
7 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,4 @@ API_SUBTYPE=app | |
API_VERSION=v1 | ||
|
||
SIGN_UP_RELEASE_TOKEN=false | ||
PASSWORD_RESET_RELEASE_TOKEN=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace App\Api\V1\Controllers; | ||
|
||
use Config; | ||
use App\User; | ||
use Tymon\JWTAuth\JWTAuth; | ||
use App\Http\Controllers\Controller; | ||
use Illuminate\Support\Facades\Password; | ||
use App\Api\V1\Requests\ResetPasswordRequest; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
class ResetPasswordController extends Controller | ||
{ | ||
public function resetPassword(ResetPasswordRequest $request, JWTAuth $JWTAuth) | ||
{ | ||
$response = $this->broker()->reset( | ||
$this->credentials($request), function ($user, $password) { | ||
$this->reset($user, $password); | ||
} | ||
); | ||
|
||
if($response !== Password::PASSWORD_RESET) { | ||
throw new HttpException(500); | ||
} | ||
|
||
if(!Config::get('boilerplate.reset_password.release_token')) { | ||
return response()->json([ | ||
'status' => 'ok', | ||
]); | ||
} | ||
|
||
$user = User::where('email', '=', $request->get('email'))->first(); | ||
|
||
return response()->json([ | ||
'status' => 'ok', | ||
'token' => $JWTAuth->fromUser($user) | ||
]); | ||
} | ||
|
||
/** | ||
* Get the broker to be used during password reset. | ||
* | ||
* @return \Illuminate\Contracts\Auth\PasswordBroker | ||
*/ | ||
public function broker() | ||
{ | ||
return Password::broker(); | ||
} | ||
|
||
/** | ||
* Get the password reset credentials from the request. | ||
* | ||
* @param ResetPasswordRequest $request | ||
* @return array | ||
*/ | ||
protected function credentials(ResetPasswordRequest $request) | ||
{ | ||
return $request->only( | ||
'email', 'password', 'password_confirmation', 'token' | ||
); | ||
} | ||
|
||
/** | ||
* Reset the given user's password. | ||
* | ||
* @param \Illuminate\Contracts\Auth\CanResetPassword $user | ||
* @param string $password | ||
* @return void | ||
*/ | ||
protected function reset($user, $password) | ||
{ | ||
$user->password = $password; | ||
$user->save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Api\V1\Requests; | ||
|
||
use Config; | ||
use Dingo\Api\Http\FormRequest; | ||
|
||
class ResetPasswordRequest extends FormRequest | ||
{ | ||
public function rules() | ||
{ | ||
return Config::get('boilerplate.reset_password.validation_rules'); | ||
} | ||
|
||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
tests/Functional/Api/V1/Controllers/ResetPasswordControllerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace App\Functional\Api\V1\Controllers; | ||
|
||
use DB; | ||
use Config; | ||
use App\User; | ||
use App\TestCase; | ||
use Carbon\Carbon; | ||
use Illuminate\Foundation\Testing\DatabaseMigrations; | ||
|
||
class ResetPasswordControllerTest extends TestCase | ||
{ | ||
use DatabaseMigrations; | ||
|
||
public function testResetSuccessfully() | ||
{ | ||
$this->post('api/reset', [ | ||
'email' => 'test@email.com', | ||
'token' => 'my_super_secret_code', | ||
'password' => 'mynewpass', | ||
'password_confirmation' => 'mynewpass' | ||
])->seeJson([ | ||
'status' => 'ok' | ||
])->assertResponseOk(); | ||
} | ||
|
||
public function testResetSuccessfullyWithTokenRelease() | ||
{ | ||
Config::set('boilerplate.reset_password.release_token', true); | ||
|
||
$this->post('api/reset', [ | ||
'email' => 'test@email.com', | ||
'token' => 'my_super_secret_code', | ||
'password' => 'mynewpass', | ||
'password_confirmation' => 'mynewpass' | ||
])->seeJsonStructure([ | ||
'status', | ||
'token' | ||
])->seeJson([ | ||
'status' => 'ok' | ||
])->assertResponseOk(); | ||
} | ||
|
||
public function testResetReturnsProcessError() | ||
{ | ||
$this->post('api/reset', [ | ||
'email' => 'unknown@email.com', | ||
'token' => 'this_code_is_invalid', | ||
'password' => 'mynewpass', | ||
'password_confirmation' => 'mynewpass' | ||
])->seeJsonStructure([ | ||
'error' | ||
])->assertResponseStatus(500); | ||
} | ||
|
||
public function testResetReturnsValidationError() | ||
{ | ||
$this->post('api/reset', [ | ||
'email' => 'test@email.com', | ||
'token' => 'my_super_secret_code', | ||
'password' => 'mynewpass' | ||
])->seeJsonStructure([ | ||
'error' | ||
])->assertResponseStatus(422); | ||
} | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$user = new User([ | ||
'name' => 'Test User', | ||
'email' => 'test@email.com', | ||
'password' => '123456' | ||
]); | ||
$user->save(); | ||
|
||
DB::table('password_resets')->insert([ | ||
'email' => 'test@email.com', | ||
'token' => 'my_super_secret_code', | ||
'created_at' => Carbon::now() | ||
]); | ||
} | ||
} |