1 year ago

Error messages output in API

Posted 1 year ago by AlexNsk

Hi, everyone. When I try to logn in in my API, if all login data was provided correctly - everything is fine, but if I make some mistake I want to get messages with validation errors. But it`s just redirects me to homepage. Here is my Request:

class SignInRequest extends FormRequest
    public function authorize()
        return true;

    public function rules()
        return [
            'email' => 'required|exists:users',
            'password' => 'required|min:6',

And Controller:

public function signIn(SignInRequest $request): JsonResponse
        $authResult = Auth::attempt([
            'email' => $request->email,
            'password' => $request->password,

        if($authResult) {
            Auth::user()->api_token = str_random(64);
            return response()->json([
                'name' => Auth::user()->name,
                'email' =>Auth::user()->email,
                'api_token' => Auth::user()->api_token
            ], 200);
        return response()->json('user not found', 404);

Please sign in or create an account to participate in this conversation.