"Remember Me" Not Working

Closed
Vanguard - Advanced PHP Login and User Management Vanguard - Advanced PHP Login and User Management July 01, 2022
Login to reply
Andrew Hartley commented privately
Milos Stojanovic Support Agent
2 years ago

Hey Andrew,

Unfortunately, there is not much that I can do here. I've fully tested the code on multiple servers and I haven't had any issues at all, and I haven't had any other complaints from other customers. 

If it is not working for you, feel free to request a refund and I'll be glad to approve it.

Regards,
Milos

Andrew Hartley
2 years ago

Hello,

Since implementing the changes, I have been unsuccessful in the following code, which was working before.

 require __DIR__ . $dirOffset . '/vanguard/extra/auth.php';
$user = Auth::user();
 $userid = $user->id;

I get the error "Attempt to read property "id" on null" most often, but when I refresh, it ends up being fine. I really want to implement this software and hope we can work through these issues.

Andrew Hartley
2 years ago

Hey Milos,

You're right: within the testing, removing the following line did work on my server:

$kernel->pushMiddleware(\Vanguard\Http\Middleware\EncryptCookies::class);

Adding this publicly since it did work and no longer generates an error! My local server is still being a bully, but honestly, I'm just going to set the expiration time for 2 weeks on there and call it a day since it doesn't matter as much there! :)

Thank you so much for the extra help, Milos!

Andrew

Milos Stojanovic Support Agent commented privately
Andrew Hartley commented privately
Milos Stojanovic Support Agent commented privately
Andrew Hartley commented privately
Andrew Hartley commented privately
Milos Stojanovic Support Agent
2 years ago

Hey Andrew, 

That's strange, it should not create the public folder at its original location for sure, so something is messed up with the configuration. Unfortunately, since I cannot reproduce the issue you have with the latest code that I've provided, the only way for me to help will be to debug things directly on your server.

So, please provide the necessary SSH/FTP credentials so I can access your server and see what's happening. You should create a "Private" comment since this ticket is now public.

Regards,
Milos

Andrew Hartley
2 years ago

So there is an error that comes up when I FIRST use auth.php after going to the account page and back to the main site, but it's a different one. It only generates this error on the local environment, not the production, though I am using identical files on each (except for database and mail settings). After this error generates, if I refresh, it no longer recognizes the logged-in state right away. I attached the log. Thank you again!

Andrew

Andrew Hartley
2 years ago

Hello Milos,

When I remove that line of code, it does cause the log error to stop generating, but I'm back at my original starting point (same as the below video), which you anticipated with the code you provided. I tested this new code in both my local environment (XAMPP, fresh installation with SSL active) and production environment (Sitegrounds hosting), and now it won't stay logged in on any pages that use auth.php for even the duration of the SESSION_LIFETIME.

Just in case, I have also attached my .env file with sensitive information redacted on my local testing environment. The /account folder contains the contents to the /public folder, and everything was set up to route correctly to the vanguard folder on the same level as the public_html. I have no idea if this is related, and it doesn't create any issues, but whenever I access the vanguard app (either directly or when calling auth.php), it recreates an empty public folder in its original location if the folder no longer exists. I doubt this is related, but was sharing it in case it somehow was.

Just trying to provide as much context as I can. Thank you for your support!

Andrew

Milos Stojanovic Support Agent
2 years ago

Hey Andrew,

Hmm, I see what's happening. Does it work if you remove the following line?

$kernel->pushMiddleware(\Illuminate\Routing\Middleware\SubstituteBindings::class);

From what I've tested, the following code works fine for me, but I'm unsure if I have the same environment as you.

<?php

require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
$request = Illuminate\Http\Request::capture();
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$kernel->pushMiddleware(\Vanguard\Http\Middleware\EncryptCookies::class);
$kernel->pushMiddleware(\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class);
$kernel->pushMiddleware(\Illuminate\Session\Middleware\StartSession::class);
$response = $kernel->handle($request);
$response->setStatusCode(200);
$response->sendHeaders();
$kernel->terminate($request, $response);
/**
 * Redirect to provided url
 * @param $url
 */
function redirectTo($url)
{
    if (! headers_sent()) {
        header('Location: '.$url, true, 302);
    } else {
        echo '<script type="text/javascript">';
        echo 'window.location.href="'.$url.'";';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
        echo '</noscript>';
    }
    exit;
}

