initializeApi($request); if ($response !== null) return $response; $this->setRequest($request); $path = trim($request->path(), '/'); $parts = explode('/', $path); $action = $parts[array_key_last($parts)] ?? ''; $needLogin = !action_in_arr($this->noNeedLogin, $action); try { $this->auth = Auth::instance(); $token = get_auth_token(['ba', 'user', 'token'], $request); if ($token) $this->auth->init($token); } catch (TokenExpirationException) { if ($needLogin) return $this->error(__('Token expiration'), [], 409); } if ($needLogin) { if (!$this->auth->isLogin()) { return $this->error(__('Please login first'), ['type' => Auth::NEED_LOGIN], Auth::LOGIN_RESPONSE_CODE); } if (!action_in_arr($this->noNeedPermission, $action)) { $routePath = get_controller_path($request) . '/' . $action; if (!$this->auth->check($routePath)) { return $this->error(__('You have no permission'), [], 401); } } } event_trigger('frontendInit', $this->auth); return null; } }