started work on OTP and disabled registration
This commit is contained in:
@@ -4,6 +4,9 @@ namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
||||
use App\Mail\LoginOtpMail;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class LoginController extends Controller
|
||||
{
|
||||
@@ -37,4 +40,36 @@ class LoginController extends Controller
|
||||
$this->middleware('guest')->except('logout');
|
||||
$this->middleware('auth')->only('logout');
|
||||
}
|
||||
public function authenticate(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'email' => 'required|email',
|
||||
'password' => 'required',
|
||||
]);
|
||||
|
||||
$staff = StaffMember::where('email', $request->email)->where('status', 'ACTIVE')->first();
|
||||
|
||||
// 1. Check if staff exists and password is correct
|
||||
if ($staff && Hash::check($request->password, $staff->password)) {
|
||||
|
||||
// 2. Generate 6-digit OTP
|
||||
$otp = rand(100000, 999999);
|
||||
|
||||
// 3. Save to database with 10-minute expiration
|
||||
$staff->update([
|
||||
'otp_code' => $otp,
|
||||
'otp_expires_at' => now()->addMinutes(10)
|
||||
]);
|
||||
|
||||
// 4. Send Email
|
||||
Mail::to($staff->email)->send(new LoginOtpMail($otp));
|
||||
|
||||
// 5. Store staff ID in session
|
||||
session(['otp_staff_id' => $staff->id]);
|
||||
|
||||
return redirect()->route('otp.verify');
|
||||
}
|
||||
|
||||
return back()->withErrors(['email' => 'Invalid credentials or inactive account.']);
|
||||
}
|
||||
}
|
||||
|
||||
118
app/Http/Controllers/OfficeLocationController.php
Normal file
118
app/Http/Controllers/OfficeLocationController.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\OfficeLocation;
|
||||
use App\Models\StaffMember;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class OfficeLocationController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$locations = OfficeLocation::with('manager', 'documents')
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate(9);
|
||||
|
||||
$staffMembers = StaffMember::where('status', 'ACTIVE')->orderBy('name')->get();
|
||||
|
||||
$countries = DB::table('countries')->orderBy('en_short_name', 'asc')->get();
|
||||
|
||||
return view('offices.index', compact('locations', 'staffMembers', 'countries'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'country' => 'required|string|max:191',
|
||||
'city' => 'required|string|max:191',
|
||||
'physical_address' => 'nullable|string',
|
||||
'postal_address' => 'nullable|string|max:191',
|
||||
'zip_code' => 'nullable|string|max:15',
|
||||
'office_phone' => 'nullable|string|max:20',
|
||||
'country_manager_id' => 'nullable|integer',
|
||||
]);
|
||||
|
||||
OfficeLocation::create($validated);
|
||||
|
||||
return response()->json(['success' => true, 'message' => 'Office location added successfully!']);
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$location = OfficeLocation::findOrFail($id);
|
||||
|
||||
$validated = $request->validate([
|
||||
'country' => 'required|string|max:191',
|
||||
'city' => 'required|string|max:191',
|
||||
'physical_address' => 'nullable|string',
|
||||
'postal_address' => 'nullable|string|max:191',
|
||||
'zip_code' => 'nullable|string|max:15',
|
||||
'office_phone' => 'nullable|string|max:20',
|
||||
'country_manager_id' => 'nullable|integer',
|
||||
]);
|
||||
|
||||
$location->update($validated);
|
||||
|
||||
return response()->json(['success' => true, 'message' => 'Office location updated successfully!']);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
OfficeLocation::findOrFail($id)->delete();
|
||||
return response()->json(['success' => true, 'message' => 'Office location removed.']);
|
||||
}
|
||||
|
||||
// --- ADD THESE METHODS TO OfficeLocationController.php ---
|
||||
|
||||
public function showDocuments($id)
|
||||
{
|
||||
$location = OfficeLocation::with('documents')->findOrFail($id);
|
||||
return view('offices.documents', compact('location'));
|
||||
}
|
||||
|
||||
public function storeDocument(Request $request, $id)
|
||||
{
|
||||
$location = OfficeLocation::findOrFail($id);
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:191',
|
||||
'file' => 'required|file|mimes:pdf,jpg,jpeg,png,doc,docx|max:10240',
|
||||
'validity_period' => 'required|string',
|
||||
// Require a date UNLESS the period is 'On Demand'
|
||||
'next_renewal_date' => 'nullable|required_unless:validity_period,On Demand|date'
|
||||
]);
|
||||
|
||||
$file = $request->file('file');
|
||||
$filename = time() . '_' . preg_replace('/\s+/', '_', $file->getClientOriginalName());
|
||||
$filePath = $file->storeAs("office_documents/{$location->id}", $filename, 'public');
|
||||
|
||||
// Get current staff ID
|
||||
$staff = \App\Models\StaffMember::where('email', auth()->user()->email)->first();
|
||||
|
||||
\App\Models\OfficeLocationDocument::create([
|
||||
'office_location_id' => $location->id,
|
||||
'name' => $request->name,
|
||||
'file_path' => $filePath,
|
||||
'file_extension' => $file->getClientOriginalExtension(),
|
||||
'validity_period' => $request->validity_period,
|
||||
'next_renewal_date' => $request->validity_period === 'On Demand' ? null : $request->next_renewal_date,
|
||||
'uploaded_by' => $staff ? $staff->id : null,
|
||||
]);
|
||||
|
||||
return response()->json(['success' => true, 'message' => 'Document archived successfully!']);
|
||||
}
|
||||
|
||||
public function destroyDocument($docId)
|
||||
{
|
||||
$document = \App\Models\OfficeLocationDocument::findOrFail($docId);
|
||||
|
||||
if ($document->file_path && \Illuminate\Support\Facades\Storage::disk('public')->exists($document->file_path)) {
|
||||
\Illuminate\Support\Facades\Storage::disk('public')->delete($document->file_path);
|
||||
}
|
||||
|
||||
$document->delete();
|
||||
return response()->json(['success' => true, 'message' => 'Document deleted.']);
|
||||
}
|
||||
}
|
||||
69
app/Http/Controllers/OtpController.php
Normal file
69
app/Http/Controllers/OtpController.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\StaffMember;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class OtpController extends Controller
|
||||
{
|
||||
public function show()
|
||||
{
|
||||
if (!session()->has('otp_staff_id')) {
|
||||
return redirect()->route('login');
|
||||
}
|
||||
return view('auth.otp-verify');
|
||||
}
|
||||
|
||||
public function verify(Request $request)
|
||||
{
|
||||
$request->validate(['otp' => 'required|numeric|digits:6']);
|
||||
|
||||
$staff = StaffMember::findOrFail(session('otp_staff_id'));
|
||||
|
||||
// Check if OTP matches and is not expired
|
||||
if ($staff->otp_code == $request->otp && now()->lessThanOrEqualTo($staff->otp_expires_at)) {
|
||||
|
||||
// Clear the OTP data
|
||||
$staff->update([
|
||||
'otp_code' => null,
|
||||
'otp_expires_at' => null
|
||||
]);
|
||||
|
||||
// Log the staff member in
|
||||
// NOTE: If using a specific guard, use Auth::guard('staff')->login($staff);
|
||||
Auth::login($staff);
|
||||
|
||||
// Clear the temp session
|
||||
session()->forget('otp_staff_id');
|
||||
|
||||
return redirect()->intended('/');
|
||||
}
|
||||
|
||||
return back()->withErrors(['otp' => 'The verification code is invalid or has expired.']);
|
||||
}
|
||||
public function resend(Request $request)
|
||||
{
|
||||
// Ensure the session still exists
|
||||
if (!session()->has('otp_staff_id')) {
|
||||
return redirect()->route('login')->withErrors(['email' => 'Session expired. Please log in again.']);
|
||||
}
|
||||
|
||||
$staff = StaffMember::findOrFail(session('otp_staff_id'));
|
||||
|
||||
// Generate a new 6-digit OTP
|
||||
$otp = rand(100000, 999999);
|
||||
|
||||
// Update database with new code and fresh 10-minute expiration
|
||||
$staff->update([
|
||||
'otp_code' => $otp,
|
||||
'otp_expires_at' => now()->addMinutes(10)
|
||||
]);
|
||||
|
||||
// Send the new code via email
|
||||
Mail::to($staff->email)->send(new LoginOtpMail($otp));
|
||||
|
||||
return back()->with('success', 'A new verification code has been sent to your email.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user