Regards,

Milos

Andrew Hartley
2 years ago

Hello again, so I checked my logs and notice that I'm now getting an error after the implementation. I isolated the various lines, and this is the one that seems to be giving the error:

$kernel->pushMiddleware(\Illuminate\Routing\Middleware\SubstituteBindings::class);

I've attached the log generated by a single execution.

Thank you!

Andrew

Andrew Hartley
2 years ago

Wow, that absolutely did the trick, Milos! Thank you for your help, and I am completely okay with this ticket being made public in the event anyone else runs into this issue. I appreciate the time you've spent helping me and others!

Milos Stojanovic Support Agent
2 years ago

Hey Andrew, 

Thanks for the video and explanation. 

Can you please update the extra/auth.php file to the following content and see if it resolves the issue:

<?php

require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
$request = Illuminate\Http\Request::capture();
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$kernel->pushMiddleware(\Vanguard\Http\Middleware\EncryptCookies::class);
$kernel->pushMiddleware(\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class);
$kernel->pushMiddleware(\Illuminate\Session\Middleware\StartSession::class);
$kernel->pushMiddleware(\Illuminate\Routing\Middleware\SubstituteBindings::class);
$response = $kernel->handle($request);
$response->sendHeaders();
/**
 * Redirect to provided url
 * @param $url
 */
function redirectTo($url)
{
    if (! headers_sent()) {
        header('Location: '.$url, true, 302);
    } else {
        echo '<script type="text/javascript">';
        echo 'window.location.href="'.$url.'";';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
        echo '</noscript>';
    }
    exit;
}

Regards,
Milos

Andrew Hartley
2 years ago

Sorry, I selected the wrong file. I moved the auth require file above the global config (used on the rest of the website), and it still produces the same results.

<?php

 $dirOffset = "/..";

 require __DIR__ . $dirOffset . '/vanguard/extra/auth.php';

 require __DIR__ . $dirOffset . '/config/global.php';

?>

No errors on the page. auth.php doesn't recognize that I'm logged in until it goes back to the main application, then it picks it up for the duration of the session. Though this bug is eluding me, you write some very clean code and made a fine product!

Thank you for your help!

Andrew

Milos Stojanovic Support Agent
2 years ago

Hey Andrew,

Unfortunately, your clip is only 3 seconds and I don't see anything happening in there. Can you please record a video on what exactly is happening?

Also, in the code snippet below, I see that you have the following line

 require __DIR__ . $dirOffset . '/config/global.php';

What that is for? By default, there is no need to do anything like that for Vanguard.

Regards,
Milos

Andrew Hartley
2 years ago
I made a short clip so you can see the non-logged in state on the site leads to already being logged in on the page, then once I am active on the profile page, it detects the logged in state on the rest of the site.
Andrew Hartley
2 years ago

Of course! This is the code that each page opens with:

<?php

 ## Includes ##

 $dirOffset = "/..";

 require __DIR__ . $dirOffset . '/config/global.php';

 ## Check if Logged In ##

 require __DIR__ . $dirOffset . '/vanguard/extra/auth.php';

?>

When it runs like this, it isn't detecting that I am logged in. When I go to the app, I will appear logged in. Then when I go back to the page I was at before, it now recognizes that I'm logged in for the duration set by SESSION_LIFETIME in .env.

So it is working in the main part of the app, but only works on the rest of the site for the duration of SESSION_LIFETIME.

Thank you!

Andrew

Milos Stojanovic Support Agent
2 years ago

Hey Andrew,

Can you please provide the code that you are using where you find that the "Remember Me" is not working as expected? I'm not sure that I fully understand what you mean when you say "the Remember Me is only not working through the Auth function"...

Regards,
Milos

Andrew Hartley
2 years ago

An added note: when I go to the profile section directly to login, it will route to the logged-in state. It seems like the Remember Me is only not working through the Auth function.

Milos Stojanovic Support Agent
2 years ago
Thanks for creating a ticket! We'll get back to you as soon as we are back from vacation.
Andrew Hartley
2 years ago

Hello,

The "Remember Me" function does not seem to be working and it times out at the end of SESSION_LIFETIME. These are the settings that I use:

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_DRIVER=sync
SESSION_DRIVER=database
SESSION_LIFETIME=120

Thank you for your help!

Andrew