136 lines
4.0 KiB
PHP
136 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Mail\AdminPasswordResetEmail;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\IamPrincipal;
|
|
use App\Models\IamPrincipalOtp;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class LoginController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return view('Admin.pages.auth.login');
|
|
}
|
|
|
|
public function login(Request $request)
|
|
{
|
|
dd($request);
|
|
$validatedData = $request->validate([
|
|
'email' => 'required|email',
|
|
'password' => 'required|string',
|
|
]);
|
|
|
|
$user = IamPrincipal::where('email_address', $validatedData['email'])->first();
|
|
|
|
if ($user) {
|
|
if (Hash::check($validatedData['password'], $user->password)) {
|
|
|
|
Auth::guard('admin')->login($user);
|
|
return jsonResponseWithSuccessMessage(__('success.authentic_success'), 200);
|
|
} else {
|
|
return jsonResponseWithErrorMessage(__('auth.password'), 401);
|
|
}
|
|
} else {
|
|
return jsonResponseWithErrorMessage(__('auth.email'), 401);
|
|
}
|
|
}
|
|
|
|
public function forgot_password()
|
|
{
|
|
return view('Admin.pages.auth.forgot_password');
|
|
}
|
|
|
|
public function add_forgot_password(Request $request)
|
|
{
|
|
$user = DB::table('iam_principal')->where('email_address', $request->email)->first();
|
|
if (!$user) {
|
|
return jsonResponseWithErrorMessage(__('auth.email'), 404);
|
|
}
|
|
|
|
$otp = rand(1234, 9999);
|
|
$expirationTime = now()->addMinutes(5);
|
|
|
|
$data = [
|
|
'principal_xid' => $user->id,
|
|
'otp_code' => $otp,
|
|
'otp_purpose' => 'admin forgot password',
|
|
'valid_till' => $expirationTime,
|
|
'created_at' => Carbon::now(),
|
|
];
|
|
|
|
$user_token = DB::table('iam_principal_otp')->where('principal_xid', $user->id)->first();
|
|
if ($user_token) {
|
|
DB::table('iam_principal_otp')
|
|
->where('principal_xid', $user->id)
|
|
->update($data);
|
|
} else {
|
|
DB::table('iam_principal_otp')
|
|
->insert($data);
|
|
}
|
|
|
|
Session::put('admin_data', $data);
|
|
|
|
$sessionDetails = Session::get('admin_data');
|
|
|
|
Mail::to($request->email)->send(new AdminPasswordResetEmail($data));
|
|
|
|
return jsonResponseWithSuccessMessage(__('success.authentic_success'), 200);
|
|
}
|
|
|
|
public function otp_page()
|
|
{
|
|
return view('Admin.pages.auth.otp');
|
|
}
|
|
|
|
public function verify_otp(Request $request)
|
|
{
|
|
try {
|
|
$admin = IamPrincipalOtp::where('principal_xid', $request->id)
|
|
->where('otp_code', $request->otp)
|
|
->where('valid_till', '>', now())
|
|
->first();
|
|
|
|
if ($admin) {
|
|
return jsonResponseWithSuccessMessage(__('success.confirmed_password'), 200);
|
|
} else {
|
|
return jsonResponseWithErrorMessage(__('auth.otp_expired_invalid'), 401);
|
|
}
|
|
} catch (\Exception $e) {
|
|
Log::error('Passport function failed: ' . $e->getMessage());
|
|
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
|
|
return response()->json(['error' => $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
|
|
public function reset_password_page()
|
|
{
|
|
return view('Admin.pages.auth.password_reset');
|
|
}
|
|
|
|
public function updatePassword(Request $request)
|
|
{
|
|
$id = $request->reset_id;
|
|
$user = IamPrincipal::find($id);
|
|
if (!$user) {
|
|
return jsonResponseWithErrorMessage(__('auth.user_not_found'), 404);
|
|
}
|
|
|
|
$user->update([
|
|
'password' => Hash::make($request->confirm_password),
|
|
]);
|
|
|
|
return jsonResponseWithSuccessMessage(__('success.update_data'), 200);
|
|
}
|
|
}
|