laravel原生验证密码是否正确
xuexi 2025-08-29 00:08:10 发表在:PHP 查看数:22
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    public function authenticate(Request $request)
    {
        // 先验证请求数据
        $request->validate([
            'email' => 'required|email',
            'password' => 'required',
        ]);

        // 查找用户
        $user = User::where('email', $request->email)->first();

        // 检查用户是否存在且密码是否正确
        if (!$user || !Hash::check($request->password, $user->password)) {
            return back()->withErrors([
                'email' => '提供的凭据与我们的记录不匹配。',
            ]);
        }

        // 密码正确,执行登录等操作
        auth()->login($user);

        return redirect()->intended('/dashboard');
    }
}
最近访问时间:2025-08-30 15:42:53
知识库:376条鸣谢:TAY  备案号:蜀ICP备2024090044号-1