Laravel 4 AUTH via AJAX, possible session release

I did this in previous projects using Sentry 2, but I can't get it to work with Auth's built-in functions.

User storage method:

public function store()
{
    $validation = new Services\Validators\User;

    if ($validation->passesForStore()) {
        // Create a new user
        $user = new User;
        $user->name     = Input::get('name');
        $user->password = Input::get('password');
        $user->email    = Input::get('email');
        $user->save();

        // Authenticate the user
        Auth::loginUsingId($user->id);

        return JSend::success([
            'redirect' => URL::action('VaultController@index')
        ]);
    }

    // Provide a custom message for an e-mailaddress that already in use
    $message = '';
    if ($validation->hasFailed('email', 'unique')) {
        $message = 'This e-mailaddress has already been taken';
    }

    return JSend::fail([
        'failed' => $validation->failedFields(),
        'message' => $message
    ]);
}

AJAX response handler:

// Redirect to given URL
if (typeof response.data.redirect != 'undefined') {
    window.location = response.data.redirect;
}

The user was successfully created by placing Auth::check();immediately after the results loginUsingIdin true, but as soon as the page is redirected to window.location, it Auth::check()leads to false and the user does not register in, even when you manually go to another page.

App.session Options:

'lifetime' => 120,
'expire_on_close' => true,

Mind that the login method works the same way, but does not work! (it works without using AJAX)

=== UPDATE ===

. , . ? vhost localhost , . ( ?).

, , . :

Route::get('logout', function(){
    Auth::logout();
    Session::flush();

    dd(Auth::check());
});

, , false. ! , . true, . "".

Route::get('authenticated', function(){
    dd(Auth::check());
});

:)

=== UPDATE 2 ===

, . , , . ?

=== UPDATE 3 ===

xxxx . / , .

/, , , .

, ( ), ,

+2

Source: https://habr.com/ru/post/1539724/


All Articles