27 lines
733 B
PHP
27 lines
733 B
PHP
<?php
|
|
|
|
namespace Api\Core;
|
|
|
|
class Controller {
|
|
protected function auth() {
|
|
$token = Auth::getBearerToken();
|
|
if (!$token) Response::error('No token provided', 401);
|
|
|
|
$payload = Auth::verifyToken($token);
|
|
if (!$payload) Response::error('Invalid or expired token', 401);
|
|
|
|
return $payload;
|
|
}
|
|
|
|
protected function validate($data, $rules) {
|
|
$errors = [];
|
|
foreach ($rules as $field => $rule) {
|
|
if ($rule === 'required' && (!isset($data[$field]) || empty($data[$field]))) {
|
|
$errors[] = "Field '{$field}' is required";
|
|
}
|
|
}
|
|
if (!empty($errors)) {
|
|
Response::json(['errors' => $errors], 422);
|
|
}
|
|
}
|
|
} |