CodeIgniter Forums
CI4 Filter before method throw for page unauthorized - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forum-31.html)
+--- Thread: CI4 Filter before method throw for page unauthorized (/thread-76046.html)



CI4 Filter before method throw for page unauthorized - 404NotFound - 04-09-2020

I'm able to filter request to my rest api using ci4 filter.
But the problem is, I dont understand how to throw the error for showing page unauthorized like in http message.

PHP Code:
class Authorization implements FilterInterface {
  public function before(RequestInterface $request){
    $token $request->getServer('HTTP_AUTHORIZATION')
    if($token == null){
      // How can I send the response that showing unauthorized 401?
      // 
    }
  }




RE: CI4 Filter before method throw for page unauthorized - jean5769 - 04-09-2020

Hello,

I use HTTP Responses to manage my response. For example, if my website has a maintenance mode activated, I will send this :

$this->response->setStatusCode(503);
$this->response->setHeader('Retry-After', '3600');
$this->response->setBody('<h1>Maintenance mode activated.</h1>');
return $this->response->send();

You can have more informations about this at this page : https://codeigniter4.github.io/userguide/outgoing/response.html

Also, for REST API, I think you can use API Response Trait : https://codeigniter4.github.io/userguide/outgoing/api_responses.html

I never used API Response, but you can use something like that :

return $this->respond($data, 401, $description);

OR

return $this->failUnauthorized($description);

Hope this help you !


RE: CI4 Filter before method throw for page unauthorized - 404NotFound - 04-10-2020

(04-09-2020, 11:05 PM)jean5769 Wrote: Hello,

I use HTTP Responses to manage my response. For example, if my website has a maintenance mode activated, I will send this :

$this->response->setStatusCode(503);
$this->response->setHeader('Retry-After', '3600');
$this->response->setBody('<h1>Maintenance mode activated.</h1>');
return $this->response->send();

You can have more informations about this at this page : https://codeigniter4.github.io/userguide/outgoing/response.html

Also, for REST API, I think you can use API Response Trait : https://codeigniter4.github.io/userguide/outgoing/api_responses.html

I never used API Response, but you can use something like that :

return $this->respond($data, 401, $description);

OR

return $this->failUnauthorized($description);

Hope this help you !

I already try that in my filter. Its not working properly.
It is sending the right code which is 401, but it also sent the body too.
That code implement new different response, not the one that used and shared by the controller.
I need to say again that this is Filter, not a controller, or response trait.


RE: CI4 Filter before method throw for page unauthorized - jean5769 - 04-10-2020

And what about this code ?

$response = \Config\Services::response();
$response->setStatusCode(401);
$response->send();
die();

You send the response and you kill your script, so there is no body.


RE: CI4 Filter before method throw for page unauthorized - kilishan - 04-10-2020

Returning a response object works as well.

PHP Code:
$response service('response');
$response->setStatusCode(401);

return 
$response



RE: CI4 Filter before method throw for page unauthorized - 404NotFound - 04-10-2020

(04-10-2020, 07:04 AM)kilishan Wrote: Returning a response object works as well.

PHP Code:
$response service('response');
$response->setStatusCode(401);

return 
$response

Works like a charm ✨
Awesome thanks for this.
How can I stupidly missed the services


RE: CI4 Filter before method throw for page unauthorized - kmp8072 - 12-15-2020

(04-09-2020, 06:53 PM)404NotFound Wrote: I'm able to filter request to my rest api using ci4 filter.
But the problem is, I dont understand how to throw the error for showing page unauthorized like in http message.

PHP Code:
class Authorization implements FilterInterface {
  public function before(RequestInterface $request){
    $token $request->getServer('HTTP_AUTHORIZATION')
    if($token == null){
      // How can I send the response that showing unauthorized 401?
      // 
    }
  }
}

This class is not extending resource controller so first you need to do that i did 

use CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\Filters\FilterInterface;
use 
CodeIgniter\RESTful\ResourceController;
use 
CodeIgniter\API\ResponseTrait;

class 
AuthFilter extends ResourceController implements FilterInterface{
public 
$response;

use 
ResponseTrait;

public function 
before(RequestInterface $request$arguments null)

    {
         
$this->response = \Config\Services::response(); // if you don't do this response will be undefined and error will be thrown
         
if something not authorised

         $description 
lang('Validation.token_invalid');

         return $this->failUnauthorized($description);

    }