File: /var/www/staging.fixgini.com/app/Http/Controllers/AuthenticationController.php
<?php
namespace App\Http\Controllers;
use App\Services\DeviceService;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
class AuthenticationController extends Controller
{
protected $deviceService;
public function __construct(DeviceService $deviceService)
{
$this->deviceService = $deviceService;
}
protected function getLocation()
{
try {
$clientIp = request()->getClientIp();
// Check if there's a country data in the session and if the IP matches
if (Session::has('country') && Session::get('country')['ip'] === $clientIp) {
info('IP address unchanged, using session data');
return Session::get('country'); // Use the session data directly
}
// If IP is different or no data in session, call the external API
$response = Http::get("https://ipinfo.io/{$clientIp}?token=55690b2a8bf492");
if ($response->successful()) {
$data = $response->json();
$location = explode(',', $data['loc'] ?? '');
$latitude = $location[0] ?? '';
$longitude = $location[1] ?? '';
// Include city and state in the response if needed
$data = [
'ip' => $clientIp ?? '',
'latitude' => $latitude ?? '',
'longitude' => $longitude ?? '',
];
// Store the new data in the session
Session::put('country', $data);
return $data;
}
} catch (\Exception $e) {
Log::error('Location fetching error: ' . $e->getMessage());
}
return null; // Return null if location fetching fails
}
protected function getDevice()
{
if (!Session::has('device_name')) {
$deviceName = $this->deviceService->getDeviceName();
Session::put('device_name', $deviceName);
}
}
protected function getLocationAndDevice()
{
$this->getLocation();
$this->getDevice();
}
public function welcome()
{
$this->getLocationAndDevice();
return view('welcome');
}
public function decisionScreen()
{
return view("auth.decision-screen");
}
public function sellerNIN()
{
return view("auth.nin-registration");
}
public function login()
{
$this->getLocationAndDevice();
if (Session::get('token')) {
return redirect('/services');
}
return view('auth.login');
}
public function register()
{
if (Session::get('token')) {
return redirect()->intended('/services');
}
$this->getLocationAndDevice();
return view('auth.register');
}
public function verifyAccountEmail()
{
if (Session::get('token')) {
return redirect()->intended('/services');
}
return view('auth.verify-account-email');
}
public function becomeSeller()
{
return view('auth.become-a-seller');
}
public function forgetPassword()
{
if (Session::get('token')) {
return redirect()->intended('/services');
}
return view('auth.forget-password');
}
public function setNewPassword()
{
if (Session::get('token')) {
return redirect()->intended('/services');
}
return view('auth.set-new-password');
}
public function verifyEmail()
{
if (Session::get('token')) {
return redirect()->intended('/services');
}
return view('auth.verify-email');
}
public function verifyPhone()
{
if (Session::get('token')) {
return redirect()->intended('/services');
}
return view('auth.verify-phone');
}
public function logout()
{
Session::flush();
return redirect()->to('/login')->with('error', 'Logout');
}
}