Forgot password feature added

This commit is contained in:
Supan Adit Pratama 2020-06-28 23:57:23 +07:00
parent 54037eed87
commit 550a96d27b
6 changed files with 48 additions and 13 deletions

View File

@ -2,8 +2,11 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Mail\ForgotPassword;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
class SecurityController extends Controller class SecurityController extends Controller
{ {
@ -108,6 +111,28 @@ class SecurityController extends Controller
} }
} }
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) public function formLogout(Request $request)
{ {
$request->session()->flush(); $request->session()->flush();

View File

@ -3,7 +3,6 @@
namespace App\Mail; namespace App\Mail;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
@ -11,14 +10,19 @@ class ForgotPassword extends Mailable
{ {
use Queueable, SerializesModels; use Queueable, SerializesModels;
public $name;
public $password;
/** /**
* Create a new message instance. * 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;
} }
/** /**
@ -28,6 +32,6 @@ class ForgotPassword extends Mailable
*/ */
public function build() public function build()
{ {
return $this->view('view.name'); return $this->view('template.email.forgot');
} }
} }

View File

@ -41,7 +41,7 @@
<div class="login-box-body"> <div class="login-box-body">
<p class="login-box-msg">Forgot Password</p> <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"> <div class="form-group has-feedback">
<input type="email" class="form-control" placeholder="Email" id="email"> <input type="email" class="form-control" placeholder="Email" id="email">
<span class="glyphicon glyphicon-envelope form-control-feedback"></span> <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
@ -72,17 +72,16 @@
<script src="{{asset('plugin/iCheck/icheck.min.js')}}"></script> <script src="{{asset('plugin/iCheck/icheck.min.js')}}"></script>
<script> <script>
$(document).ready(function () { $(document).ready(function () {
$("#login-form").on("submit", function (e) { $("#forgot-password-form").on("submit", function (e) {
e.preventDefault(); e.preventDefault();
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: "/web/login", url: "/web/forgot",
headers: { headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'), 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
}, },
data: JSON.stringify({ data: JSON.stringify({
"email": $("#email").val(), "email": $("#email").val(),
"password": $("#password").val()
}), }),
contentType: "application/json", contentType: "application/json",
dataType: "json", dataType: "json",
@ -90,7 +89,7 @@
success: function (result) { success: function (result) {
toastr.success(result.message); toastr.success(result.message);
window.setTimeout(function () { window.setTimeout(function () {
location.reload(); location.href = "/";
}, 500); }, 500);
}, },
error: function (result) { error: function (result) {

View File

@ -53,7 +53,7 @@
<div class="row"> <div class="row">
<div class="col-xs-8"> <div class="col-xs-8">
<a href="/register" class="text-center">Register new account</a><br/> <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> </div>
<!-- /.col --> <!-- /.col -->
<div class="col-xs-4"> <div class="col-xs-4">
@ -62,7 +62,8 @@
<!-- /.col --> <!-- /.col -->
</div> </div>
</form> </form>
{{-- <a href="/forgot/password">Forgot Password</a><br>--}} <br/>
Version 1.0.2
</div> </div>
<!-- /.login-box-body --> <!-- /.login-box-body -->
</div> </div>

View File

@ -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>

View File

@ -15,7 +15,7 @@ use Illuminate\Support\Facades\Route;
Route::get('/', "SecurityController@initSystem"); Route::get('/', "SecurityController@initSystem");
Route::get('/login', "SecurityController@viewLogin")->name('login'); 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::get('/register', "SecurityController@viewRegister");
Route::middleware('auth.web')->group(function () { Route::middleware('auth.web')->group(function () {
@ -43,5 +43,6 @@ Route::middleware('auth.web')->group(function () {
}); });
Route::post('/web/login', "SecurityController@formLogin"); Route::post('/web/login', "SecurityController@formLogin");
Route::post('/web/forgot', "SecurityController@formForgotPassword");
Route::post('/web/register', "SecurityController@formRegister"); Route::post('/web/register', "SecurityController@formRegister");
Route::get('/web/logout', "SecurityController@formLogout"); Route::get('/web/logout', "SecurityController@formLogout");