Welcome Guest, Not a member yet? Register   Sign In
  auth() differs from my localapi and remote
Posted by: JohnUK - 05-04-2024, 04:14 AM - Replies (3)

Hello.
I am not very knowable on CodeIgniter or shield, but I was able to get my project going locally on my machine.
I created a API made in codeigniter4 and shield. It works nice locally.
Two days ago I place it on a server and I am running the frontend from my local machine. The online API behaves differently from my local API and believe me that I tested all that I could, so I request for help.
On the server I tried to debug and to provide information for this post.
On my Controllers/AuthController.php inside the userlogin() function, I made sure that the login is working using:

PHP Code:
                   
                    log_message
('info''auth()->loggedIn(): ' auth()->loggedIn()); 

this outputs for the log file:
                                    INFO - 2024-05-04 10:51:58 --> auth()->loggedIn(): 1

That looks ok, login information is also fetched (name, id, ...). All ok and equal to my localhost.
After the login, and after the user presses a button to go to a reserved area, a filter is applied and here the code:
PHP Code:
<?php

namespace App\Filters;

use 
CodeIgniter\Filters\FilterInterface;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;

class 
GroupFilter implements FilterInterface
{
    /**
    * Do whatever processing this filter needs to do.
    * By default it should not return anything during
    * normal execution. However, when an abnormal state
    * is found, it should return an instance of
    * CodeIgniter\HTTP\Response. If it does, script
    * execution will end and that Response will be
    * sent back to the client, allowing for error pages,
    * redirects, etc.
    *
    * @param RequestInterface $request
    * @param array|null      $arguments
    *
    * @return mixed
    */
    public function before(RequestInterface $request$arguments null)
    {
        $user auth()->user();
        
        log_message
('info''Is user logged: ' auth()->loggedIn());

        if ($user == null) {
            $response service('response');
            $response->setStatusCode(401);
            $response->setBody("{\"error\": \"unauthorized - login must be previously made\"}"); 

outputs:
             INFO - 2024-05-04 10:51:58 --> Is user logged:

(note that it does not contain 0 or 1, it is simply empty). $user is also null.
On my localserver this contains the correct information. No other error is detected. It is like auth() is not working.
I checked and the auth service is loaded on the Autoload.php, though a helper.

Thank you for any advice.


  Shield Common Code to Detect Force Password Reset
Posted by: bmmcito - 05-04-2024, 02:56 AM - Replies (3)

Hi,
Just looking for som eguidance as to where to detect if a Force Password Reset has been requested.
I'd like this to be on EVERY page, so that no matter where a user is, when a force password reset is set, they are redirected to the change password form we have.
Can someone please give me some guidance as to where to add some code to detect this before every page is rendered (except password reset / login / logout)


  PHPUnit on Production
Posted by: paulkd - 05-04-2024, 12:14 AM - Replies (1)

Hi,

CodeIgniter 4.5.1

I'm getting the following message on my production server after deploying with GIT

Code:
Warning: require(/home/project/vendor/composer/../phpunit/phpunit/src/Framework/Assert/Functions.php): Failed to open stream: No such file or directory in /home/project/vendor/composer/autoload_real.php on line 55

Note: I've edited the actual project folder names above.

I've removed "phpunit/phpunit": "^9.5"  from composer.json "require-dev" and run composer update, but it seems that something is still adding it to the composer.lock file

I also have this entry in .gitignore
Code:
# Don't save phpunit under version control.
phpunit

Any help/pointers would be appreciated


  Ci4, CLI Spark Run Standart Command Terminal
Posted by: dhiya as - 05-03-2024, 11:38 PM - Replies (4)

hi, is there a function/feature to run the command line outside the spark command?

I am creating an application installation feature via the spark CLI, 
I want to run several commands such as 
"composer require..", "cd", etc, 
these commands are standard from the terminal..can I do that with spark?


  Can't read a session
Posted by: ElTomTom - 05-03-2024, 11:26 AM - Replies (5)

We configure our project to use the default suggested by CodeIgniter for session configuration, we only changed the name of the cookie.

We grant the permissions to the session directory in Ubuntu:

Code:
chmod -R 755 writable/
chown -R www-data:www-data writable/


There is no ajax or anything else, just normal access with views.
But as soon as we run the project, the error appears in the log:


PHP Code:
ERROR 2024-05-03 18:19:45 --> SessionUnable to obtain lock for file '/var/www/html/website/writable/session/mysession....4vpq81...psa3h'.
CRITICAL 2024-05-03 18:19:45 --> ErrorExceptionsession_start(): Failed to read session datauser (path: /var/www/html/website/writable/session)
in SYSTEMPATH/Session/Session.php on line 919.
[internal function]: CodeIgniter\Debug\Exceptions->errorHandler()
2 SYSTEMPATH/Session/Session.php(919): session_start()
3 SYSTEMPATH/Session/Session.php(241): CodeIgniter\Session\Session->startSession()
4 SYSTEMPATH/Config/Services.php(699): CodeIgniter\Session\Session->start()
5 SYSTEMPATH/Config/BaseService.php(258): CodeIgniter\Config\Services::session()
6 SYSTEMPATH/Config/BaseService.php(199): CodeIgniter\Config\BaseService::__callStatic()
7 SYSTEMPATH/Config/Services.php(664): CodeIgniter\Config\BaseService::getSharedInstance()
8 SYSTEMPATH/Config/BaseService.php(258): CodeIgniter\Config\Services::session()
9 APPPATH/Controllers/Login.php(16): CodeIgniter\Config\BaseService::__callStatic()
10 SYSTEMPATH/CodeIgniter.php(915): App\Controllers\Login->__construct()
11 SYSTEMPATH/CodeIgniter.php(494): CodeIgniter\CodeIgniter->createController()
12 SYSTEMPATH/CodeIgniter.php(361): CodeIgniter\CodeIgniter->handleRequest()
13 FCPATH/index.php(79): CodeIgniter\CodeIgniter->run() 


PHP Code:
<?php

namespace Config;

use 
CodeIgniter\Config\BaseConfig;
use 
CodeIgniter\Session\Handlers\FileHandler;

class 
Session extends BaseConfig
{

    public string $driver FileHandler::class;

    public string $cookieName 'mysession';

    public int $expiration 7200;

    public string $savePath WRITEPATH 'session';

    public bool $matchIP false;

    public int $timeToUpdate 300;

    public bool $regenerateDestroy false;

    public ?string $DBGroup null;



What could be happening?


Ps.: My php.ini is configured by default to use a session with Redis.


  Unlocking the Power of SQL Queries for Improved Performance
Posted by: InsiteFX - 05-03-2024, 11:23 AM - No Replies

Unlocking the Power of SQL Queries for Improved Performance


  custom validation dinamic
Posted by: pippuccio76 - 05-03-2024, 10:04 AM - Replies (5)

hi there is a way to send to parameter to a validation rule ?

Code:
    public function required_if($first_parameter,$second_parameter)
    {

    if(!$first_parameter ){

    return true;

    }else if($first_parameter AND !$second_parameter){
   
    return false;
   
    }else{
   
    return true;
   
    }
   
    }


If is possible how can i pass they in rules ?


Question MaintenanceFilter vs JavaScript
Posted by: Gary - 05-03-2024, 01:19 AM - No Replies

With all the changes to CI's routing, I'm wondering if there is a clever trick to be able to bypass the Maintenance filter on a (in my case necessary) second entry?

In order to refresh the page (with a "Down for Maintenance" page), it is necessary to initiate a page refresh... which is easy enough to initiate in the (now enabled) MainenanceFilter.  The problem is that this means there is now a second request (redirection) coming in from the client... which then gets repeatedly caught by the MaintenanceFilter.

I have patched it with a few new $_SESSION variables... to indicate that it's a reentry call... and also some changed to the users' $_SESSION (so as to disable the users' control/input, in such a manner that the session can be be restored/re-enabled, if the maintenance outage is only going to be brief).

So the question is... although I have it working, there are messy (and seldom required) $_SESSION checks and processing now dotted around elsewhere in the code, which irritates me... is there a more elegant way of handling this need for the reentry/second run though the Filter/s?


  Trying to remove index.php from url in latest codeigniter 4.5.1
Posted by: kabeza - 05-02-2024, 08:57 AM - Replies (7)

Hi
I've started a new codeigniter 4.5.1 appstarter project at my PC and am having difficult time to remove the index.php from uri

1) This is the virtualhost set in my Apache. Points at public as suggested

Code:
<VirtualHost *:80>
    ServerAdmin admin@localhost
    ServerName sw
    ServerAlias sw
    DocumentRoot /media/data/services/public
    <Directory /media/data/services/public>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error_sw.log
    CustomLog ${APACHE_LOG_DIR}/access_sw.log combined
</VirtualHost>

2) I've enabled the mod_rewrite, restarted apache, edited /etc/hosts file. Then I browse to http://sw , which shows the codeigniter welcome, great!

3) I rename the root/env file to .env and leave it with

Code:
#--------------------------------------------------------------------
# APP
#--------------------------------------------------------------------
CI_ENVIRONMENT = development
app.baseURL = 'http://sw'
app.indexPage = ''

4) I define a new route in app/config/Routes.php

Code:
$routes->get('/services', 'Services::index');

Then, my controller app/Controllers/Services.php looks like

PHP Code:
<?php

namespace App\Controllers;

class 
Services extends BaseController
{
    public function index(): string
    
{
        return view('view_services');
    }


Then, when I try this url in browser

http://sw/services

I'm getting 404 ...

5) Tried also replacing the public/.htaccess with the suggested in
https://codeigniter.com/user_guide/gener...x-php-file
No luck

6) I've also tried setting the baseURL, indexPage, etc. options in app/Config/App.php without luck

