2

I am get an empty response when I $.ajax to a route that executes the controller below from a cross domain request. When I uncomment the var_dump line I get a response with data otherwise I get a 404 response and the responsejson object is undefined. Any help greatly appreciated. When I access the get equivalent of the same route directly in the browser, I get a valid json response.

<?php

use App\Models\User;

class AuthenticationController extends \BaseController {

public function getLogin() {
    return $this->postLogin();
}

public function postLogin() {
    $credentials = array(
        'email' => Input::get('email'),
        'password' => Input::get('password')
    );

    try {
        $user = Sentry::authenticate($credentials, false);

        if ($user) {
            //var_dump(array('flash' => 'Authentication failed'));
            //return Response::json(array('flash' => 'Authentication failed'), 401);
            return $user->toJson();
        }
    } catch (Exception $e) {
        return Response::json(array('flash' => 'Authentication failed'), 401);
    }
}

public function getLogout() {
    Sentry::logout();
    return Response::json(array('flash' => 'Logged out'), 200);
    //return Redirect::route('admin.login');
}
}

2 Answers 2

2

Have you tried something like this:

try {
    $user = Sentry::authenticate($credentials, false);

    if ($user) {
        return Response::json($user);
        # or
        Response::json($user)->send();
    }
} catch (Exception $e) {
    return Response::json(array('flash' => 'Authentication failed'), 401);
}
Sign up to request clarification or add additional context in comments.

1 Comment

SeanNieuwoudt, I believe the code you have would work. Thanks for the quick response. I turned out the problem was from me sending headers twice.
2

It turns out I was sending headers twice. I initially had trouble with CORS since I was connecting from an Angular JS front end running on local host to this laravel API also running on local host but on a different port. I was sending a header at the top of my Routes.php like this:

header('Access-Control-Allow-Origin', '*');

file even though my filters.php already had the code shown below:

App::before(function($request)
{
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {

    header('Access-Control-Allow-Origin', '*');
    header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
    header('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept, Authorization, X-Request-With');
    header('Access-Control-Allow-Credentials', 'true');

    exit;
}
});

App::after(function($request, $response)
{
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Allow', 'GET, POST, OPTIONS');
$response->headers->set('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept, Authorization, X-Requested-With');
$response->headers->set('Access-Control-Allow-Credentials', 'true');
return $response;
});

I hope this helps someone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.