Skip to content

Commit

Permalink
Forgot password feature added
Browse files Browse the repository at this point in the history
  • Loading branch information
supanadit committed Jun 28, 2020
1 parent 54037ee commit 550a96d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 13 deletions.
25 changes: 25 additions & 0 deletions app/Http/Controllers/SecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace App\Http\Controllers;

use App\Mail\ForgotPassword;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;

class SecurityController extends Controller
{
Expand Down Expand Up @@ -108,6 +111,28 @@ public function formRegister(Request $request)
}
}

public function formForgotPassword(Request $request)
{
$request->validate([
"email" => "required",
]);
$user = \App\User::where("email", $request->input("email"))->first();
if ($user != null) {
$name = $user->name;
$newPassword = Str::random(4);
$user->password = Hash::make($newPassword);
Mail::to($user->email)->send(new ForgotPassword($name, $newPassword));
$user->save();
return response()->json([
"message" => "Please check your email",
], 200);
} else {
return response()->json([
"message" => "Email " . $request->input('email') . " is not exist",
], 400);
}
}

public function formLogout(Request $request)
{
$request->session()->flush();
Expand Down
14 changes: 9 additions & 5 deletions app/Mail/ForgotPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class ForgotPassword extends Mailable
{
use Queueable, SerializesModels;

public $name;
public $password;

/**
* Create a new message instance.
*
* @return void
* @param $name
* @param $password
*/
public function __construct()
public function __construct($name, $password)
{
//
$this->name = $name;
$this->password = $password;
}

/**
Expand All @@ -28,6 +32,6 @@ public function __construct()
*/
public function build()
{
return $this->view('view.name');
return $this->view('template.email.forgot');
}
}
9 changes: 4 additions & 5 deletions resources/views/forgot.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<div class="login-box-body">
<p class="login-box-msg">Forgot Password</p>

<form action="/" method="post" id="login-form">
<form action="/" method="post" id="forgot-password-form">
<div class="form-group has-feedback">
<input type="email" class="form-control" placeholder="Email" id="email">
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
Expand Down Expand Up @@ -72,25 +72,24 @@
<script src="{{asset('plugin/iCheck/icheck.min.js')}}"></script>
<script>
$(document).ready(function () {
$("#login-form").on("submit", function (e) {
$("#forgot-password-form").on("submit", function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "/web/login",
url: "/web/forgot",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
},
data: JSON.stringify({
"email": $("#email").val(),
"password": $("#password").val()
}),
contentType: "application/json",
dataType: "json",
async: true,
success: function (result) {
toastr.success(result.message);
window.setTimeout(function () {
location.reload();
location.href = "/";
}, 500);
},
error: function (result) {
Expand Down
5 changes: 3 additions & 2 deletions resources/views/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<div class="row">
<div class="col-xs-8">
<a href="/register" class="text-center">Register new account</a><br/>
Version 1.0.2
<a href="/forgot/password" class="text-center">Forgot Password</a><br/>
</div>
<!-- /.col -->
<div class="col-xs-4">
Expand All @@ -62,7 +62,8 @@
<!-- /.col -->
</div>
</form>
{{-- <a href="/forgot/password">Forgot Password</a><br>--}}
<br/>
Version 1.0.2
</div>
<!-- /.login-box-body -->
</div>
Expand Down
5 changes: 5 additions & 0 deletions resources/views/template/email/forgot.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
Hi, {{ $name }}<br/>
This is your new password : {{ $password }}<br/>
<p>You can change it after login back</p>
</div>
3 changes: 2 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

Route::get('/', "SecurityController@initSystem");
Route::get('/login', "SecurityController@viewLogin")->name('login');
//Route::get('/forgot/password', "SecurityController@viewForgotPassword");
Route::get('/forgot/password', "SecurityController@viewForgotPassword");
Route::get('/register', "SecurityController@viewRegister");

Route::middleware('auth.web')->group(function () {
Expand Down Expand Up @@ -43,5 +43,6 @@
});

Route::post('/web/login', "SecurityController@formLogin");
Route::post('/web/forgot', "SecurityController@formForgotPassword");
Route::post('/web/register', "SecurityController@formRegister");
Route::get('/web/logout', "SecurityController@formLogout");

0 comments on commit 550a96d

Please sign in to comment.