I've got no ideas left to solve this
Thanks for any suggestion


  Convert Filters to Registrar
Posted by: Willen - 05-02-2024, 08:06 AM - Replies (5)

Hello! 

I am having a little bit of trouble understanding the deprecation of Filters in favor of Registrars. I need some help in order to get my head around how Registrars work and what the pros are compared to the now deprecated Filters-way of doing things.
To convert my Filters to Registrar, I tried to follow the minimal instruction in the deprecated-note on the documentation-page. Since it does not cover much, I am not sure if I have done it correctly and what else I need to do. I have created a Registrar.php file in the Config-folder, where my filters are defined following the structure in the example:

Code:
<?php namespace App\Config;

use App\Filters\{AuthFilter, Cors, AdminFilter};

class Registrar {
  public static function Filters(): array {
    return [
      'aliases' => [
        'authFilter' => AuthFilter::class,
        'cors' => Cors::class,
        'adminFilter' => AdminFilter::class,
      ],
    ];
  }
}

I am a little unsure of what more I need to do, if anything? Should I change anything in the actual Filters-files? Can I remove the Filters from the $aliases list in Filters.php? Does it change the way I use the Filters in the Routes file?
I currently have three filters; Cors, AdminFilter and AuthFilter. These filters are being used before certain routes. For example, the AuthFilter makes sure a request is authenticated before letting the request through to its respective endpoint. My Cors filter handles Cors, and runs before any request. These filters work flawlessly with the now deprecated Filter-structure, but I do not get how I use them in my Routes the Registrars way. This is how my routes look now:

