Ved21212's avatar

Facing issue while logout

I have 2 guards seller and branch when ever I logout as seller I get logged out from branch simultaneously how can I end session for one guard?

    protected $redirectTo = '/sellerbranch/home';
    public function __construct()
    {
        $this->middleware('guest:sellerbranch')->except('logout');
    }
    public function showLoginForm()
    {
        return view('sellerbranch.auth.login');
    }

    public function login(Request $request)
    {
        
        $abc = $this->credentials($request);
        $xyz['branch_email'] = $abc['email'];
        $xyz['password'] = $abc['password'];
        if ($this->guard()->validate($xyz)) {
            $user = $this->guard()->getLastAttempted();

            if ($this->attemptLogin($request)) {
                return $this->sendLoginResponse($request);
            } else {
                $this->incrementLoginAttempts($request);
                return redirect('seller_login')->with('loginWaring', 'You have register successfully. Please contact admin');
            }
        }

        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }

        
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }


    public function edit($id)
    {
        $order = array();

        $user_id = Auth::guard('web_seller')->user()->id;

        $sellerPermisionList = DB::table("tbl_seller_permission")
            ->select('*')
            ->where('user_id','=',$user_id)
            ->get();

        $seller_id = $sellerPermisionList['0']->seller_id;

        $order = DB::table("tbl_order as o")
            ->select('o.*','uc.user_type','o.id as oid','uc.name as cust_name','uc.email as cust_email','uc.mobile_no as cust_mobile_no')
            ->leftJoin('users AS uc', 'uc.id', '=', 'o.user_id')
            ->where('o.id', '=', $id)
            ->orderBy('o.updated_at', 'desc')
            ->get();

        //dd($order);
        foreach ($order as $key => $value) {
            $id = $value->id;

            $CheckCouponOrder = DB::table("tbl_coupon_history")
                ->select('id')
                ->where('order_id', '=', $id)
                ->get();

            if(sizeof($CheckCouponOrder) != 0){

            }else{


            }

            $ServiceBookingDetailsArray = DB::table("tbl_order_details as od")
                ->select('os.name as order_status_name','s.name as seller_name','od.*','p.product_name',DB::raw("(SELECT coupon_price FROM tbl_coupon_history WHERE tbl_coupon_history.product_id = od.product_id) as coupon_price"))
                ->leftJoin('products AS p', 'p.id', '=', 'od.product_id')
                ->leftJoin('tbl_order_status AS os', 'os.id', '=', 'od.order_status_id')
                ->leftJoin('sellers AS s', 's.id', '=', 'od.product_seller_id')
                ->where('od.product_seller_id', '=', $seller_id)
                ->where('od.order_booking_id', '=', $id)
                ->get();

            $order[$key]->product_item_count = sizeof($ServiceBookingDetailsArray);


            $order[$key]->product_details = $ServiceBookingDetailsArray;
            //dd($ServiceBookingDetailsArray);

            foreach ($ServiceBookingDetailsArray as $keys => $vals) {

                //BN
                if($vals->branch_id != 0){
                    $sellerbranchArray = SellerBranch::findOrFail($vals->branch_id);
                    $branch_name = $sellerbranchArray->branch_name;
                }else{
                    $branch_name = "Assign";
                }

                $order[$key]->product_details[$keys]->branch_name = $branch_name;

                //BN


                $option_value = $vals->option_value;
                $order_quantity = $vals->order_quantity;
                $product_id = $vals->product_id;
                $product_price = $vals->product_price;
                $special_price = $vals->product_sp_price;
                //Rating Code
                if($id != ''){
                    $checkRatingUserArray = DB::table('tbl_ratings')
                        ->select('rating_count')
                        ->where('product_id', '=', $product_id)
                        ->where('user_id', '=', $id)
                        ->orderBy('created_at', 'asc')
                        ->limit(1)
                        ->get();

                    if(sizeof($checkRatingUserArray) != 0){
                        $order[$key]->product_details[$keys]->can_rated = 1;
                        $order[$key]->product_details[$keys]->user_rating = $checkRatingUserArray[0]->rating_count;
                    }else{
                        $order[$key]->product_details[$keys]->can_rated = 0;
                        $order[$key]->product_details[$keys]->user_rating = 0;
                    }

                    $checkRatedAvg = DB::table('tbl_ratings as r')
                        ->select(DB::raw('round(avg(r.rating_count)) AS avg_rate'),DB::raw('count(r.id) AS user_count'))
                        ->where('r.product_id', '=', $product_id)
                        ->get();

                    if(sizeof($checkRatedAvg) != 0){
                        $avg_rate = $checkRatedAvg[0]->avg_rate;
                        $user_count = $checkRatedAvg[0]->user_count;

                        $order[$key]->product_details[$keys]->avg_rate = $avg_rate;
                        $order[$key]->product_details[$keys]->total_user_count = $user_count;
                    }else{
                        $order[$key]->product_details[$keys]->avg_rate = 0;
                        $order[$key]->product_details[$keys]->total_user_count = 0;
                    }

                }else{
                    $order[$key]->product_details[$keys]->can_rated = 0;
                    $order[$key]->product_details[$keys]->avg_rate = 0;
                    $order[$key]->product_details[$keys]->total_user_count = 0;
                    $order[$key]->product_details[$keys]->user_rating = 0;
                }

                $newOptValname = array();
                $total_sp_option = 0;
                $total_option = 0;
                if ($option_value != "[]") {
                    $arrayOpDecode = json_decode($option_value);

                    foreach ($arrayOpDecode as $opDecodeVal) {

                        $product_option_value_id = $opDecodeVal->product_option_id;

                        $ProdOptionArray = DB::table("tbl_product_option_value")
                            ->where('id', '=', $product_option_value_id)
                            ->get();

                        $option_id = $ProdOptionArray[0]->option_id;
                        $option_value_id = $ProdOptionArray[0]->option_value_id;
                        $option_price = $ProdOptionArray[0]->price;
                        $option_price_prefix = $ProdOptionArray[0]->price_prefix;

                        if ($option_price_prefix == "+") {
                            $product_price_new = ($special_price + $option_price);
                            $special_price_new = ($special_price + $option_price);
                            $product_price_with_option = "+ " . $option_price;
                        } else {
                            $product_price_new = ($special_price - $option_price);
                            $special_price_new = ($special_price - $option_price);
                            $product_price_with_option = " -" . $option_price;
                        }

                        $total_option += $product_price_new;
                        $total_sp_option += $special_price_new;

                        $OptionDataArray = DB::table("tbl_option_description")
                            ->where('id', '=', $option_id)
                            ->get();
                        $option_Name = $OptionDataArray[0]->option_name;

                        $OptionDatanewArray = DB::table("tbl_option_value_description")
                            ->where('id', '=', $option_value_id)
                            ->get();
                        $option_value_Name = $OptionDatanewArray[0]->option_value_name;

                        $newOptValname[] = array(
                            'Service_Option_Value' => $option_Name,
                            'Service_Option_Value_Name' => $option_value_Name,
                            'product_price' => $special_price,
                            'special_price' => $special_price_new,
                            'option_price' => $option_price,
                            'product_price_with_option' => $product_price_with_option
                        );
                    }

                    //New Code Option Start
                    $Prev_Service_Option_Value_New = "";
                    $newOptValnameOld = array();
                    $newOptValnameVal = array();
                    foreach ($newOptValname as $keys1 => $vals) {
                        $Service_Option_Value_New = $vals['Service_Option_Value'];
                        $Service_Option_Value_Name_New = $vals['Service_Option_Value_Name'];
                        $product_price_New = $vals['product_price'];
                        $special_price_New = $vals['special_price'];
                        $product_price_with_option = $vals['product_price_with_option'];
                        $option_price_without_prefix = $vals['option_price'];

                        if ($Prev_Service_Option_Value_New == $Service_Option_Value_New) {
                        } else {
                            $newOptValnameOld = array();
                        }

                        $newOptValnameOld[] = array(
                            'Service_Option_Value_Name' => $Service_Option_Value_Name_New,
                            'product_price' => $product_price_New,
                            'special_price' => $special_price_New,
                            'product_price_with_option' => $product_price_with_option,
                            'option_price_without_prefix' => $option_price_without_prefix
                        );

                        $newOptValnameVal[$Service_Option_Value_New] = array(
                            'Service_Option_Value' => $Service_Option_Value_New,
                            'Service_Option_selected' => $newOptValnameOld
                        );

                        $Prev_Service_Option_Value_New = $Service_Option_Value_New;
                    }
                    $newOptValnameOld1 = array();
                    foreach ($newOptValnameVal as $keys2 => $vals) {
                        $newOptValnameOld1[] = $vals;
                    }

                    $order[$key]->product_details[$keys]->product_option_data = $newOptValnameOld1;

                } else {
                    $order[$key]->product_details[$keys]->product_option_data = array();

                }
            }
        }

        $orderStatusArray = DB::table("tbl_order_status")
            ->select('id','name')
            ->orderBy('id', 'asc')
            ->get();

        $order[0]->order_status_array = $orderStatusArray;

        //BB
        $seller_branch_array = SellerBranch::where('seller_id', '=', $seller_id)->get();

        $order[0]->seller_branch_array = $seller_branch_array;
        //BB

        $orderHistoryArray = DB::table("tbl_order_history as oh")
            ->select('oh.notify','oh.comment','oh.date_added','os.name')
            ->leftJoin('tbl_order_status AS os', 'os.id', '=', 'oh.order_status_id')
            ->where('oh.order_id', '=', $id)
            ->orderBy('oh.date_added', 'desc')
            ->get();

        $order[0]->order_history = $orderHistoryArray;
        $couponHistoryArray = DB::table("tbl_coupon_history")
            ->select('*')
            ->where('order_id', '=', $id)
            ->orderBy('id', 'asc')
            ->get()
            ->sum('coupon_price');

        $get_coupon_id = CouponHistory::where('order_id', '=', $id)->first();
        $order[0]->coupon_total = $couponHistoryArray;
        if(!empty($get_coupon_id)){
            $order_coupon = Coupon::where('id',$get_coupon_id->coupon_id)->first();
            $order[0]->code = $order_coupon->code;
        }else{
            $order[0]->code = '';
        }
        $giftHistoryArray = DB::table("tbl_gift_card_history")
            ->select('*')
            ->where('order_id', '=', $id)
            ->orderBy('id', 'asc')
            ->get()
            ->sum('gift_amount');

        $order[0]->gift_total = $giftHistoryArray;

        //BBB
        $order[0]->seller_id = $seller_id;

        $CheckInvoiceOrder = InvoiceOrder::where('order_id', '=', $id)->where('seller_id', '=', $seller_id)->get();

        if(sizeof($CheckInvoiceOrder) == 0){
            $order_invoice_id = "";
        }else{
            $order_invoice_id = $CheckInvoiceOrder[0]->invoice_no;
        }
        $order[0]->invoice_no = $order_invoice_id;

        $get_payment_status = PaymentDetails::where('order_id', '=', $id)->get();
        $order[0]->payment_status = $get_payment_status;
        $order = $order[0];
        return view('seller.order.edit', compact(['order']));
    }

    protected function attemptLogin(Request $request)
    {
        $abc = $this->credentials($request);
        $xyz['branch_email'] = $abc['email'];
        $xyz['password'] = $abc['password'];
        return $this->guard()->attempt(
            $xyz, $request->filled('remember')
        );
    }

    protected function credentials(Request $request)
    {
        return $request->only('email', 'password');
    }

    protected function sendLoginResponse(Request $request)
    {
        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        return $this->authenticated($request, $this->guard()->user())
                ?: redirect()->intended($this->redirectPath());
    }



    /**
     * Log the user out of the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function logout(Request $request)
    {
        $sessionKey = $this->guard()->getName();
        $this->guard('sellerbranch')->logout();

        $request->session()->invalidate();

        return redirect('sellerbranch/login');
    }


    protected function guard()
    {
        return Auth::guard('sellerbranch');
    }


0 likes
4 replies
bobbybouwmann's avatar

Well, the problem is that Laravel flushes the session and that also deletes the session from the server.

I believe you can remove the following line and it should work again

$request->session()->invalidate();

However, this might give you weird issues because some sessions might never be destroyed which can be a security concern at some point.

Normally when you have multiple guards, you have one with a state like the default session guard and one without a state, for example, an API token.

Another solution is writing your own logout method of course ;)

Ved21212's avatar

@bobbybouwmann Is it somehow possible to invalidate session only for a single guard? Can I somehow customize it ?

bobbybouwmann's avatar

Not that I know of. But you can dive into the code of course ;)

Please or to participate in this conversation.