Support the ongoing development of Laravel.io →
Security Requests Architecture

Hello,

i created a middleware that check if there is a cookie, if there isn't it create it and return the cookie with the response. inside my view i try to read the cookie but on the first load the cookie dosn't exist inside the view, but he actually exist inside the browser. only when i refresh the page the cookie exist inside the view himself, this is my code :

<?php

namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use Cookie;

class CheckUserAccountPlanCookie
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        
            if(!Auth::guard($guard)->guest()){
                
                if($request->hasCookie('a_p')) {
                    return $next($request);
                }
                else {
                    $AccountPlan = \App\User::GetUserSubscriptionType();
                            if(!$AccountPlan){
                                $user_diff = \App\User::IfUserTrial();
                                if($user_diff){
                                    $AccountPlan['plan_id'] = 'trial';
                                    $AccountPlan['days_left'] = $user_diff;
                                }
                                else {
                                    $AccountPlan['plan_id'] = false;
                                }
                            }
                    $response = $next($request);
                    return $response->withCookie(Cookie::make('a_p', $AccountPlan, 4));
                        
                }
                        
                    
            }
    }
        
}

Am im doing something wrong?

Last updated 3 years ago.
0
public function handle($request, Closure $next, $guard = null)
{

    if (!Auth::guard($guard)->guest()) {

        if ($this->hasCookie('a_p')) {
            return $next($request);
        } else {
            $AccountPlan = \App\User::GetUserSubscriptionType();
            if (!$AccountPlan) {
                $user_diff = \App\User::IfUserTrial();
                if ($user_diff) {
                    $AccountPlan['plan_id']   = 'trial';
                    $AccountPlan['days_left'] = $user_diff;
                } else {
                    $AccountPlan['plan_id'] = false;
                }
            }
            $this->makeMyCookie();
            return $next($request);
        }
    }
}

protected function makeMyCookie($AccountPlan)
{
    return Cookie::queue(Cookie::make('a_p', $AccountPlan, 129600));
}

protected function hasCookie($cookie_name)
{
     $cookie_exist = Cookie::get($cookie_name);
     return ($cookie_exist) ? true : false;
}
Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

maorkavod maorkavod Joined 12 Sep 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.