PHP Code: filter use Modules\Login\Filters\Auth; use Modules\Login\Filters\Noauth;
public $aliases = [ "auth" => Auth::class, "noauth" => Noauth::class, ];
Auth public function before(RequestInterface $request, $arguments = null) { if (!session()->get('isAdminLoggedIn')) { return redirect()->to(base_url(route_to('admin'))); } }
Route $routes->get("dashboard", "DashboardController::index", ["filter" => "auth"]);
What is the issue in above code. Filter is not working in CodeIgniter 4. Not working filter auth in dashboard route.
-
paliz
Member
-
Posts: 236
Threads: 19
Joined: Oct 2020
Reputation:
1
07-06-2021, 09:58 AM
(This post was last modified: 07-06-2021, 10:07 AM by paliz.)
PHP Code: <?php
namespace Config;
use CodeIgniter\Config\BaseConfig; use CodeIgniter\Filters\CSRF; use CodeIgniter\Filters\DebugToolbar; use CodeIgniter\Filters\Honeypot; use Modules\Auth\Filters\AuthFilter; use Modules\Auth\Filters\CsrfFilter; use Modules\Auth\Filters\JwtFilter; use Modules\Auth\Filters\ThrottleFilter; use Modules\Common\Filters\ContentNegotiationFilter; use Modules\Common\Filters\CorsFilter; use Modules\Common\Filters\UrlFilter;
class Filters extends BaseConfig { /** * Configures aliases for Filter classes to * make reading things nicer and simpler. * * @var array */ public $aliases = [ //'csrf' => CSRF::class, 'toolbar' => DebugToolbar::class, 'honeypot' => Honeypot::class, 'toolbar' => DebugToolbar::class, 'honeypot' => Honeypot::class, 'csrf' => CsrfFilter::class, 'cors' => CorsFilter::class, 'auth' => AuthFilter::class, 'jwt' => JwtFilter::class, 'url' => UrlFilter::class, 'throttle' => ThrottleFilter::class, 'contentNegotiation' => ContentNegotiationFilter::class,
];
/** * List of filter aliases that are always * applied before and after every request. * * @var array */ public $globals = [ 'before' => [ // 'honeypot', // 'csrf', // 'honeypot', // 'csrf', 'cors', 'url', 'contentNegotiation' // 'csrf', ], 'after' => [ 'toolbar', // 'honeypot', // 'csrf', ], ];
/** * List of filter aliases that works on a * particular HTTP method (GET, POST, etc.). * * Example: * 'post' => ['csrf', 'throttle'] * * @var array */ public $methods = [
// 'get' => ['csrf'], // 'post' => ['csrf'], // 'put' => ['csrf'], // 'delete' => ['csrf']
];
/** * List of filter aliases that should run on any * before or after URI patterns. * * Example: * 'isLoggedIn' => ['before' => ['account/*', 'profiles/*']] * * @var array */ public $filters = [ 'auth' => ['before' => 'api*'], 'jwt' => ['before' => 'api*',],
]; }
PHP Code: <?php namespace Modules\Common\Filters;
use Modules\Shared\Enums\FilterErrorType; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\Response; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\Filters\FilterInterface; use CodeIgniter\Config\Services;
class UrlFilter implements FilterInterface { public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) {
}
public function before(RequestInterface $request, $arguments = null) {
$response = Services::response(); $uri = uri_string(); $pathArray = array(); if (strlen($uri)>1) { $pathArray = explode('/', $uri); }
if (count($pathArray) <= 1) { return $response->setJSON(['success' => false, 'error' => lang('Site.filter.url'), 'type' => FilterErrorType::Url])->setContentType('application/json') ->setStatusCode(Response::HTTP_BAD_REQUEST, lang('Response.filter.url')); }
if (in_array('api', $pathArray) === false){ return $response->setJSON(['success' => false, 'error' => lang('Site.filter.url'), 'type' => FilterErrorType::Url])->setContentType('application/json') ->setStatusCode(Response::HTTP_BAD_REQUEST, lang('Response.filter.url')); }
}
}
Enlightenment Is Freedom
07-06-2021, 09:50 PM
(This post was last modified: 07-06-2021, 10:23 PM by [email protected].)
(07-06-2021, 08:18 AM)Magellan Wrote: The filter seems correct to me, maybe the problem is in the 'isAdminLoggedIn' session value,. Are you sure it is correctly set?
Yes it is correctly set
(07-06-2021, 08:56 AM)ikesela Wrote: try follow format must implement before and after function
Both functions before and after is already implemented but filters not working.
-
Chroma
Member
-
Posts: 116
Threads: 7
Joined: Nov 2014
Reputation:
1
I had a lot of trouble figuring out filters, but I see that you have api* rather than what I would have expected api/*
I don't know if that matters, but it might.
I am using filters in a custom namespace and that is working fine in all version of CI 4 that I have tried.
|