Laravel内置Remember Me功能失效求助:添加Socialite后仅短时有效
Hey Pierre, let's break down why your Laravel "Remember Me" feature is only sticking around for a few hours after adding Socialite. Since you haven't modified core/vendor code or touched related configs, here are the most probable issues to check and fix:
1. Verify Socialite Login Logic Includes "Remember Me" Handling
The most likely culprit is that your Socialite authentication callback isn't properly enabling the remember token when logging users in. Unlike Laravel's default login form, Socialite doesn't automatically handle the "remember me" checkbox—you have to explicitly pass the remember flag to the authentication method.
Check your Socialite callback code (usually in AuthController or a dedicated SocialiteController):
// ❌ Wrong: Doesn't enable persistent login Auth::login($user); // ✅ Correct: Respects the "remember me" preference (if using a checkbox) $remember = $request->has('remember'); // Or hardcode to true if you want it always on Auth::login($user, $remember);
If you're using loginUsingId($user->id) instead of Auth::login(), note that loginUsingId doesn't update the remember_token by default—stick with Auth::login($user, $remember) to ensure the token is set.
2. Double-Check Session & Auth Configuration Values
Even if you didn't intentionally modify them, it's worth confirming these configs weren't indirectly altered (e.g., via environment variables):
- Open
config/session.php:- Ensure
lifetimeis set to a reasonable value (default is 120 minutes, but this is for non-remembered sessions—remembered sessions should use the auth config). - Make sure
expire_on_closeisfalse(so sessions don't die when the browser closes).
- Ensure
- Open
config/auth.php:- Under the
rememberarray, verifyexpireis set to the default432000minutes (that's 5 years). If this was accidentally changed to a small number (e.g., 120 minutes = 2 hours), that's exactly the issue you're seeing.
- Under the
- Check your
.envfile forSESSION_LIFETIMEorREMEMBER_ME_EXPIREvariables that might override the config defaults.
3. Confirm the remember_token Field is Being Updated
Every time a user logs in (including via Socialite), Laravel should regenerate and save a new remember_token to the user table. To test this:
- Look up a user in your database and note their current
remember_tokenvalue. - Log the user in via Socialite.
- Check the database again—if the
remember_tokenhasn't changed, your login logic isn't triggering the token update. This usually happens if you're not usingAuth::login($user, $remember)as mentioned earlier.
4. Check Cookie Security & SameSite Settings
Socialite involves cross-domain redirects, which can affect how browsers persist cookies. Make sure your session cookie settings are compatible:
In config/session.php:
'secure' => env('SESSION_SECURE_COOKIE', true), // Set to true if using HTTPS 'same_site' => 'lax', // Or 'none' if you need cross-domain cookies (requires secure=true)
If your site uses HTTPS but secure is false, browsers might treat the remember cookie as temporary and delete it after a short time.
5. Clear Stale Sessions & Cookies
Old, conflicting cookies or server-side sessions might be causing the issue. Try these steps:
- Clear your browser's cookies for your domain.
- Run
php artisan session:clearon your server to wipe existing sessions. - Test the "Remember Me" feature again with a fresh login.
Start with checking your Socialite login logic first—since that's the only change you made, it's the most likely root cause. If none of these fix it, let me know and we can dig deeper!
内容的提问来源于stack exchange,提问作者Pierre Le Bot

