laravel原生验证密码是否正确
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