admin email update module

This commit is contained in:
Ritikesh yadav
2024-07-03 11:36:57 +05:30
parent f1cf2faa5e
commit 403aaba17a
6 changed files with 475 additions and 224 deletions

View File

@@ -11,6 +11,7 @@ use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use DataTables;
use Mail;
use App\Mail\sendEmailOTP;
use App\Mail\UpdatePasswordMail;
class AdminProfileController extends Controller
@@ -21,6 +22,51 @@ class AdminProfileController extends Controller
return view('Admin.Pages.manage_sub_admin.edit_admin_profile', compact('data'));
}
public function sendOtpOnMail(Request $request)
{
// dd($request->all());
$validator = validator::make($request->all(),['new_email'=>'required|unique:users,email'],['requreid'=>'Email field is required.','unique'=>'Email should be unique.']);
$validateMessage = validationErrorMessage($validator);
if($validateMessage)
{
return response()->json(['status'=>400,'message'=>$validateMessage]);
}
$OTP = rand(1000,9999);
Mail::to($request->new_email)->send(new sendEmailOTP($OTP));
session()->forget(['email_update_otp','old_email','new_email']);
session()->put('email_update_otp',$OTP);
session()->put('old_email',$request->old_email);
session()->put('new_email',$request->new_email);
if(session()->has('email_update_otp') || session()->has('old_email') || session()->has('new_email'))
{
return response()->json(['status'=>200,'message'=>"OTP has been send to your mail."]);
}
return response()->json(['status'=>400,'message'=>"Error in sending OTP."]);
}
public function verifyEmailOtp(Request $request)
{
$otp = session()->get('email_update_otp');
$old_email = session()->get('old_email');
$new_email = session()->get('new_email');
// dd($new_email,$old_email);
if(!$otp && !$old_email && !$new_email)
{
return response()->json(['status'=>400,'message'=>'Enter your email again.']);
}
if((int)$otp != (int)$request->email_otp)
{
return response()->json(['status'=>400,'message'=>'OTP not matched.']);
}
$updated = User::where('email',$old_email)->update(['email'=>$new_email]);
if($updated)
{
return response()->json(['status'=>200,'message'=>'Email updated successfully']);
}
}
public function getUsers(Request $request)
{
if ($request->ajax()) {

36
app/Mail/sendEmailOTP.php Normal file
View File

@@ -0,0 +1,36 @@
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class sendEmailOTP extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
protected $otp;
public function __construct($otp)
{
$this->otp = $otp;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$otp = $this->otp;
return $this->subject('OTP from Jericho Altenatives')->view('Admin.email.otp-mail',compact('otp'));
}
}

View File

@@ -1515,3 +1515,17 @@ label.error {
.modal .tabdiv label.error {
padding: 6px 6px 6px 20px !important;
}
a.edit_email {
position: absolute;
top: 45px;
right: 22px;
color: #000;
}
.email {
position: relative;
}
#admin_email_update label, #admin_email_update input {
width: 100%;
}

View File

@@ -20,19 +20,25 @@
<div class="col-md-6 fv-row mt-2">
<label class="required fs-6 fw-semibold mb-2">Name</label>
<input type="hidden" name="update_id" value="{{ $data->id }}">
<input type="text" class="form-control form-control-solid" placeholder="" name="name" value="{{$data->name}}" />
<input type="text" class="form-control form-control-solid" placeholder=""
name="name" value="{{ $data->name }}" />
</div>
<div class="col-md-6 fv-row mt-2">
<label class="required fs-6 fw-semibold mb-2">email</label>
<input type="email" class="form-control form-control-solid" placeholder="" name="email" value="{{$data->email}}" />
<div class="col-md-6 fv-row mt-2 email">
<label class="required fs-6 fw-semibold mb-2">Email</label>
<input type="email" class="form-control form-control-solid" placeholder=""
id="email" name="email" readonly value="{{ $data->email }}" />
<a class="edit_email" data-old-email="{{ $data->email }}"><i
class="fa-solid fs-5 fa-pen-to-square"></i></a>
</div>
<div class="col-md-6 fv-row mt-4">
<label class="required fs-6 fw-semibold mb-2">Contact Number</label>
<input type="number" class="form-control form-control-solid" placeholder="" name="mobile_number" value="{{$data->contact_number}}" />
<input type="number" class="form-control form-control-solid" placeholder=""
name="mobile_number" value="{{ $data->contact_number }}" />
</div>
<div class="col-md-6 fv-row mt-4">
<label class="required fs-6 fw-semibold mb-2">Profile Image</label>
<input type="file" class="form-control form-control-solid" placeholder="" name="profile_image" />
<input type="file" class="form-control form-control-solid" placeholder=""
name="profile_image" />
</div>
</div>
<div class="d-flex justify-content-center my-6">
@@ -64,15 +70,18 @@
<div class="col-md-6 fv-row mt-2">
<label class="fs-6 fw-semibold mb-2">Current Password</label>
<input type="hidden" name="updateId" value="{{ $data->id }}">
<input type="password" class="form-control form-control-solid" placeholder="" name="current_password" id="current_password" />
<input type="password" class="form-control form-control-solid" placeholder=""
name="current_password" id="current_password" />
</div>
<div class="col-md-6 fv-row mt-2">
<label class="fs-6 fw-semibold mb-2">New Password</label>
<input type="password" class="form-control form-control-solid" placeholder="" id="password" name="password" />
<input type="password" class="form-control form-control-solid" placeholder=""
id="password" name="password" />
</div>
<div class="col-md-6 fv-row mt-2">
<label class="fs-6 fw-semibold mb-2">Confirm Password</label>
<input type="password" class="form-control form-control-solid" placeholder="" id="password_confirmation" name="password_confirmation" />
<input type="password" class="form-control form-control-solid" placeholder=""
id="password_confirmation" name="password_confirmation" />
</div>
</div>
<div class="submit_btn d-flex justify-content-center my-6">
@@ -86,9 +95,203 @@
</div>
</div>
</div>
{{-- edit email popup modal start --}}
<div class="modal fade" id="getEmailModal" aria-hidden="true" aria-labelledby="getEmailModalLabel" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<form id="admin_email_update">
@csrf
<div class="modal-header">
<h5 class="modal-title" id="getEmailModalLabel">Edit Email</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
{{-- Show a second modal and hide this one with the button below. --}}
<div>
<label for="email" class="mb-2">New Email</label>
<input type="hidden" name="old_email" id="old_email">
<input type="email" placeholder="Enter your email" name="new_email" id="new_email"
class="form-control form-control-solid valid">
</div>
</div>
<div class="modal-footer">
{{-- <button class="btn btn-primary" data-bs-target="#getEmailModal2" data-bs-toggle="modal" data-bs-dismiss="modal">Send OTP</button> --}}
<button class="btn btn-primary" type="submit" id="sendEmailOtp">Send OTP</button>
</div>
</form>
</div>
</div>
</div>
{{-- edit email popup modal end --}}
{{-- edit email otp popup modal start --}}
<div class="modal fade" id="email_otp" aria-hidden="true" aria-labelledby="getEmailModalLabel" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<form id="verify_email_otp">
@csrf
<div class="modal-header">
<h5 class="modal-title" id="getEmailModalLabel">OTP</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
{{-- Show a second modal and hide this one with the button below. --}}
<div>
<label for="email_otp" class="mb-2">Enter OTP</label>
<input type="number" name="email_otp" id="email_otp" class="form-control form-control-solid valid">
</div>
</div>
<div class="modal-footer">
{{-- <button class="btn btn-primary" data-bs-target="#getEmailModal2" data-bs-toggle="modal" data-bs-dismiss="modal">Send OTP</button> --}}
<button class="btn btn-primary" type="submit" id="verifyEmailOtp">Verify OTP</button>
</div>
</form>
</div>
</div>
</div>
{{-- edit email otp popup modal end --}}
@endsection
@section('scripts')
<script>
$('.edit_email').on('click', function() {
$('#old_email').val($(this).data('old-email'));
$('#getEmailModal').modal('show');
});
$('#admin_email_update').validate({
ignore: [],
debug: false,
rules: {
new_email: {
required: true,
},
},
messages: {
new_email: {
required: 'Enter your email',
},
},
submitHandler: function(form) {
var formData = new FormData(form);
$.ajax({
url: "{{ route('send-otp-on-mail') }}",
type: 'POST',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: formData,
dataType: 'json',
contentType: false, // Required for FormData
processData: false, // Required for FormData
success: function(result) {
if (result.status == 200) {
// Handle success case
// toastr.success(result.message);
$('#getEmailModal').modal('hide')
$('#email_otp').modal('show')
// setTimeout(function(){},3000)
} else {
toastr.warning(result.message);
}
},
error: function(xhr, status, error) {
console.error(xhr.responseText);
toastr.error('Error occurred while sending request.');
}
});
}
});
$('#verify_email_otp').validate({
ignore: [],
debug: false,
rules: {
email_otp: {
required: true,
},
},
messages: {
email_otp: {
required: 'Enter your OTP',
},
},
submitHandler: function(form) {
var formData = new FormData(form);
$.ajax({
url: "{{ route('verify-email-update-otp') }}",
type: 'POST',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: formData,
dataType: 'json',
contentType: false, // Required for FormData
processData: false, // Required for FormData
success: function(result) {
if (result.status == 200) {
// Handle success case
// toastr.success(result.message);
$('#getEmailModal').modal('hide')
$('#email_otp').modal('hide')
toastr.success(result.message);
setTimeout(function() {
window.reload()
}, 3000)
} else {
toastr.warning(result.message);
}
},
error: function(xhr, status, error) {
console.error(xhr.responseText);
toastr.error('Error occurred while sending request.');
}
});
}
});
// $('#admin_email_update').validate({
// ignore: [],
// debug: false,
// rules:{
// new_email:{
// required: true,
// },
// },
// messages:{
// new_email:{
// required: 'Enter your email',
// },
// },
// submitHandler: function(form){
// var formData = FormData(form);
// $.ajax({
// url:"{{ route('send-otp-on-mail') }}",
// type:'POST',
// // $.ajaxSetup({
// headers: {
// 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
// },
// // });
// data: formData,
// dataType: 'json',
// // contentType: false,
// // processData: false,
// success:function(result)
// {
// if(result.status == 200)
// {
// }
// else{
// toastr.warning(result.message);
// }
// }
// })
// }
// });
$('#update_profile').validate({
ignore: [],
debug: false,
@@ -102,8 +305,7 @@
email: "Enter your email",
mobile_number: "Enter your mobile number",
},
submitHandler: function(form)
{
submitHandler: function(form) {
var formData = new FormData(form);
$.ajax({
url: "{{ route('update-profile') }}",
@@ -112,17 +314,13 @@
dataType: 'json',
contentType: false,
processData: false,
success: function(result)
{
if (result.status == 200)
{
success: function(result) {
if (result.status == 200) {
toastr.success(result.message);
setTimeout(function() {
window.location.reload()
}
, 2000);
} else
{
}, 2000);
} else {
toastr.warning(result.message);
}
}
@@ -175,8 +373,7 @@
equalTo: "Your password does not match",
},
},
submitHandler: function(form)
{
submitHandler: function(form) {
var formData = new FormData(form);
$.ajax({
url: "{{ route('update-password') }}",
@@ -185,16 +382,13 @@
contentType: false,
processData: false,
dataType: 'json',
success: function(result)
{
if (result.status == 200)
{
success: function(result) {
if (result.status == 200) {
toastr.success(result.message);
setTimeout(function() {
window.location.href = '{{route("admin.login")}}';
window.location.href = '{{ route('admin.login') }}';
}, 2000);
} else
{
} else {
toastr.warning(result.message);
}
}

View File

@@ -1,44 +1,3 @@
<!-- <!DOCTYPE html>
<html>
<head>
<title>ItsolutionStuff.com</title>
</head>
<body>
<h1>{{ $mailData['title'] }}</h1>
<p>{{ $mailData['body'] }}</p>
<p>{{$otp}}</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Thank you</p>
</body>
</html> -->
<!doctype html>

View File

@@ -172,6 +172,8 @@ Route::get('/view-clear', function () {
//admin update profile route
Route::get('view-profile', [AdminProfileController::class, 'viewProfile'])->name('view-profile');
Route::post('send-otp-on-mail', [AdminProfileController::class, 'sendOtpOnMail'])->name('send-otp-on-mail');
Route::post('verify-email-update-otp', [AdminProfileController::class, 'verifyEmailOtp'])->name('verify-email-update-otp');
Route::post('update-profile', [AdminProfileController::class, 'editProfile'])->name('update-profile');
Route::post('update-password', [AdminProfileController::class, 'updatePassword'])->name('update-password');
Route::get('view-user-password', [AdminProfileController::class, 'viewUsersProfile'])->name('view-user-password');