Laravel Error MethodNotAllowedHttpException

0

I'm doing a JWT login and at the time of testing the route I get an error:

  

MethodNotAllowedHttpException

routes.php

 Route::group(['middleware'=>'cors'], function(){
    Route::post('/auth_login', 'ApiAuthController@userAuth');
});

ApiAuthController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class ApiAuthController extends Controller
{
    public function userAuth(Request $requet){
        $credentials = $request->only('email','password');
        $token = null;
        try{
            if(!$token = JWTAuth::attempt($credentials)){
                return response()->json('error','invalid credentials');
            }

        }catch(JWTException $ex){
                return responsee()->json(['error'=>'somthing_went_worng'],500);
        }
   return response()->json(compact('token'));
    }
}

Why is this error?

    
asked by Felipe Ahumada Araya 21.06.2016 в 15:49
source

1 answer

0

The solution was the following

As you realize, this receives a request call in which some credentials arrive such as mail and password, but the call is to the User model, which is where the data is linked. Therefore the solution is to call

use App\User;

With only that, it is solved, the complete solution is

  <?php 
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use JWTAuth;

use App\User;

class ApiAuthController extends Controller
{
    public function userAuth(Request $request){
        $credentials = $request->only('email','password');
        $token = null;
        $user = null;
        try{

            if(! $request->has('password')){
                $user = User::where('email', $request->input('email'))->first();

                if (empty($user)){
                    $user = User::create([
                        'name' => $request->input('name'),
                        'email' => $request->input('email'),
                        'avatar' => $request->input('avatar')
                    ]);
                }

                if(! $token = JWTAuth::fromUser($user)){
                        return response()->json(['error' => 'invalid_credentials'], 500);
                }

            }else{
                if(! $token = JWTAuth::attempt($credentials)){
                    return response()->json(['error' => 'invalid_credentials'], 500);
                }
                $user = JWTAuth::toUser($token);
            }
        }catch(JWTException $ex){
            return response()->json(['error' => 'Something_went_error'], 500);
        }

        return response()->json(compact('token', 'user'));
    }

}
    
answered by 21.07.2016 в 20:26