Code:
$routes->group('', ['filter' => 'cors'], static function (RouteCollection $routes): void {

  $routes->group('auth', static function ($routes) {
    $routes->options('login', 'Auth::login');
    $routes->post('login', 'Auth::login');
    $routes->options('refresh_token', 'Auth::refresh_token');
    $routes->get('refresh_token', 'Auth::refresh_token', ['filter' => 'authFilter']);
  });

  $routes->group('users', static function ($routes) {
    $routes->options('get_all_users', 'Users::get_all_users');
    $routes->get('get_all_users', 'Users::get_all_users', ['filter' => 'authFilter']);
    $routes->options('delete_user_by_username', 'Users::delete_user_by_username');
    $routes->delete('delete_user_by_username', 'Users::delete_user_by_username', ['filter' => 'adminFilter']);
    $routes->options('add_user', 'Users::add_user');
    $routes->post('add_user', 'Users::add_user', ['filter' => 'adminFilter']);
  });


I made these filters a while ago, so I was not aware of Codeigniters own Cors-filter. But now that I have looked into it, it also uses the Filter-way of doing things and not the Registrar-way. Filters was set to deprecated in 4.2, how come a new feature that is introduced in 4.5 use a deprecated way of doing things?


Welcome, Guest
You have to register before you can post on our site.

Username
  

Password
  





Latest Threads
Set a $_GET value
by CCAldo
29 minutes ago
how to use set_select on ...
by demyr
1 hour ago
CodeIgniter Tasks - Regis...
by ltarrant
4 hours ago
Mastering SQL: Comprehens...
by InsiteFX
5 hours ago
Call multiple function in...
by warcooft
6 hours ago
Call to undefined functio...
by nanohe
10 hours ago
Query Builder Conditions ...
by kenjis
10 hours ago
Create a copy of file fro...
by Ege
Yesterday, 04:01 PM
How to use Amazon PHP SDK...
by asimeou
Yesterday, 10:53 AM
fatal logger emergency
by kenjis
Yesterday, 03:39 AM

Forum Statistics
» Members: 87,699
» Latest member: vn88vet
» Forum threads: 77,651
» Forum posts: 376,397

Full Statistics

Search Forums

(Advanced Search)


Theme © iAndrew 2016 - Forum software by © MyBB