<?php

namespace Modules\Mall\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Jason\Api\Api;
use Modules\Mall\Models\Shop;

class Authenticate
{

    /**
     * Notes   :
     * @Date   : 2021/5/7 9:38 上午
     * @Author : <Jason.C>
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure                  $next
     * @return mixed
     * @throws \Exception
     */
    public function handle(Request $request, Closure $next)
    {
        $shop = Shop::byUser(Api::user())->first();

        if ($shop) {
            $request->merge(['shop' => $shop]);
        } else {
            throw new \Exception('Do not have a shop');
        }

        return $next($request);
    }

}