None of the below methods is setting cookie. i tried defining the route in Routes/api.php
also.And in case of session,dd(session()->get('access_token'));
inside the session_login()
method immaediately after setting session gives data.But in index()
method, it gives null data. I am able to use the Symfony\Component\HttpFoundation\Cookie
facade only, when using the direct facade use cookie;
it shows error undefined type
.So, i couldn't try the cookie::queue()
similarly for session also, not able to try with use session
facade. that's why i've commented it in the below code.How can i fix this? i've already gone through many links in internet. Any help is much appreciated.
controller
use Symfony\Component\HttpFoundation\Cookie;
//use Cookie;
use Illuminate\Support\Facades\Session;
//use Session;
class AuthController extends Controller
{
public function getCookie(Request $request){
$value = $request->cookie('access_token');
return $value;
}
public function login(Request $request)
{
$response = new Response();
$response->withCookie(cookie('access_token','abcd',60));
//Cookie::queue('access_token','abcd',60);
//Cookie::queue(Cookie::make('access_token','abcd',60)); --shows error Call to undefined method Symfony\Component\HttpFoundation\Cookie::queue()
return redirect('/getCookie');
}
public function session_login(Request $request)
{
session(['access_token'=>$token]);
return redirect('/');
}
public function index()
{
return session()->get('access_token');
}
}
Routes/web.php
Route::post('/signin', [AuthController::class,'login']);
Route::get('/getCookie',[AuthController::class,'getCookie']);
Route::get('/',[AuthController::class,'index']);
Config/session.php
<?php
use Illuminate\Support\Str;
return [
'driver' => env('SESSION_DRIVER', 'file'),
'lifetime' => env('SESSION_LIFETIME', 120),
'expire_on_close' => false,
'encrypt' => false,
'files' => storage_path('framework/sessions'),
'connection' => env('SESSION_CONNECTION', null),
'table' => 'sessions',
'store' => env('SESSION_STORE', null),
'lottery' => [2, 100],
'cookie' => env(
'SESSION_COOKIE',
Str::slug(env('APP_NAME', 'laravel'), '_').'_session'
),
'path' => '/',
'domain' => env('SESSION_DOMAIN', null),
'secure' => env('SESSION_SECURE_COOKIE'), //also tried adding false to this
'http_only' => true,
'same_site' => 'lax',
];
.env file
BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file //tried changing this to database also
SESSION_LIFETIME=120
SESSION_DOMAIN=mydomain.com
SESSION_SECURE_COOKIE=false