230 lines
7.3 KiB
PHP
Executable File
230 lines
7.3 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use CodeIgniter\HTTP\IncomingRequest;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
use App\Models\UserModel;
|
|
use App\Models\AuthHistoryModel;
|
|
|
|
use App\Libraries\AuthLogout;
|
|
|
|
class LoginController extends BaseController
|
|
{
|
|
use ResponseTrait;
|
|
|
|
public function index()
|
|
{
|
|
|
|
// $session_data = ['isLoggedIn' => True ,'userid' => '25'];
|
|
// set_session_data($session_data);
|
|
// $isLoggedIn = check_session();
|
|
$isLoggedIn = check_session();
|
|
$hasCookie = check_cookie();
|
|
if ($isLoggedIn && $hasCookie) {
|
|
return redirect()->to(base_url('/dashboard/view'));
|
|
}
|
|
return view('login');
|
|
}
|
|
|
|
public function receiveGoogleOAuthResponse()
|
|
{
|
|
$UserModel = new UserModel();
|
|
//echo 'DONE';die();
|
|
if ($this->request->getGet('code')) {
|
|
$code = (string) $this->request->getGet('code');
|
|
log_message('error', 'Get OAuth Responce Code Sucessfully');
|
|
log_message('error', 'OAuthResponceCode : `'.$code.'`');
|
|
$value = googleOAuthLogin($this->request->getGet('code'));
|
|
if($value){
|
|
$user = $UserModel->getUserByEmail($value->email);
|
|
if($user){
|
|
if($user->is_active !== '0'){
|
|
|
|
$user_team = $UserModel->getUserTeamsByUserID($user->id);
|
|
// dd($user_team);
|
|
session()->regenerate(true);
|
|
$session_data = [
|
|
'isLoggedIn' => True ,
|
|
'userid' => $user->id,
|
|
'userData' => $user,
|
|
'userProfile' => $value->picture,
|
|
'user_team' => $user_team,
|
|
];
|
|
$path = getenv('cookie.Path');
|
|
$domain = getenv('cookie.Domain');
|
|
$https = getenv('ccokie.secure');
|
|
// setcookie('session_data', json_encode($session_data), time() + 12 * 60 * 60, $path, $domain, $https, true);
|
|
set_session_data($session_data);
|
|
|
|
// Bind session to device
|
|
set_session_data(['fingerprint' => generateFingerprint()]);
|
|
|
|
log_message('error', 'Set The UserId : `'. $user->id .'` in Session');
|
|
log_message('error', 'User Login Sucessfully');
|
|
|
|
$this->getUserDeviceInfo($user->id, 'NhanceUser');
|
|
return redirect()->to(base_url('/dashboard/view'));
|
|
|
|
}else{
|
|
log_message('error', 'User Not Active');
|
|
session()->setFlashdata('error', 'User Not Active');
|
|
return redirect()->to(base_url('login'));
|
|
}
|
|
}else{
|
|
|
|
log_message('error', 'User Not Registered');
|
|
session()->setFlashdata('error', 'User Not Registered');
|
|
return redirect()->to(base_url('login'));
|
|
}
|
|
}
|
|
|
|
}else{
|
|
|
|
return redirect()->to(base_url('login'));
|
|
}
|
|
}
|
|
|
|
public function initiateGoogleOAuth()
|
|
{
|
|
return googleOAuthLogin(false);
|
|
}
|
|
|
|
public function logout()
|
|
{
|
|
// $path = getenv('cookie.Path');
|
|
// session()->regenerate(true);
|
|
// session()->destroy();
|
|
|
|
// $path = getenv('cookie.Path');
|
|
// $domain = getenv('cookie.Domain');
|
|
// $https = getenv('cookie.secure');
|
|
|
|
// setcookie('session_data',null, time() - 42000, $path, $domain, $https, true);
|
|
// // return redirect()->to(base_url('login'));
|
|
// return redirect()->to(base_url('login'))
|
|
// ->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
|
|
// ->setHeader('Pragma', 'no-cache')
|
|
// ->setHeader('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT');
|
|
|
|
|
|
return AuthLogout::logout();
|
|
}
|
|
|
|
public function getUserDeviceInfo($userId, $type_of_user)
|
|
{
|
|
|
|
// Load the UserAgent library
|
|
$userAgent = $this->request->getUserAgent();
|
|
|
|
// Get the user's platform (e.g., Windows, Mac, Linux)
|
|
$platform = $userAgent->getPlatform();
|
|
|
|
// Get the user's browser (e.g., Chrome, Firefox, Safari)
|
|
$browser = $userAgent->getBrowser();
|
|
|
|
// Get the user's IP address
|
|
$ipAddress = $this->request->getIPAddress();
|
|
|
|
|
|
$datd = [
|
|
'user_id' => $userId,
|
|
'user_type' => $type_of_user,
|
|
'ip' => $ipAddress,
|
|
'platform' => $platform,
|
|
'broswer' => $browser,
|
|
];
|
|
// print_r($datd);
|
|
// die();
|
|
$AuthHistoryModel = new AuthHistoryModel;
|
|
$insertAuthHistory = $AuthHistoryModel->insert($datd);
|
|
return $datd;
|
|
}
|
|
|
|
public function loginPos()
|
|
{
|
|
return view('pos_login');
|
|
}
|
|
|
|
public function getVerifyPosMobileNo()
|
|
{
|
|
$json = $this->request->getJSON();
|
|
$mobile = $json->mobile ?? null;
|
|
|
|
if (empty($mobile)) {
|
|
return $this->respond([
|
|
'status' => false,
|
|
'code' => 400,
|
|
'data' => 'Mobile number is required'
|
|
], 200);
|
|
}
|
|
|
|
$cleanMobile = preg_replace('/^\+91/', '', $mobile); // Remove +91 if present
|
|
|
|
$userModel = new \App\Models\UserModel(); // Update namespace if needed
|
|
$user = $userModel->where('mobile', $cleanMobile)->first();
|
|
|
|
if (!$user) {
|
|
return $this->respond([
|
|
'status' => false,
|
|
'code' => 404,
|
|
'data' => 'User not found'
|
|
], 200);
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => true,
|
|
'code' => 200,
|
|
'data' => $user // Optional: Replace with true or limited fields if needed
|
|
], 200);
|
|
}
|
|
|
|
public function getVerifiedPosUserData()
|
|
{
|
|
$json = $this->request->getJSON();
|
|
$mobile = $json->mobile ?? null;
|
|
|
|
if (!$mobile) {
|
|
return $this->respond([
|
|
'status' => false,
|
|
'code' => 400,
|
|
'message' => 'Mobile number is required'
|
|
], 200); // always return 200 with internal status
|
|
}
|
|
|
|
$cleanMobile = preg_replace('/^\+91/', '', $mobile);
|
|
|
|
$userModel = new \App\Models\UserModel();
|
|
$user = $userModel->where('mobile', $cleanMobile)->first();
|
|
|
|
if (!$user) {
|
|
return $this->respond([
|
|
'status' => false,
|
|
'code' => 404,
|
|
'message' => 'Employee not found'
|
|
], 200);
|
|
}
|
|
|
|
// Set session
|
|
session()->set([
|
|
'user_id' => $user['id'],
|
|
'user_name' => $user['first_name'],
|
|
'user_email' => $user['email'],
|
|
'is_logged_in' => true,
|
|
]);
|
|
|
|
return $this->respond([
|
|
'status' => true,
|
|
'code' => 200,
|
|
'message' => 'Login successful',
|
|
'redirect_url' => base_url('/dashboard/view')
|
|
], 200);
|
|
}
|
|
|
|
|
|
}
|