You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel内置Remember Me功能失效求助:添加Socialite后仅短时有效

Troubleshooting Laravel's "Remember Me" After Adding 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 lifetime is 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_close is false (so sessions don't die when the browser closes).
  • Open config/auth.php:
    • Under the remember array, verify expire is set to the default 432000 minutes (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.
  • Check your .env file for SESSION_LIFETIME or REMEMBER_ME_EXPIRE variables 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:

  1. Look up a user in your database and note their current remember_token value.
  2. Log the user in via Socialite.
  3. Check the database again—if the remember_token hasn't changed, your login logic isn't triggering the token update. This usually happens if you're not using Auth::login($user, $remember) as mentioned earlier.

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:clear on 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:14:36