Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Can't retrieve session data saved in POST request

I'm having an issue with Laravel session create and read. I can create session values , but problem is access the data. If I create an session value in POST request, then I can't access it on GET request.

Here is my route

Route::get('/', 'HomeController@index');
Route::get('/admin/dashboard', 'AdminController@dashBoard');
Route::post('/admin/login_admin', 'AdminController@doLogin');
Route::post('/admin/login_test', 'AdminController@test');
Route::get('/admin/login', 'AdminController@seeLogin');

Here the doLogin using post method:

public function doLogin(Request $request){

   $email = $request->input("email");
   $password = $request->input("password");

   $checkerInfo = AdminGetLoginChecker($email, $password);
   //if logged in
   if($checkerInfo){
       $request->session()->put('loggedIn', '1');
       $request->session()->put('userId', $checkerInfo);
       $request->session()->save();
       return response()->json([
           'success' => true,
           'message' => 'logged In'
       ]);
   }

   return response()->json([
       'success' => false,
       'message' => 'Incorrect User Email, Password'
   ]);

 }

Now problem is if request to access the session value in other get request section, then it doesn't allow to access.

public function dashboard(Request $request)
{

   $data = $request->session()->all();

   print_r($data);

   return view('welcome', ['name' => 'James']);
}

Here it doesn't show the saved session data, because it's a GET request.

Now if try to get request in any another method using POST request it'll show the saved data earlier in doLogin Method

//it's a POST requset
public function test(Request $request)
{
   $data = $request->session()->all();
   print_r($data);
}

How can I access the session data in GET requested methods?

Thanks in Advance,

Update:

Please note that add the url "'/admin/login_admin'" in excepts area of verifyCsrfToken middelwere to avoid csrf token issue while posting data

namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
   /**
   * The URIs that should be excluded from CSRF verification.
   *
   * @var array
   */
   protected $except = [
       //
       'url' => '/admin/login_admin'
   ];
} 
like image 485
FRabbi Avatar asked Nov 21 '16 04:11

FRabbi


1 Answers

Why are you trying to manually authenticate user?

For session operation use "Session" facade.

eg:

Session::set('your_session_key', 'Session Value');
Session::save();
$yourSessionKey = Session::get('your_session_key');
like image 168
Sohel Amin Avatar answered Oct 28 '22 18:10

Sohel Amin