From 6c47477da14f62a544b883a2f2197873c4210115 Mon Sep 17 00:00:00 2001 From: armiejean Date: Sat, 10 May 2025 00:53:15 +0800 Subject: [PATCH] login --- app/Http/Controllers/AuthController.php | 63 ++++++++---------- docker-compose.yml | 85 ++++++++++++------------- docker/nginx/default.conf | 9 ++- docker/php/Dockerfile | 40 ++++++------ resources/views/login.blade.php | 4 ++ routes/web.php | 4 +- 6 files changed, 101 insertions(+), 104 deletions(-) diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 83e195b..89b60dd 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -5,7 +5,7 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; - +use Illuminate\Support\Facades\Auth; class AuthController extends Controller { protected $apiBaseUrl = 'http://192.168.100.6:8081/api'; @@ -21,43 +21,34 @@ class AuthController extends Controller /** * Handle login form submission by calling the API */ - public function login(Request $request) - { - $url = "{$this->apiBaseUrl}/cms/login_password"; - $csrfToken = $request->session()->token(); + public function login(Request $request) +{ + $credentials = $request->only('username', 'password'); - try { - $response = Http::withHeaders([ - 'X-CSRF-TOKEN' => $csrfToken, - 'Accept' => 'application/json', - ])->post($url, [ - 'username' => $request->input('username'), - 'password' => $request->input('password'), - ]); + if (Auth::attempt($credentials)) { + $request->session()->regenerate(); + $user = Auth::user(); - $data = $response->json(); - - // 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(['login' => 'Invalid API response or no token received.']); - } - } - - return redirect()->back()->withErrors(['login' => $data['message'] ?? 'Login failed.']); - } catch (\Exception $e) { - Log::error('Login Exception: ' . $e->getMessage()); - return redirect()->back()->withErrors(['login' => 'Login request failed: ' . $e->getMessage()]); - } + // Always redirect to my-profile route, let the route's controller handle the user + return redirect()->route('my-profile'); } + return redirect()->back()->with('error', 'Invalid username or password'); +} + +public function showMyProfile() + { + // Fetch the authenticated user + $user = Auth::user(); + + // If no user is authenticated, redirect to login + if (!$user) { + return redirect()->route('login')->with('error', 'Please log in to view your profile.'); + } + + // Pass the user to the view + return view('pages.my-profile', compact('user')); + } + /** * Show the change password form @@ -133,4 +124,6 @@ class AuthController extends Controller return redirect()->route('login')->with('success', 'Logged out successfully'); } } + + } \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 6939044..2231486 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,65 +1,64 @@ +version: '3.8' + services: app: build: - context: . - dockerfile: ./docker/php/Dockerfile - container_name: unioil-app - restart: unless-stopped + context: ./docker/php + dockerfile: Dockerfile + container_name: frontend-app + restart: always + working_dir: /var/www volumes: - - .:/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" ] + - .:/var/www + + command: > + /bin/sh -c 'mkdir -p /var/www/storage /var/www/bootstrap/cache && + chown -R www-data:www-data /var/www/storage /var/www/bootstrap/cache && + chmod -R 775 /var/www/storage /var/www/bootstrap/cache && + composer install --no-dev --optimize-autoloader && + php-fpm' healthcheck: - test: [ "CMD", "pgrep", "php-fpm" ] + test: [ "CMD", "sh", "-c", "netstat -an | grep 9000 > /dev/null || exit 1" ] interval: 30s timeout: 10s retries: 10 networks: - - app_network - - db_mysql: - image: mysql:8.2 - container_name: unioil-db - restart: unless-stopped + - frontend-network + - unioil-mobile-api_backend-network 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 + - DB_HOST=db_mysql + - DB_PORT=3306 + - DB_DATABASE=unioil-database + - DB_USERNAME=rootuser + - DB_PASSWORD=password + - CACHE_DRIVER=file + - API_URL=http://backend-web:8081 - nginx: - image: nginx:alpine - container_name: unioil-nginx - restart: unless-stopped + web-frontend: + image: nginx:1.26.3-alpine + container_name: web-frontend + restart: always ports: - "8000:80" + expose: + - "80" volumes: - - .:/var/www/html + - .:/var/www - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro depends_on: app: condition: service_healthy + healthcheck: + test: [ "CMD", "curl", "-f", "http://localhost" ] + interval: 30s + timeout: 10s + retries: 5 networks: - - app_network - -volumes: - mysql-data: - storage-volume: - driver: local + - frontend-network + - unioil-mobile-api_backend-network networks: - app_network: + frontend-network: driver: bridge + unioil-mobile-api_backend-network: + external: true diff --git a/docker/nginx/default.conf b/docker/nginx/default.conf index b7a0c78..a898d62 100644 --- a/docker/nginx/default.conf +++ b/docker/nginx/default.conf @@ -1,8 +1,7 @@ server { listen 80; server_name localhost; - - root /var/www/html/public; + root /var/www/public; index index.php index.html; location / { @@ -10,10 +9,10 @@ server { } location ~ \.php$ { - include fastcgi_params; - fastcgi_pass app:9000; # laravel app container + fastcgi_pass frontend-app:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + include fastcgi_params; } location ~ /\.ht { @@ -22,4 +21,4 @@ server { error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; -} +} \ No newline at end of file diff --git a/docker/php/Dockerfile b/docker/php/Dockerfile index 71ad469..0ddb219 100644 --- a/docker/php/Dockerfile +++ b/docker/php/Dockerfile @@ -1,29 +1,33 @@ -# Base image -FROM php:8.3-fpm-alpine +FROM php:8.3-fpm -# Install required PHP extensions -RUN docker-php-ext-install pdo pdo_mysql bcmath +# Install system dependencies +RUN apt-get update && apt-get install -y \ + libpng-dev \ + libjpeg-dev \ + libfreetype6-dev \ + zip \ + unzip \ + git \ + curl \ + libzip-dev \ + net-tools \ + && docker-php-ext-configure gd --with-freetype --with-jpeg \ + && docker-php-ext-install gd pdo pdo_mysql zip # Install Composer COPY --from=composer:2.7 /usr/bin/composer /usr/bin/composer -# Install Node.js and npm -RUN apk add --no-cache nodejs npm - # Set working directory -WORKDIR /var/www/html +WORKDIR /var/www -# Copy app files -COPY . /var/www/html +# Copy application code +COPY . /var/www -# Ensure entrypoint script is executable -RUN chmod +x /var/www/html/docker/php/entrypoint.sh +# Set permissions +RUN chown -R www-data:www-data /var/www \ + && chmod -R 755 /var/www -# Set permissions for app files -RUN chown -R www-data:www-data /var/www/html - -# Expose PHP-FPM port +# Expose port EXPOSE 9000 -# Start PHP-FPM (handled in entrypoint.sh) -CMD ["sh", "-c", "/var/www/html/docker/php/entrypoint.sh"] +CMD ["php-fpm"] \ No newline at end of file diff --git a/resources/views/login.blade.php b/resources/views/login.blade.php index d3ae044..75fffed 100644 --- a/resources/views/login.blade.php +++ b/resources/views/login.blade.php @@ -25,6 +25,10 @@ @endif + @if (session('error_username')) + {{ session('error_username') }} + @endif +
@csrf
diff --git a/routes/web.php b/routes/web.php index f7f099c..01b0536 100644 --- a/routes/web.php +++ b/routes/web.php @@ -24,9 +24,7 @@ Route::get('/change-password', [AuthController::class, 'showChangePasswordForm'] Route::post('/change-password', [AuthController::class, 'changePassword'])->name('password.change'); // Redirect to my-profile (adjust as needed) -Route::get('/my-profile', function () { - return view('pages.my-profile'); // Replace with your actual profile view or controller -})->name('my-profile'); +Route::get('/my-profile', [AuthController::class, 'showMyProfile'])->name('my-profile')->middleware('auth'); // Handle logout Route::post('/logout', [AuthController::class, 'logout'])->name('logout');