Login logout handled : GWM
This commit is contained in:
parent
8cb3a736c1
commit
e7461a54c0
@ -3,12 +3,14 @@
|
||||
namespace App\Controllers;
|
||||
use CodeIgniter\RESTful\ResourceController;
|
||||
use App\Models\StaffModel;
|
||||
use App\Models\StaffAttendanceModel;
|
||||
|
||||
class StaffAuthController extends ResourceController
|
||||
{
|
||||
protected $format = 'json';
|
||||
protected $myLogger;
|
||||
protected $StaffModel;
|
||||
protected $StaffAttendanceModel;
|
||||
|
||||
|
||||
public function __construct()
|
||||
@ -17,6 +19,7 @@ class StaffAuthController extends ResourceController
|
||||
helper('oauth_helper');
|
||||
$this->myLogger = \Config\Services::mylogger();
|
||||
$this->StaffModel = new StaffModel();
|
||||
$this->StaffAttendanceModel = new StaffAttendanceModel();
|
||||
|
||||
}
|
||||
|
||||
@ -124,6 +127,24 @@ class StaffAuthController extends ResourceController
|
||||
if ($StaffData && isset($this->request->getJSON()->otp) ) {
|
||||
|
||||
$this->StaffModel->where('id', $StaffData['id'])->where('email_otp', $otp)->where('is_active', 1)->set(['email_otp'=>null])->update();
|
||||
|
||||
|
||||
// Attendance check or insert
|
||||
$today = date('Y-m-d');
|
||||
|
||||
$existingAttendance = $this->StaffAttendanceModel->where('staff_id', $StaffData['id'])->where('DATE(login_datetime)', $today)->first();
|
||||
|
||||
if (!$existingAttendance) {
|
||||
// New login for today
|
||||
$this->StaffAttendanceModel->insert(['staff_id' => $StaffData['id'],'login_datetime' => date('Y-m-d H:i:s')]);
|
||||
$lastLoginTime = date('Y-m-d H:i:s');
|
||||
} else {
|
||||
// Already logged in today, reuse last login time
|
||||
$lastLoginTime = $existingAttendance['login_datetime'];
|
||||
}
|
||||
|
||||
// Add last login info to JWT
|
||||
$StaffData['last_login_datetime'] = $lastLoginTime;
|
||||
|
||||
$result = generateJWT($StaffData);
|
||||
|
||||
@ -139,6 +160,10 @@ class StaffAuthController extends ResourceController
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -25,6 +25,38 @@ class JwtAuthFilter implements FilterInterface
|
||||
return service('response')->setJSON(['status' => 401, 'message' => 'Invalid or expired token'])->setStatusCode(401);
|
||||
}
|
||||
|
||||
// ⚙️ Extract date info from token
|
||||
$userId = $decodedToken->id ?? null;
|
||||
$lastLogin = $decodedToken->last_login_datetime ?? null;
|
||||
|
||||
if ($userId && $lastLogin) {
|
||||
|
||||
$lastLoginDate = date('Y-m-d', strtotime($lastLogin));
|
||||
$todayDate = date('Y-m-d');
|
||||
|
||||
if ($lastLoginDate !== $todayDate) {
|
||||
|
||||
$attendanceModel = new \App\Models\StaffAttendanceModel();
|
||||
|
||||
// Find today's (previous) attendance record (yesterday)
|
||||
$attendance = $attendanceModel
|
||||
->where('staff_id', $userId)
|
||||
->where('DATE(login_datetime)', $lastLoginDate)
|
||||
->first();
|
||||
|
||||
if ($attendance && !$attendance['logout_datetime']) {
|
||||
$attendanceModel->update($attendance['id'], [
|
||||
'logout_datetime' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
}
|
||||
|
||||
return service('response')->setJSON([
|
||||
'status' => 403,
|
||||
'message' => 'Session expired, please login again'
|
||||
])->setStatusCode(403);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ if (!function_exists('generateJWT')) {
|
||||
{
|
||||
$key = getenv('JWT_SECRET'); // Load secret from .env
|
||||
$issuedAt = time();
|
||||
$expire = $issuedAt + 14400; // Token valid for 4 hour
|
||||
$expire = time() + (60 * 60 * 12); // Token valid for 24 hour
|
||||
|
||||
$payload = [
|
||||
'iss' => base_url(), // Issuer
|
||||
|
||||
11
app/Models/StaffAttendanceModel.php
Normal file
11
app/Models/StaffAttendanceModel.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php namespace App\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class StaffAttendanceModel extends Model
|
||||
{
|
||||
protected $table = 'staff_attendance';
|
||||
protected $primaryKey = 'id';
|
||||
protected $returnType = 'array';
|
||||
protected $allowedFields = ['staff_id', 'login_datetime', 'logout_datetime'];
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user