Session and Auth in Nuclio. How to use it in proper way?

179 views Asked by At

When i try to called: Auth::getInstance()->authenticate($email,$password) for authenticate in login controller, i called Auth::getInstance()->isAuthenticated() and get result bool(true). Then i go redirect to another page, Auth::getInstance()->isAuthenticated() give bool(false). After i use this authentication, how can i get the session is already bool(true) at any page after that until i'm Auth::getInstance()->unauthenticate() that session or make it global for the session? Currently i'm using session database.

Problem : How to authenticate the current user after redirect to another page?

2

There are 2 answers

0
AdamGiles On

Without knowing more about your code, I can predict a couple of possible sources of this type of behavior...

1) You're not writing the fact that the user is authenticated to your session/cookie, so the second page request isn't aware of the result of the first one.

2) If the authentication is successful on the first page (and you record this in the session/cookie), and the redirection happens, but you redirect back to a page already seen by the user (e.g. Homepage -> Login page -> Homepage) then your browser might be loading it out of it's local cache rather than fetching the new (authenticated) page from the server.

Try dumping your session variables to the browser to see if the authentication result is being preserved between requests, and try appending a timestamp on the redirection url or using headers to prevent client side caching. This will at least allow you to narrow down, or eliminate these two options.

0
TimChandler On

The Auth plugin already manages all session control for authentication without any additional effort from the developer.

The problem you are facing could likely be because the session is not starting for some reason. This could be because Nuclio isn't detecting that it is being run from a browser. Nuclio detects this by checking REMOTE_HOST and HTTP_HOST values in $_SERVER. If both are null, it won't start the session (to avoid generating headers on a command line).

Also make sure that your base application class is extending the Nuclio Application plugin class and NOT overriding the __construct method without calling the parent construct method as this would cause all the initialization to fail and no session will be created/resumed.