Laravel登录系统问题:页面重载用户登出,需保持登录5分钟
问题解决步骤
1. 修复页面重载登出问题
你当前直接返回视图的方式会导致会话无法正确持久化,Laravel需要通过重定向来确保会话Cookie被浏览器正确接收并保存。修改登录成功后的响应逻辑:
public function dologin(Request $request) { $email = $request->input('email'); $password = $request->input('password'); $customer = Customer::where('email', $email)->first(); if ($customer && Hash::check($password, $customer->password)) { Auth::login($customer); // 替换直接返回视图为路由重定向 return redirect()->route('customerprofile'); } else { return back()->withErrors([ 'email' => ['Les informations de connexion sont incorrectes.'], ]); } }
同时,确保customerprofile路由对应的控制器方法中,通过Auth::user()获取登录用户数据并传递给视图:
// 示例:customerprofile路由对应的控制器方法 public function showProfile() { $user = Auth::user(); return view('customerprofile', compact('user')); }
2. 设置登录状态保持5分钟
要让登录状态保持5分钟,只需修改会话过期时间:
- 打开项目根目录的
.env文件,添加或修改:SESSION_LIFETIME=5 - 或者直接修改
config/session.php中的lifetime配置项:'lifetime' => env('SESSION_LIFETIME', 5),
修改后,用户登录后的会话会在5分钟无活动后自动过期登出。
额外检查
确认你的Customer模型正确实现了Laravel认证所需的接口:
use Illuminate\Foundation\Auth\User as Authenticatable; class Customer extends Authenticatable { // 模型其他代码... }
如果模型未继承Authenticatable类,需要确保它实现了Illuminate\Contracts\Auth\Authenticatable接口的所有方法。
内容的提问来源于stack exchange,提问作者Adéwalé AKANNI
相关产品推荐
相关产品推荐

