cms-frontend/resources/views/login.blade.php

118 lines
5.1 KiB
PHP

```php
@extends('layouts.login')
@section('content')
<div class="container py-5">
<div class="row justify-content-center">
<div class="col-12 col-md-6">
<img src="{{ asset('img/logo.png') }}" alt="Unioil Logo" class="img-fluid" style="max-width: 150px;">
<div class="mb-3 text-center">
<h4 class="mb-1 fw-bold">Welcome</h4>
<span style="font-size: 14px;" class="text-muted">Sign in to continue</span>
</div>
<!-- Error/Success Messages -->
<div id="alert" class="alert d-none" role="alert"></div>
<form id="loginForm">
<div class="mb-3">
<label class="form-label fw-semibold" style="font-size: 13px; color: #003366;">Enter Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Username" required>
</div>
<div class="mb-3">
<label class="form-label fw-semibold text-primary" style="font-size: 13px; color: #003366 !important;">Enter Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Password" required>
</div>
<div class="row mt-4">
<div class="col-6">
<a href="#" class="text-decoration-none text-primary" data-bs-toggle="modal" data-bs-target="#forgotPasswordModal">Forgot Username</a>
</div>
<div class="col-6 text-end">
<button type="submit" class="btn btn-primary w-100" style="background-color: #E74610; border-color: #E74610;">
Login
</button>
</div>
</div>
</form>
<!-- Forgot Password Modal -->
<div class="modal fade" id="forgotPasswordModal" tabindex="-1" aria-labelledby="forgotPasswordModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" style="max-width: 337px;">
<div class="modal-content">
<div class="modal-body">
<h4>Forgot Password</h4>
<p>
To have your password reset, please contact <br>
Unioil's admin at <a href="mailto:admin@unioil.com" class="text-primary">admin@unioil.com</a>
</p>
</div>
<div class="modal-footer justify-content-end">
<button class="btn btn-primary" data-bs-dismiss="modal" style="background-color: #E74610; border-color: #E74610; width: 64px;">Ok</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Axios CDN -->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
document.getElementById('loginForm').addEventListener('submit', async function (e) {
e.preventDefault();
const username = document.getElementById('username').value.trim();
const password = document.getElementById('password').value.trim();
const alertBox = document.getElementById('alert');
// Use environment variable for the API base URL
const apiBaseUrl = '{{ env('API_URL', 'http://web:80') }}/api';
// Reset alert box
alertBox.classList.add('d-none');
alertBox.classList.remove('alert-success', 'alert-danger');
alertBox.textContent = '';
try {
// Validate username
const usernameResponse = await axios.post(`${apiBaseUrl}/cms/login_username`, { username });
if (!usernameResponse.data.success) {
throw new Error(usernameResponse.data.message || 'Invalid username');
}
// Validate password
const passwordResponse = await axios.post(`${apiBaseUrl}/cms/login_password`, { username, password });
if (!passwordResponse.data.success) {
throw new Error(passwordResponse.data.message || 'Invalid password');
}
// Success
alertBox.classList.remove('d-none', 'alert-danger');
alertBox.classList.add('alert-success');
alertBox.textContent = 'Login successful! Redirecting...';
// Store token
if (passwordResponse.data.token) {
localStorage.setItem('authToken', passwordResponse.data.token);
}
setTimeout(() => {
window.location.href = '{{ url("/my-profile") }}';
}, 1000);
} catch (error) {
// Log detailed error to console for debugging
console.error('Login error:', error, error.response);
alertBox.classList.remove('d-none', 'alert-success');
alertBox.classList.add('alert-danger');
alertBox.textContent = error.response?.data?.message || error.message || 'Login failed. Please try again.';
}
});
</script>
@endsection
```