login
This commit is contained in:
parent
52bcd422e2
commit
8bcecdc10b
|
@ -3,104 +3,86 @@ 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\Session;
|
||||||
|
|
||||||
class AuthController extends Controller
|
class AuthController extends Controller
|
||||||
{
|
{
|
||||||
|
public function showLoginForm()
|
||||||
|
{
|
||||||
|
return view('login');
|
||||||
|
}
|
||||||
|
|
||||||
public function login(Request $request)
|
public function login(Request $request)
|
||||||
{
|
{
|
||||||
$request->validate([
|
$apiUrl = env('BACKEND_API_URL', 'http://localhost:8080');
|
||||||
'username' => 'required|string',
|
$loginEndpoint = '/api/cms/login_password';
|
||||||
'password' => 'required|string',
|
|
||||||
|
Log::info('API URL being used: ' . $apiUrl . $loginEndpoint);
|
||||||
|
|
||||||
|
$response = Http::timeout(30)->post($apiUrl . $loginEndpoint, [
|
||||||
|
'username' => $request->input('username'),
|
||||||
|
'password' => $request->input('password'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$username = $request->input('username');
|
if ($response->successful()) {
|
||||||
$password = $request->input('password');
|
$data = $response->json();
|
||||||
|
if (isset($data['success']) && isset($data['message'])) {
|
||||||
// Step 1: Verify the username
|
if ($data['message'] === 'User must change password') {
|
||||||
$usernameApiUrl = 'http://192.168.56.1:8080/api/cms/username_login';
|
Session::put('admin_uuid', $data['success']['admin_uuid']);
|
||||||
|
Session::put('username', $request->input('username'));
|
||||||
try {
|
return redirect()->route('password.change')->with('success', 'You must change your password.');
|
||||||
$usernameResponse = Http::timeout(30)->post($usernameApiUrl, [
|
|
||||||
'username' => $username,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$usernameData = $usernameResponse->json();
|
|
||||||
|
|
||||||
// Check if username verification failed
|
|
||||||
if (!$usernameResponse->successful() || !isset($usernameData['data']['is_verified']) || !$usernameData['data']['is_verified']) {
|
|
||||||
return redirect()->back()->with('error', $usernameData['message'] ?? 'Username does not exist.');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 2: Authenticate with password
|
|
||||||
$loginApiUrl = 'http://192.168.56.1:8080/api/cms/login_password';
|
|
||||||
|
|
||||||
$loginResponse = Http::timeout(30)->post($loginApiUrl, [
|
|
||||||
'username' => $username,
|
|
||||||
'password' => $password,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$loginData = $loginResponse->json();
|
|
||||||
|
|
||||||
if (!$loginResponse->successful()) {
|
|
||||||
return redirect()->back()->with('error', $loginData['message'] ?? 'Login failed.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$loginData['success']) {
|
|
||||||
$message = $loginData['message'] ?? 'Login failed';
|
|
||||||
if ($message === 'User must change password') {
|
|
||||||
// Store admin_uuid in session for password change
|
|
||||||
session(['admin_uuid' => $loginData['data']['admin_uuid']]);
|
|
||||||
return redirect()->route('change-password')->with('error', 'You must change your password.');
|
|
||||||
}
|
}
|
||||||
return redirect()->back()->with('error', $message);
|
// Store token in session
|
||||||
|
Session::put('access_token', $data['success']['token']);
|
||||||
|
Session::put('username', $request->input('username'));
|
||||||
|
return redirect()->route('dashboard')->with('success', 'Login successful.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($loginData['data']['token'])) {
|
|
||||||
session(['authToken' => $loginData['data']['token']]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return redirect()->route('my-profile')->with('success', 'Login successful!');
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
return redirect()->back()->with('error', 'Error connecting to API: ' . $e->getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$errorMessage = 'Login failed. Please check your credentials.';
|
||||||
|
if ($response->status() === 401) {
|
||||||
|
$errorMessage = $response->json()['message'] ?? 'Unauthorized access.';
|
||||||
|
} elseif ($response->status() === 422) {
|
||||||
|
$errorMessage = $response->json()['message'] ?? 'Validation error.';
|
||||||
|
}
|
||||||
|
|
||||||
|
return redirect()->back()->with('error', $errorMessage)->withInput($request->except('password'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function showChangePasswordForm()
|
||||||
|
{
|
||||||
|
return view('change-password');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a method to handle password change (optional, if you want to implement the change-password route)
|
|
||||||
public function changePassword(Request $request)
|
public function changePassword(Request $request)
|
||||||
{
|
{
|
||||||
$request->validate([
|
$apiUrl = env('BACKEND_API_URL', 'http://localhost:8080');
|
||||||
'password' => 'required|string|min:8|confirmed',
|
$changePasswordEndpoint = '/api/cms/login_changePassword';
|
||||||
|
|
||||||
|
Log::info('API URL being used: ' . $apiUrl . $changePasswordEndpoint);
|
||||||
|
|
||||||
|
$response = Http::timeout(30)->post($apiUrl . $changePasswordEndpoint, [
|
||||||
|
'admin_uuid' => Session::get('admin_uuid'),
|
||||||
|
'username' => Session::get('username'),
|
||||||
|
'password' => $request->input('new_password'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$adminUuid = session('admin_uuid');
|
if ($response->successful()) {
|
||||||
if (!$adminUuid) {
|
|
||||||
return redirect()->route('login')->with('error', 'Invalid session. Please login again.');
|
|
||||||
}
|
|
||||||
|
|
||||||
$changePasswordApiUrl = 'http://192.168.56.1:8080/api/cms/change_password';
|
|
||||||
|
|
||||||
try {
|
|
||||||
$response = Http::timeout(30)->post($changePasswordApiUrl, [
|
|
||||||
'admin_uuid' => $adminUuid,
|
|
||||||
'password' => $request->input('password'),
|
|
||||||
]);
|
|
||||||
|
|
||||||
$data = $response->json();
|
$data = $response->json();
|
||||||
|
if (isset($data['success']['token'])) {
|
||||||
if (!$response->successful() || !$data['success']) {
|
Session::put('access_token', $data['success']['token']);
|
||||||
return redirect()->back()->with('error', $data['message'] ?? 'Failed to change password.');
|
return redirect()->route('dashboard')->with('success', 'Password changed successfully.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($data['data']['token'])) {
|
|
||||||
session(['authToken' => $data['data']['token']]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear admin_uuid from session
|
|
||||||
session()->forget('admin_uuid');
|
|
||||||
|
|
||||||
return redirect()->route('my-profile')->with('success', 'Password changed successfully!');
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
return redirect()->back()->with('error', 'Error connecting to API: ' . $e->getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$errorMessage = 'Failed to change password.';
|
||||||
|
if ($response->status() === 401) {
|
||||||
|
$errorMessage = $response->json()['message'] ?? 'Unauthorized access.';
|
||||||
|
} elseif ($response->status() === 422) {
|
||||||
|
$errorMessage = $response->json()['message'] ?? 'Password cannot be the same as the previous two passwords.';
|
||||||
|
}
|
||||||
|
|
||||||
|
return redirect()->back()->with('error', $errorMessage)->withInput();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -4,7 +4,7 @@
|
||||||
<div class="container py-5">
|
<div class="container py-5">
|
||||||
<div class="row justify-content-center">
|
<div class="row justify-content-center">
|
||||||
<div class="col-12 col-md-6">
|
<div class="col-12 col-md-6">
|
||||||
<img src="{{ asset('img/logo.png') }}" alt="Unioil Logo" class="img-fluid" style="max-width: 150px;">
|
<img src="{{ asset('img/logo.png') }}" alt="Unioil Logo" class="img-fluid d-block mx-auto" style="max-width: 150px;">
|
||||||
|
|
||||||
<div class="mb-3 text-center">
|
<div class="mb-3 text-center">
|
||||||
<h4 class="mb-1 fw-bold">Change Password</h4>
|
<h4 class="mb-1 fw-bold">Change Password</h4>
|
||||||
|
@ -32,20 +32,20 @@
|
||||||
</div>
|
</div>
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
<form method="POST" action="{{ route('change-password.submit') }}">
|
<form method="POST" action="{{ route('password.submit') }}">
|
||||||
@csrf
|
@csrf
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label class="form-label fw-semibold" style="font-size: 13px; color: #003366;">New Password</label>
|
<label class="form-label fw-semibold" style="font-size: 13px; color: #003366;">Username</label>
|
||||||
<input type="password" class="form-control @error('password') is-invalid @enderror" id="password" name="password" placeholder="New Password" required>
|
<input type="text" class="form-control @error('username') is-invalid @enderror" id="username" name="username" placeholder="Username" value="{{ old('username', session('username')) }}" required readonly>
|
||||||
@error('password')
|
@error('username')
|
||||||
<div class="invalid-feedback">{{ $message }}</div>
|
<div class="invalid-feedback">{{ $message }}</div>
|
||||||
@enderror
|
@enderror
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label class="form-label fw-semibold" style="font-size: 13px; color: #003366;">Confirm Password</label>
|
<label class="form-label fw-semibold" style="font-size: 13px; color: #003366;">New Password</label>
|
||||||
<input type="password" class="form-control @error('password_confirmation') is-invalid @enderror" id="password_confirmation" name="password_confirmation" placeholder="Confirm Password" required>
|
<input type="password" class="form-control @error('new_password') is-invalid @enderror" id="new_password" name="new_password" placeholder="New Password" required>
|
||||||
@error('password_confirmation')
|
@error('new_password')
|
||||||
<div class="invalid-feedback">{{ $message }}</div>
|
<div class="invalid-feedback">{{ $message }}</div>
|
||||||
@enderror
|
@enderror
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
<div class="container py-5">
|
<div class="container py-5">
|
||||||
<div class="row justify-content-center">
|
<div class="row justify-content-center">
|
||||||
<div class="col-12 col-md-6">
|
<div class="col-12 col-md-6">
|
||||||
<img src="{{ asset('img/logo.png') }}" alt="Unioil Logo" class="img-fluid" style="max-width: 150px;">
|
<img src="{{ asset('img/logo.png') }}" alt="Unioil Logo" class="img-fluid d-block mx-auto" style="max-width: 150px;">
|
||||||
|
|
||||||
<div class="mb-3 text-center">
|
<div class="mb-3 text-center">
|
||||||
<h4 class="mb-1 fw-bold">Welcome</h4>
|
<h4 class="mb-1 fw-bold">Welcome</h4>
|
||||||
|
@ -32,7 +32,7 @@
|
||||||
</div>
|
</div>
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
<form method="POST" action="{{ route('login') }}">
|
<form method="POST" action="{{ route('login.submit') }}">
|
||||||
@csrf
|
@csrf
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label class="form-label fw-semibold" style="font-size: 13px; color: #003366;">Enter Username</label>
|
<label class="form-label fw-semibold" style="font-size: 13px; color: #003366;">Enter Username</label>
|
||||||
|
@ -43,7 +43,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label class="form-label fw-semibold text-primary" style="font-size: 13px; color: #003366 !important;">Enter Password</label>
|
<label class="form-label fw-semibold" style="font-size: 13px; color: #003366;">Enter Password</label>
|
||||||
<input type="password" class="form-control @error('password') is-invalid @enderror" id="password" name="password" placeholder="Password" required>
|
<input type="password" class="form-control @error('password') is-invalid @enderror" id="password" name="password" placeholder="Password" required>
|
||||||
@error('password')
|
@error('password')
|
||||||
<div class="invalid-feedback">{{ $message }}</div>
|
<div class="invalid-feedback">{{ $message }}</div>
|
||||||
|
|
|
@ -5,12 +5,13 @@ use Illuminate\Support\Facades\Http;
|
||||||
use App\Http\Controllers\AuthController;
|
use App\Http\Controllers\AuthController;
|
||||||
|
|
||||||
|
|
||||||
Route::get('/', function () {
|
|
||||||
return view('login');
|
|
||||||
})->name('login');
|
|
||||||
|
|
||||||
Route::post('/login', [AuthController::class, 'login'])->name('login');
|
|
||||||
Route::post('/change-password', [AuthController::class, 'changePassword'])->name('change-password.submit');
|
Route::get('/', [AuthController::class, 'showLoginForm'])->name('login');
|
||||||
|
Route::post('/login', [AuthController::class, 'login'])->name('login.submit');
|
||||||
|
Route::get('/change-password', [AuthController::class, 'showChangePasswordForm'])->name('password.change');
|
||||||
|
Route::post('/change-password', [AuthController::class, 'changePassword'])->name('password.submit');
|
||||||
|
|
||||||
|
|
||||||
Route::get('/dashboard', function () {
|
Route::get('/dashboard', function () {
|
||||||
return view('dashboard');
|
return view('dashboard');
|
||||||
|
|
Loading…
Reference in New Issue