Laravel 7 护照 Api 验证
Laravel 7 Passport Api Validation
我创建了一个 Laravel 登录护照 API 但是当提供电子邮件“null”或密码为 null 时,它没有显示需要验证的错误。它正在返回 html 的东西?
这是我的控制器代码:
<?php
namespace App\Http\Controllers\Api;
use\App\User;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class AuthController extends Controller
{
public function login(Request $request)
{
$loginData= $request->validate([
'email'=>'required',
'password'=>'required',
]);
if(!auth()->attempt($loginData))
{
return response()->json(['message'=>'Invalid credientails']);
}
$accessToken =auth()->user()->createToken('authToken')->accessToken;
return response()->json(['user'=>auth()->user(),'access_Token'=>$accessToken]);
}
}
试试这个
另外请确保在控制器中包含 class 验证器-> 使用验证器;
$validator = Validator::make($request->all(), [
'email'=>'required',
'password'=>'required',
]);
if ($validator->fails())
{
$message = $validator->errors()->first();
return response()->json(['statusCode'=>200,'success'=>false,'message'=>$message], 200);
}
我创建了一个 Laravel 登录护照 API 但是当提供电子邮件“null”或密码为 null 时,它没有显示需要验证的错误。它正在返回 html 的东西?
这是我的控制器代码:
<?php
namespace App\Http\Controllers\Api;
use\App\User;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class AuthController extends Controller
{
public function login(Request $request)
{
$loginData= $request->validate([
'email'=>'required',
'password'=>'required',
]);
if(!auth()->attempt($loginData))
{
return response()->json(['message'=>'Invalid credientails']);
}
$accessToken =auth()->user()->createToken('authToken')->accessToken;
return response()->json(['user'=>auth()->user(),'access_Token'=>$accessToken]);
}
}
试试这个
另外请确保在控制器中包含 class 验证器-> 使用验证器;
$validator = Validator::make($request->all(), [
'email'=>'required',
'password'=>'required',
]);
if ($validator->fails())
{
$message = $validator->errors()->first();
return response()->json(['statusCode'=>200,'success'=>false,'message'=>$message], 200);
}