post('/auth/login', function () { // Get the post data global $response; $data = json_decode(file_get_contents('php://input'), true); // Check if the customer number, and password are set if (!isset($data['customer_number']) || empty($data['customer_number']) || !is_numeric($data['customer_number']) || $data['customer_number'] < 1) { $response->error('Customer number is required', 400); } if (!isset($data['password']) || empty($data['password']) || strlen($data['password']) < 1) { $response->error('Password is required', 400); } // Try to log the user in $isCredentialsValid = (new authentication())->authenticate($data['customer_number'], $data['password']); // Log the incident if ($isCredentialsValid) { (new logs_o())->add('auth', 'global', 1, 0, 'AUTH_SUCCESS', 'Customer number: ' . $data['customer_number']); } else { (new logs_o())->add('auth', 'global', 1, 0, 'AUTH_FAILURE', 'Customer number: ' . $data['customer_number']); $response->error('Invalid credentials', 401); } // If the credentials are valid, create a token $token = (new authentication())->create_token($data['customer_number']); // Return the token $response->success(['token' => $token]); }); $this->get('/auth/logout', function () { // Get the token from the headers global $response; $token = $_SERVER['HTTP_AUTHORIZATION'] ?? ''; // Remove the Bearer prefix $token = str_replace('Bearer ', '', $token); // Check if the token is valid if (!(new authentication())->validate_token($token)) { $response->error('Invalid token', 401); } // Delete the token (new tokens_o())->delete($token); // Return a success message $response->success(['message' => 'Logged out']); }); $this->get('/auth/session', function () { // Get the token from the headers global $response; $token = $_SERVER['HTTP_AUTHORIZATION'] ?? ''; // Default to empty string if not set // Remove the Bearer prefix $token = str_replace('Bearer ', '', $token); // Check if the token is valid if (!(new authentication())->validate_token($token)) { $response->error('Invalid token', 401); } // Get the user object $user = (new authentication())->get_user(); // Check if the user exists if (!$user) { $response->error('User not found', 400); } // Return the (session) user object $response->success( ($user->includeIncludes(['economicCustomer', 'permissions'])->asArray()) ); }); $this->post('/auth/employee/login', function () { // Get the post data global $response; $data = json_decode(file_get_contents('php://input'), true); // Check if the employee number, and password are set if (!isset($data['user_id'])) { $response->error('Employee number is required', 400); } if (!isset($data['password'])) { $response->error('Password is required', 400); } // Try to log the user in $isCredentialsValid = (new authentication())->authenticateEmployee($data['user_id'], $data['password']); // Log the incident if ($isCredentialsValid) { (new logs_o())->add('auth', 'global', 1, 0, 'AUTH_SUCCESS', 'Employee number: ' . $data['user_id']); } else { (new logs_o())->add('auth', 'global', 1, 0, 'AUTH_FAILURE', 'Employee number: ' . $data['user_id']); $response->error('Invalid credentials', 401); } // If the credentials are valid, create a token $token = (new authentication())->create_employee_token($data['user_id']); // Return the token $response->success(['token' => $token]); }); } }