Skip to content

Commit

Permalink
forgot password implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewstoneameritech committed Nov 16, 2017
1 parent 01187ae commit 91d8591
Show file tree
Hide file tree
Showing 38 changed files with 22,977 additions and 21,308 deletions.
19 changes: 19 additions & 0 deletions app/Api/V1/Controllers/ForgotPasswordRequest.php
@@ -0,0 +1,19 @@
<?php

namespace App\Api\V1\Controllers;

use Config;
use Dingo\Api\Http\FormRequest;

class ForgotPasswordRequest extends FormRequest
{
public function rules()
{
return Config::get('email' => 'required|email');
}

public function authorize()
{
return true;
}
}
30 changes: 23 additions & 7 deletions app/Http/Controllers/Auth/ForgotPasswordController.php
@@ -1,10 +1,10 @@
<?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
{
/*
Expand All @@ -16,10 +16,8 @@ class ForgotPasswordController extends Controller
| 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.
*
Expand All @@ -29,4 +27,22 @@ 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(['status','Email with reset link sent successfully'],200);
}
$this->sendResetLinkEmail($request);
return response()->json(['status','Email with reset link sent successfully'],200);
}
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/LoginController.php
Expand Up @@ -32,7 +32,7 @@ public function login(Request $request)
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json([
"error" => "invalid_credentials",
"message" => "The user credentials were incorrect."
"message" => "The user credentials were incorrect. "
], 401);
}
} catch (JWTException $e) {
Expand Down
50 changes: 36 additions & 14 deletions app/Http/Controllers/Auth/ResetPasswordController.php
@@ -1,10 +1,10 @@
<?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
{
/*
Expand All @@ -16,17 +16,8 @@ class ResetPasswordController extends Controller
| 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;

/**
* Where to redirect users after resetting their password.
*
* @var string
*/
protected $redirectTo = '/home';

use ResetsPasswords;
/**
* Create a new controller instance.
*
Expand All @@ -36,4 +27,35 @@ 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);
}
}
32 changes: 32 additions & 0 deletions app/Notifications/ResetPassword.php
@@ -0,0 +1,32 @@
<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class ResetPassword extends Notification
{
public $token;
public $email;

public function __construct($token,$email)
{
$this->token = $token;
$this->email = $email;
}

public function via($notifiable)
{
return ['mail'];
}

public function toMail($notifiable)
{
return (new MailMessage)
->subject('Your Reset Password Link')
->line('You are receiving this email because we received a password reset request for your account.')
->action('Reset Password', url('reset-password', $this->token)."/".str_replace("@","29gnmLTv686QsnV",$this->email))
->line('If you did not request a password reset, no further action is required.');
}
}
15 changes: 15 additions & 0 deletions app/Transformers/json.php
@@ -0,0 +1,15 @@
<?php

namespace App\Transformers;

class Json
{
public static function response($data = null, $message = null, $status = null)
{
return [
'data' => $data,
'message' => $message,
'status' => $status,
];
}
}
7 changes: 7 additions & 0 deletions app/User.php
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Notifications\ResetPassword as ResetPasswordNotification;

class User extends Authenticatable
{
Expand All @@ -26,4 +27,10 @@ class User extends Authenticatable
protected $hidden = [
'password', 'remember_token',
];

public function sendPasswordResetNotification($token)
{
// Your your own implementation.
$this->notify(new ResetPasswordNotification($token, $this->getEmailForPasswordReset()));
}
}
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 91d8591

Please sign in to comment.