gpt4 book ai didi

Laravel - 测试 api 时方法 Illuminate\Auth\RequestGuard::attempt 不存在

转载 作者:行者123 更新时间:2023-12-03 20:14:59 24 4
gpt4 key购买 nike

我将 Laravel-Passport 用于我的 api 端点的 Laravel 5.8 身份验证

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;
use Validator;

public function login(Request $request)
{
$validator = Validator::make($request->all('email', 'password'), [
'email' => 'required|string|email|max:255',
'password' => 'required|string|min:6'
]);

if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()], 401);
};

if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
$user = Auth::user();
$success['token'] = $user->createToken('MyApp')-> accessToken;
return response()->json(['success' => $success], 200);
}
else{
return response()->json(['error'=>'Can not log in with the data provide.'], 401);
}
}

身份验证文件
    'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],

'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],

当我在 POSTMAN 上测试端点时,出现以下错误:

postman_error

错误的原因是什么,我该如何解决?

BadMethodCallException Method Illuminate\Auth\RequestGuard::attempt does not exist.

最佳答案

要使用用户名和密码登录用户,您需要使用您的 web像这样的身份验证守卫:

if(Auth::guard('web')->attempt(['email' => request('email'), 'password' => request('password')])){
$user = Auth::guard('web')->user();

关于Laravel - 测试 api 时方法 Illuminate\Auth\RequestGuard::attempt 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57502062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com