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