mercredi 16 décembre 2015

Laravel 5.1 API redirects ALL requests to /home after a user have registered

I have the weirdest problem ever...

Currently I have 2 routes in my laravel 5.1 project which i will use as my api.

Route::post('register', 'Auth\AuthController@postRegister');
Route::post('login', 'Auth\AuthController@postLogin');

First time I make an ajax call from my front-end to the 'register' route, everything works as expected.

However, if I make another ajax call to ANY laravel route, it redirects me to the 'home' route.

My code:

AuthController:

public function postRegister(CreateUserRequest $request)
{
     User::createUser($request);

     return response()->json();
}

public function postLogin(Request $request)
{
    if (!Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
        return response()->json()->setStatusCode(403);
    }

    return response()->json();
}

Model:

public static function createUser($data)
{
    DB::table('users')->insert([
        'f_name' =>             $data->f_name,
        'l_name' =>             $data->l_name,
        'gender' =>             $data->gender,
        'email' =>              $data->email,
        'birth_date' =>         $data->birth_date,
        'password' =>           bcrypt($data->password),
        'activation_code' =>    $data->activation_code
    ]);
}

Ajax call:

    $.ajax({
        url: Api.route('register'),
        method: 'POST',
        data: requestData,
        statusCode: {
            200: function()
            {
                console.log('register: 200');
            },
            422: function()
            {
                console.log('register: 422');
            }
        }
    });

I have tried:

  • making the first ajax call, then commenting out everything in the controller to only return a string, but it still redirects me to the 'home' route on the second request.
  • If i comment out the "DB::insert(...)" it works like expected.
  • If I make the "DB::insert()" without any data it also works like expected.
  • If i make the "DB::insert()" with any single field it redirects on the second call.
  • If I use the default Request instead of my custom request it's still redirecting.

Any suggestions are greatly appreciated!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire