Laravel自定义登录表单提交后重定向原页无法登录问题求助
Laravel登录重定向回登录页排查方案
- 密码哈希校验问题
Laravel的auth()->attempt()方法会自动对传入的明文密码做哈希校验,和数据库users表的password字段值做比对。如果你注册用户时没有使用Hash::make()对密码加密,直接存储了明文密码,校验会直接失败,触发重定向。你可以先查询数据库users表的password字段,正常加密后的密码是以$2y$开头的长字符串,如果是明文,请修改注册逻辑,存储密码时统一调用Hash::make()处理。 - User模型配置校验
确认你的User模型满足以下要求:
- 父类继承自
Illuminate\Foundation\Auth\User,而非默认的Model类 - 如果你手动配置了
$table属性,确认属性值和实际用户表名一致,默认不需要配置会自动关联users表 $fillable属性中已包含email、password字段,避免批量赋值异常
- 错误提示补全
你当前的错误提示仅绑定在email字段下,可先在登录页顶部添加全局错误提示,确认重定向原因:
@if ($errors->any()) <div class="alert alert-danger"> @foreach ($errors->all() as $error) <p>{{ $error }}</p> @endforeach </div> @endif
如果重定向后显示Your provided credentials could not be verified.,说明确实是账号密码校验失败,问题集中在用户数据或模型配置层面。
- 数据库连通性调试
可在SessionsController的store方法中添加临时调试代码,确认用户数据查询是否正常:
public function store() { $attributes = request()->validate([ 'email' => 'required|email', 'password' => 'required' ]); // 临时调试代码 dd(\App\Models\User::where('email', $attributes['email'])->first()); if (! auth()->attempt($attributes)) { throw ValidationException::withMessages([ 'email' => 'Your provided credentials could not be verified.' ]); } session()->regenerate(); return redirect('/')->with('success', 'Welcome'); }
提交登录后如果能输出对应用户数据,说明数据库连通正常,问题为密码校验失败;如果输出为null,说明库里无对应邮箱的用户,或模型表关联配置错误。
- 会话配置校验
确认.env文件中SESSION_DRIVER配置正常,本地开发可设置为file或database,如果会话配置错误,登录成功后会话无法持久化,也会触发登录页重定向。
内容的提问来源于stack exchange,提问作者Ben
相关产品推荐
相关产品推荐

