Retrieving a Session value in Laravel returns null

Viewed 3824

I am setting and attempting to retrieve a session value from a Laravel application. A Vue Js frontend is making an API call to a Laravel endpoint which sets the session value. The controller which contains the method for the Laravel endpoint is:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
Use Session;

use App\User;

class UserController extends Controller
{
    public function adduser(Request $request) {

        $user = User::where( 'sub', $request->sub )->first();

        if(!$user) {

            $user = new User();

            $user->nickname = $request->nickname;
            $user->name = $request->name;
            $user->sub = $request->sub;
            $user->api_token = $request->api_token;

            $user->save();

        }

        $user->api_token = $request->api_token;
        $user->save();

        return response()
            ->json([
                'done' => $user->api_token
            ]);

    }

    //The endpoint for this method is http://localhost:8000/api/set
    public function setSession(Request $request) {


        $request->session()->put('info', 'testing vue client again');
        Session::save();

        return response()
            ->json([
                'done' => session('info')
            ]);

    }
    //The endpoint for this method is: http://localhost:8000/api/get
    public function getSession(Request $request) {

        $value = $request->session()->get('info');
        return response()
            ->json([
                'done' => $value
            ]);

    }

}

The session data is set successfully, however when I try to retrieve this data from the getSession() method it returns null. This is the Vue Js/axios code snippet I used to attempt to retrieve the session data

axios({
 method: 'get',
 url: 'http://localhost:8000/api/get',


}).then((res) => {
console.log(res.data)
})

How do I solve this issue? Thanks

2 Answers

Routes from routes/api.php file by default doesn't have StartSession middleware because api calls considered as stateless. You can add this middleware withEncryptCookies to api middleware group in your app/Http/Kernel.php file like this, with exact sequence:

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        'throttle:60,1',
        'bindings',
    ],
];

Works for me. Also, think you can setup api group exactly as web group in your case.

And don't forget set your session variable again with new configuration.

I had reproduce the issue and seems problem with cookies. This did the trick for me:

app\Http\Middleware\EncryptCookies.php

protected $except = [
    'laravel_session',
];

Change laravel_session to your config('session.cookie') value.

See Session ID changes on refresh (Laravel 5.4 + AngularJS 1.6)

Related