Adonis-Auth-Scaffold Development Update: Api/HTTP Client Authentication Code Generation

in utopian-io •  5 years ago  (edited)

Repository

https://github.com/creatrixity/adonis-auth-scaffold

Pull Request

https://github.com/creatrixity/adonis-auth-scaffold/pull/6

Api Auth Generator Image

Continuing with the development of the authentications scaffold for Adonis, this feature adds greater flexibility alongside a much more flexible means of getting solid, tested authentications system out of the box for your Adonis project.

Correspondence from adonis-auth-scaffold viewers indicated that a lot of correspondents have API clients as primary use cases for their Adonis apps and were not invested in the current feature stack. Crafting a custom solution for API client consumers while maintaining interchangeability with current implementations already in production was important.

To strike a proper balance, it was important to be able to properly customize the use case for the CLI generator. Solving this problem involved using the graciously provided Adonis Ace helpers. The code below helps with ensuring a prompt is provided if the user does not supply either the --api-only or --http-onlyrequired flags.

  async handle({}, {
    apiOnly,
    httpOnly
  }) {
    let client;

    if (!apiOnly && !httpOnly) {
      client = await this
        .choice('Will this be used for a REST Api or for a HTTP Client?', [
          {
            name: 'For REST Api',
            value: 'api'
          }, {
            name: 'For HTTP',
            value: 'http'
          }
        ])
    } else {
      client = apiOnly ? 'api': 'http';
    }

    // Rest of method...
}

To improve the user experience for the package, it is important to keep user effort to a minimum. To this end, it was important to provide a way of writing code to already existing Adonis files. The code below adds a new line of code after the first line of start/routes.js

      // Write a module require statement to the routes.js file.
      let routesFilePath = path.join(Helpers.appRoot(), 'start/routes.js');
      let generatedRoutesFilename = apiOnly ? 'apiAuthRoutes.js': 'authRoutes.js';

      await this._prependLineToFile({
        filename: routesFilePath,
        lineNumber: 2,
        lineContent: `require('./${generatedRoutesFilename}');`
      })

The prependLineToFile method is defined below. This method takes a fully qualified filename (file path and name), a line number to have the new code inserted and the content to be inserted.

  /**
   * Prepends a line of text to a provided file.
   *
   * @param {String} Object.filename - Fully qualified path of the file to be operated on.
   * @param {Number} Object.lineNumber - Line to operate on.
   * @param {String} Object.lineContent - Content to be prepended.
   *
   * @return {Void}
   */
  async _prependLineToFile ({
    filename,
    lineNumber,
    lineContent
  }) {
    let fileContents = await this.readFile(filename, 'utf-8');
    fileContents = fileContents.split("\n");

    if (fileContents[lineNumber] === lineContent) return;

    fileContents.splice(lineNumber, 0, `\n${lineContent}\n`)

    await this.writeFile(filename, fileContents.join('\n'));
  }

What's next?

  • Provide further code documentation for existing code.
  • Write improved documentation on current features.
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
  ·  5 years ago 

Thank you for your contribution.

You probably can use a single variable mode (which can be enum) to determine if it is API or REST, which will make the logics a lot simpler.

The copyAppStarterFiles appears to me requiring a parameter client, but on the code, it passes nothing, do this code work?

For the line append/prepend, have you considered the line endings on Linux, Windows or MAC? \n, \n\r or \r?

You may want to check if a file has been prepended using a better approach rather than if (fileContents[lineNumber] === lineContent) return; .

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Great insight. The v1.1.1 fix will address these issues.

Thank you for your review, @justyy! Keep up the good work!

Hey, @creatrixity!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Congratulations @creatrixity! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 18000 upvotes. Your next target is to reach 19000 upvotes.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

Carnival Challenge - Here are the winners
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Congratulations @creatrixity! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!
  ·  5 years ago Reveal Comment