Welcome Guest, Not a member yet? Register   Sign In
auth() differs from my localapi and remote
#1

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.
Reply
#2

Solution was to:

public string $samesite = 'None';

on the Config\Cookie.php

Might help others
Reply
#3

No it won't, That is the Web CORS.

Cross-Origin Resource Sharing (CORS)
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(This post was last modified: 05-06-2024, 01:00 AM by JohnUK.)

Good morning.
In my case, both the CORS and the cookies settings had to be changed to suit my setup. For the CORS I had to set the allow-origin header to allow the different origins rather than the same domain. This fixed the browser errors.
Since my authentication was cookie based - using shield - the cookies settings needed also to be set as on my previous post. This fixed the error that I described on the first post.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB