0

我创建了一个 Laravel 登录护照 API,但是当提供的电子邮件“空”或密码为空时,它没有显示需要验证的错误。它正在返回一些东西的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]); 
   }
}
4

1 回答 1

1

尝试这个

另外请确保在控制器中包含类验证器-> 使用验证器;

$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);            
    }
于 2020-07-22T14:42:08.830 回答