This commit is contained in:
armiejean 2025-05-10 00:53:15 +08:00
parent 9b5ee500d0
commit 6c47477da1
6 changed files with 101 additions and 104 deletions

View File

@ -5,7 +5,7 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller class AuthController extends Controller
{ {
protected $apiBaseUrl = 'http://192.168.100.6:8081/api'; protected $apiBaseUrl = 'http://192.168.100.6:8081/api';
@ -22,43 +22,34 @@ class AuthController extends Controller
* Handle login form submission by calling the API * Handle login form submission by calling the API
*/ */
public function login(Request $request) public function login(Request $request)
{
$credentials = $request->only('username', 'password');
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
$user = Auth::user();
// 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()
{ {
$url = "{$this->apiBaseUrl}/cms/login_password"; // Fetch the authenticated user
$csrfToken = $request->session()->token(); $user = Auth::user();
try { // If no user is authenticated, redirect to login
$response = Http::withHeaders([ if (!$user) {
'X-CSRF-TOKEN' => $csrfToken, return redirect()->route('login')->with('error', 'Please log in to view your profile.');
'Accept' => 'application/json',
])->post($url, [
'username' => $request->input('username'),
'password' => $request->input('password'),
]);
$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.']); // Pass the user to the view
} catch (\Exception $e) { return view('pages.my-profile', compact('user'));
Log::error('Login Exception: ' . $e->getMessage());
return redirect()->back()->withErrors(['login' => 'Login request failed: ' . $e->getMessage()]);
}
} }
/** /**
* Show the change password form * Show the change password form
*/ */
@ -133,4 +124,6 @@ class AuthController extends Controller
return redirect()->route('login')->with('success', 'Logged out successfully'); return redirect()->route('login')->with('success', 'Logged out successfully');
} }
} }
} }

View File

@ -1,65 +1,64 @@
version: '3.8'
services: services:
app: app:
build: build:
context: . context: ./docker/php
dockerfile: ./docker/php/Dockerfile dockerfile: Dockerfile
container_name: unioil-app container_name: frontend-app
restart: unless-stopped restart: always
working_dir: /var/www
volumes: volumes:
- .:/var/www/html - .:/var/www
- ./storage:/var/www/html/storage
- ./bootstrap/cache:/var/www/html/bootstrap/cache command: >
depends_on: /bin/sh -c 'mkdir -p /var/www/storage /var/www/bootstrap/cache &&
db_mysql: chown -R www-data:www-data /var/www/storage /var/www/bootstrap/cache &&
condition: service_healthy chmod -R 775 /var/www/storage /var/www/bootstrap/cache &&
command: [ "sh", "-c", "/var/www/html/docker/php/entrypoint.sh" ] composer install --no-dev --optimize-autoloader &&
php-fpm'
healthcheck: healthcheck:
test: [ "CMD", "pgrep", "php-fpm" ] test: [ "CMD", "sh", "-c", "netstat -an | grep 9000 > /dev/null || exit 1" ]
interval: 30s interval: 30s
timeout: 10s timeout: 10s
retries: 10 retries: 10
networks: networks:
- app_network - frontend-network
- unioil-mobile-api_backend-network
db_mysql:
image: mysql:8.2
container_name: unioil-db
restart: unless-stopped
environment: environment:
MYSQL_ROOT_PASSWORD: newpassword - DB_HOST=db_mysql
MYSQL_DATABASE: unioil-database - DB_PORT=3306
MYSQL_USER: rootuser - DB_DATABASE=unioil-database
MYSQL_PASSWORD: password - DB_USERNAME=rootuser
volumes: - DB_PASSWORD=password
- mysql-data:/var/lib/mysql - CACHE_DRIVER=file
healthcheck: - API_URL=http://backend-web:8081
test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ]
interval: 30s
timeout: 10s
retries: 5
networks:
- app_network
nginx: web-frontend:
image: nginx:alpine image: nginx:1.26.3-alpine
container_name: unioil-nginx container_name: web-frontend
restart: unless-stopped restart: always
ports: ports:
- "8000:80" - "8000:80"
expose:
- "80"
volumes: volumes:
- .:/var/www/html - .:/var/www
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
depends_on: depends_on:
app: app:
condition: service_healthy condition: service_healthy
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost" ]
interval: 30s
timeout: 10s
retries: 5
networks: networks:
- app_network - frontend-network
- unioil-mobile-api_backend-network
volumes:
mysql-data:
storage-volume:
driver: local
networks: networks:
app_network: frontend-network:
driver: bridge driver: bridge
unioil-mobile-api_backend-network:
external: true

View File

@ -1,8 +1,7 @@
server { server {
listen 80; listen 80;
server_name localhost; server_name localhost;
root /var/www/public;
root /var/www/html/public;
index index.php index.html; index index.php index.html;
location / { location / {
@ -10,10 +9,10 @@ server {
} }
location ~ \.php$ { location ~ \.php$ {
include fastcgi_params; fastcgi_pass frontend-app:9000;
fastcgi_pass app:9000; # laravel app container
fastcgi_index index.php; fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} }
location ~ /\.ht { location ~ /\.ht {

View File

@ -1,29 +1,33 @@
# Base image FROM php:8.3-fpm
FROM php:8.3-fpm-alpine
# Install required PHP extensions # Install system dependencies
RUN docker-php-ext-install pdo pdo_mysql bcmath 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 # Install Composer
COPY --from=composer:2.7 /usr/bin/composer /usr/bin/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 # Set working directory
WORKDIR /var/www/html WORKDIR /var/www
# Copy app files # Copy application code
COPY . /var/www/html COPY . /var/www
# Ensure entrypoint script is executable # Set permissions
RUN chmod +x /var/www/html/docker/php/entrypoint.sh RUN chown -R www-data:www-data /var/www \
&& chmod -R 755 /var/www
# Set permissions for app files # Expose port
RUN chown -R www-data:www-data /var/www/html
# Expose PHP-FPM port
EXPOSE 9000 EXPOSE 9000
# Start PHP-FPM (handled in entrypoint.sh) CMD ["php-fpm"]
CMD ["sh", "-c", "/var/www/html/docker/php/entrypoint.sh"]

View File

@ -25,6 +25,10 @@
</div> </div>
@endif @endif
@if (session('error_username'))
<span style="color: red;">{{ session('error_username') }}</span>
@endif
<form method="POST" action="{{ route('login') }}"> <form method="POST" action="{{ route('login') }}">
@csrf @csrf
<div class="mb-3"> <div class="mb-3">

View File

@ -24,9 +24,7 @@ Route::get('/change-password', [AuthController::class, 'showChangePasswordForm']
Route::post('/change-password', [AuthController::class, 'changePassword'])->name('password.change'); Route::post('/change-password', [AuthController::class, 'changePassword'])->name('password.change');
// Redirect to my-profile (adjust as needed) // Redirect to my-profile (adjust as needed)
Route::get('/my-profile', function () { Route::get('/my-profile', [AuthController::class, 'showMyProfile'])->name('my-profile')->middleware('auth');
return view('pages.my-profile'); // Replace with your actual profile view or controller
})->name('my-profile');
// Handle logout // Handle logout
Route::post('/logout', [AuthController::class, 'logout'])->name('logout'); Route::post('/logout', [AuthController::class, 'logout'])->name('logout');