diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index a56c49c..83e195b 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -4,61 +4,133 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; -use Illuminate\Support\Facades\Session; -use Illuminate\Support\Facades\Validator; -use Illuminate\Http\RedirectResponse; +use Illuminate\Support\Facades\Log; class AuthController extends Controller { + protected $apiBaseUrl = 'http://192.168.100.6:8081/api'; + + /** + * Show the login form + */ public function showLoginForm() { return view('login'); } - public function login(Request $request): RedirectResponse + /** + * Handle login form submission by calling the API + */ + public function login(Request $request) { - $validator = Validator::make($request->all(), [ - 'username' => 'required|string', - 'password' => 'required|string', - ]); - - if ($validator->fails()) { - return redirect()->back() - ->withErrors($validator) - ->withInput(); - } + $url = "{$this->apiBaseUrl}/cms/login_password"; + $csrfToken = $request->session()->token(); try { - $response = Http::timeout(30)->post(config('services.backend_api.url') . '/api/cms/login_password', [ - 'username' => $request->username, - 'password' => $request->password, + $response = Http::withHeaders([ + 'X-CSRF-TOKEN' => $csrfToken, + 'Accept' => 'application/json', + ])->post($url, [ + 'username' => $request->input('username'), + 'password' => $request->input('password'), ]); - $json = $response->json(); + $data = $response->json(); - if ($response->successful()) { - if ($json['code'] === 200) { - Session::put('user', $json['data']['user'] ?? null); - return redirect('my-profile'); + // Log the full response for debugging + Log::info('Login API Response: ', [$data]); + + if ($response->successful() && isset($data['code']) && $data['code'] === 200) { + if (isset($data['data']) && isset($data['data']['access_token'])) { + session(['token' => $data['data']['access_token']]); + return redirect()->intended(route('my-profile')); + } elseif (isset($data['data']['prompt_password'])) { + session(['admin_uuid' => $data['data']['admin_uuid']]); + return redirect()->route('password.change.form'); } else { - return redirect()->back() - ->withErrors(['username' => $json['message'] ?? 'Login failed.']) - ->withInput(); + return redirect()->back()->withErrors(['login' => 'Invalid API response or no token received.']); } - } else { - $message = $json['message'] ?? 'Login request failed. Please try again.'; - return redirect()->back() - ->withErrors(['username' => $message]) - ->withInput(); } - } catch (\Illuminate\Http\Client\ConnectionException $e) { - return redirect()->back() - ->withErrors(['username' => 'Unable to connect to the server. Please try again later.']) - ->withInput(); + + return redirect()->back()->withErrors(['login' => $data['message'] ?? 'Login failed.']); } catch (\Exception $e) { - return redirect()->back() - ->withErrors(['username' => 'An error occurred: ' . $e->getMessage()]) - ->withInput(); + Log::error('Login Exception: ' . $e->getMessage()); + return redirect()->back()->withErrors(['login' => 'Login request failed: ' . $e->getMessage()]); + } + } + + /** + * Show the change password form + */ + public function showChangePasswordForm() + { + if (!session()->has('admin_uuid')) { + return redirect()->route('login')->withErrors(['error' => 'Unauthorized access']); + } + + return view('change-password'); + } + + /** + * Handle change password form submission by calling the API + */ + public function changePassword(Request $request) + { + $url = "{$this->apiBaseUrl}/cms/login_changePassword"; + $csrfToken = $request->session()->token(); + + try { + $response = Http::withHeaders([ + 'X-CSRF-TOKEN' => $csrfToken, + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . session('token'), + ])->post($url, [ + 'admin_uuid' => $request->input('admin_uuid'), + 'password' => $request->input('password'), + ]); + + $data = $response->json(); + + Log::info('Change Password API Response: ', [$data]); + + if ($response->successful() && isset($data['code']) && $data['code'] === 200) { + if (isset($data['data']['access_token'])) { + session(['token' => $data['data']['access_token']]); + } elseif (isset($data['data']['token'])) { + session(['token' => $data['data']['token']]); + } + session()->forget('admin_uuid'); + return redirect()->route('my-profile')->with('success', $data['message'] ?? 'Password changed successfully'); + } + + return redirect()->back()->withErrors(['error' => $data['message'] ?? 'Failed to change password']); + } catch (\Exception $e) { + Log::error('Change Password Exception: ' . $e->getMessage()); + return redirect()->back()->withErrors(['error' => 'Password change request failed: ' . $e->getMessage()]); + } + } + + /** + * Handle logout by calling the API + */ + public function logout(Request $request) + { + $url = "{$this->apiBaseUrl}/logout_cms"; + $csrfToken = $request->session()->token(); + + try { + Http::withHeaders([ + 'X-CSRF-TOKEN' => $csrfToken, + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . session('token'), + ])->post($url); + + session()->flush(); + return redirect()->route('login')->with('success', 'Logged out successfully'); + } catch (\Exception $e) { + Log::error('Logout Exception: ' . $e->getMessage()); + session()->flush(); + return redirect()->route('login')->with('success', 'Logged out successfully'); } } } \ No newline at end of file diff --git a/app/Http/Controllers/ChangePasswordController.php b/app/Http/Controllers/ChangePasswordController.php new file mode 100644 index 0000000..ce1a0dd --- /dev/null +++ b/app/Http/Controllers/ChangePasswordController.php @@ -0,0 +1,86 @@ +apiBaseUrl = env('API_BASE_URL', 'http://your-backend-api-url'); + } + + public function showChangePasswordForm() + { + return view('change-password'); + } + + public function updatePassword(Request $request) + { + $request->validate([ + 'password' => 'required|min:8|confirmed', + ]); + + $adminId = Session::get('admin_id'); + $apiToken = Session::get('api_token'); + + if (!$adminId || !$apiToken) { + return redirect()->route('login')->with('error', 'You must be logged in to change your password'); + } + + $response = Http::withToken($apiToken) + ->get("{$this->apiBaseUrl}/api/admin/{$adminId}"); + + if ($response->failed()) { + return redirect()->back()->with('error', 'Unable to fetch admin data. Please try again later.'); + } + + $admin = $response->json(); + + if (!$admin) { + return redirect()->route('login')->with('error', 'Admin not found'); + } + + $updateResponse = Http::withToken($apiToken) + ->put("{$this->apiBaseUrl}/api/admin/{$adminId}", [ + 'password' => bcrypt($request->password), + 'is_passwordChanged' => 1, + ]); + + if ($updateResponse->failed()) { + return redirect()->back()->with('error', 'Failed to update password. Please try again.'); + } + + return redirect()->route('my-profile')->with('success', 'Password updated successfully'); + } + + // Method to fetch and display admin profile data + public function showProfile() + { + $adminId = Session::get('admin_id'); + $apiToken = Session::get('api_token'); + + if (!$adminId || !$apiToken) { + return redirect()->route('login')->with('error', 'You must be logged in to view your profile'); + } + + $response = Http::withToken($apiToken) + ->get("{$this->apiBaseUrl}/api/admin/{$adminId}"); + + if ($response->failed()) { + return redirect()->back()->with('error', 'Unable to fetch profile data. Please try again later.'); + } + + $admin = $response->json(); + + if (!$admin) { + return redirect()->route('login')->with('error', 'Admin not found'); + } + + return view('my-profile', ['admin' => $admin]); + } +} \ No newline at end of file diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 452e6b6..edaa977 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -3,6 +3,9 @@ namespace App\Providers; use Illuminate\Support\ServiceProvider; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\View; + class AppServiceProvider extends ServiceProvider { @@ -19,6 +22,10 @@ class AppServiceProvider extends ServiceProvider */ public function boot(): void { - // + // Share authenticated user with the 'layouts.app' view + View::composer('layouts.app', function ($view) { + $user = Auth::user(); + $view->with('user', $user); + }); } } diff --git a/config/services.php b/config/services.php index 38225ce..2da3f3e 100644 --- a/config/services.php +++ b/config/services.php @@ -35,9 +35,9 @@ return [ ], ], - 'backend_api' => [ - 'url' => env('BACKEND_API_URL', 'http://192.168.56.1:80'), - ], + 'backend_api' => [ + 'url' => 'http://192.168.100.6:8081', // Use the backend container name and internal port +], ]; diff --git a/docker-compose.yml b/docker-compose.yml index c70a26c..6939044 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,9 @@ services: - .:/var/www/html - ./storage:/var/www/html/storage - ./bootstrap/cache:/var/www/html/bootstrap/cache + depends_on: + db_mysql: + condition: service_healthy command: [ "sh", "-c", "/var/www/html/docker/php/entrypoint.sh" ] healthcheck: test: [ "CMD", "pgrep", "php-fpm" ] @@ -16,8 +19,26 @@ services: timeout: 10s retries: 10 networks: - - frontend_network - - unioil-mobile-api_app_network + - app_network + + db_mysql: + image: mysql:8.2 + container_name: unioil-db + restart: unless-stopped + environment: + MYSQL_ROOT_PASSWORD: newpassword + MYSQL_DATABASE: unioil-database + MYSQL_USER: rootuser + MYSQL_PASSWORD: password + volumes: + - mysql-data:/var/lib/mysql + healthcheck: + test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ] + interval: 30s + timeout: 10s + retries: 5 + networks: + - app_network nginx: image: nginx:alpine @@ -32,14 +53,13 @@ services: app: condition: service_healthy networks: - - frontend_network + - app_network volumes: + mysql-data: storage-volume: driver: local networks: - frontend_network: + app_network: driver: bridge - unioil-mobile-api_app_network: - external: true diff --git a/resources/views/change-password.blade.php b/resources/views/change-password.blade.php index 238a781..9766e50 100644 --- a/resources/views/change-password.blade.php +++ b/resources/views/change-password.blade.php @@ -8,57 +8,50 @@

Change Password

- Enter your new password + Enter a new password to continue
+ @if ($errors->any()) +
+ @foreach ($errors->all() as $error) + {{ $error }}
+ @endforeach +
+ @endif @if (session('success')) - + + + @endsection \ No newline at end of file diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 797ed18..2bb33fc 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -230,7 +230,7 @@ diff --git a/resources/views/login.blade.php b/resources/views/login.blade.php index 22a6132..d3ae044 100644 --- a/resources/views/login.blade.php +++ b/resources/views/login.blade.php @@ -12,23 +12,16 @@ - @if (session('success')) - - @endif - @if (session('error')) - - @endif @if ($errors->any()) -