Coder Social home page Coder Social logo

Comments (3)

grantg182 avatar grantg182 commented on August 22, 2024

Upon further investigation, it appears this is broken from Joomla 3.8 onwards.

In my custom plugin, I have the following basic structure:- /plugins/api/customapi/custom.php:-

class plgAPICustomApi extends ApiPlugin
{
        public function __construct(&$subject, $config = array())
        {
                parent::__construct($subject, $config = array());

                //load helper file
                require_once JPATH_SITE.'/plugins/api/customapi/playlists/helper/simpleschema.php';

                ApiResource::addIncludePath(dirname(__FILE__).'/customapi');

                // Set resources & access
                $this->setResourceAccess('custom', 'public', 'get');
        }
}

/plugins/api/customapi/custom.php:-

class CustomApiApiResourceCustom extends ApiResource
{

        /**
         * get Method to get custom object
         *
         * @return  JSON Object
         *
         * @since  1.0
         */
        public function get()
        {
                $this->plugin->setResponse($this->getCustomObject());
        }
        public function getCustomObject() 
        {
                // my code to get article data and process 3rd party apps.
        }
}

My getCustomObject() function gets some data from the current viewed article. I am calling the custom API from an AJAX request.

I have since discovered that if I create an API key and hardcode this as an Authentication: Bearer header in the AJAX request, that I remain logged in. However, this is of no use - I literally do not care about authentication, whether the user is logged in or not, hence setResourceAccess is set to public.

What is the best way forward? Some, not all, of my "users" will be logged in, but I need the api to return the same data irrespective if they are logged in or not - so do not need nor wish to have to create an API token on the fly for each "user".

from com_api.

coolbung avatar coolbung commented on August 22, 2024

If you are sure that you will be using the API calls on the same Joomal site where the user is logged in, then you can set the X-Auth header to session.

However, usually if it is the same site, you can just create a controller.json.php instead of creating an API plugin.

from com_api.

coolbung avatar coolbung commented on August 22, 2024

Closing this, please reopen in case you continue to face issues even with the approach suggested above.

from com_api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.