legacy-docs icon indicating copy to clipboard operation
legacy-docs copied to clipboard

Extending Providers example error

Open HigoRibeiro opened this issue 5 years ago • 1 comments

Today I was asked to help someone in need of authentication with Ad (Active Directory) and I indicated the AdonisJS documentation (Extending Providers) that shows how to do this and I came across what I considered an error:

  • In the example it shows that we should use hooks.after, but I understand that hooks.before should be used, because when the manager is called to get his extensions it is empty ([]). Once the extensions are called after the Providers are registered.
  • In the example it is shown how to extend the Adonis/Src/Session provider, but this provider does not exist and to extend an authentication provider is slightly different, as the example below.

Extend provider Auth with new scheme

ioc.extend(
    "Adonis/Src/Auth",
    "ad",
    function() {
      return class ADScheme {},
    "scheme"
  );

Thank you

HigoRibeiro avatar Mar 25 '19 19:03 HigoRibeiro

I would like to extend on this problem with a more concrete example with core code

In the documentation (Extending providers) it sohuld be noted that 'Adonis/Src/Session' is actually going to call Manager from session

If we go to Manager.js in Session, or in my case Auth we get this:

extend (key, implementation, type) {

and if I call this in start/hooks.js file like so (Test is imported at the top and is being initialized here)

hooks.after.providersRegistered(() => { ioc.extend('Adonis/Src/Auth', 'test', function () { return new Test(); }) })

it doesn't work because of two reasons

  1. I need to put hooks.before or I get error saying that my test serializer needs to be defined
  2. There is a fourth parameter missing that will specify that we are creating a serializer

And so the proper code looks like this

hooks.before.providersRegistered(() => { ioc.extend('Adonis/Src/Auth', 'test', function () { return new Test(); },'serializer') })

and now it works.

Another problem is in Ioc.extend documentation in code where it doesn't state that there need to be four paramaters, but only 3 (and this is also true for website docs)

backspacerhino avatar Sep 23 '19 16:09 backspacerhino