create account

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

View this thread on: hive.blogpeakd.comecency.com
· @creatrixity · (edited)
$35.66
Adonis-Auth-Scaffold Development Update: Api/HTTP Client Authentication Code Generation
## Repository

[https://github.com/creatrixity/adonis-auth-scaffold](https://github.com/creatrixity/adonis-auth-scaffold)

## Pull Request

[https://github.com/creatrixity/adonis-auth-scaffold/pull/6](https://github.com/creatrixity/adonis-auth-scaffold/pull/6)

![Api Auth Generator Image](https://user-images.githubusercontent.com/5021686/54365412-10485200-466f-11e9-8fb5-cbaa920c6950.gif)

Continuing with the development of the [authentications scaffold for Adonis](https://github.com/creatrixity/adonis-auth-scaffold), 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-only`required flags.

```js
  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`

```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.

```js
  /**
   * 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.
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 59 others
properties (23)
authorcreatrixity
permlinkadonis-auth-scaffold-development-update-api-http-client-authentication-code-generation
categoryutopian-io
json_metadata{"tags":["utopian-io","development","programming","node-js","technology"],"image":["https://user-images.githubusercontent.com/5021686/54365412-10485200-466f-11e9-8fb5-cbaa920c6950.gif"],"links":["https://github.com/creatrixity/adonis-auth-scaffold","https://github.com/creatrixity/adonis-auth-scaffold/pull/6"],"app":"steemit/0.1","format":"markdown"}
created2019-03-14 20:00:12
last_update2019-03-14 20:12:12
depth0
children8
last_payout2019-03-21 20:00:12
cashout_time1969-12-31 23:59:59
total_payout_value27.108 HBD
curator_payout_value8.547 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,552
author_reputation16,463,772,859,893
root_title"Adonis-Auth-Scaffold Development Update: Api/HTTP Client Authentication Code Generation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,317,387
net_rshares52,344,066,852,180
author_curate_reward""
vote details (123)
@hivebuzz ·
Congratulations @creatrixity! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@creatrixity/payout.png?202101182135"></td><td>You received more than 2000 as payout for your posts. Your next target is to reach a total payout of 3000</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@creatrixity) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>

properties (22)
authorhivebuzz
permlinkhivebuzz-notify-creatrixity-20210119t040241000z
categoryutopian-io
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2021-01-19 04:02:39
last_update2021-01-19 04:02:39
depth1
children0
last_payout2021-01-26 04:02:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length660
author_reputation370,789,806,165,141
root_title"Adonis-Auth-Scaffold Development Update: Api/HTTP Client Authentication Code Generation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id101,441,917
net_rshares0
@justyy ·
$10.21
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](https://github.com/creatrixity/adonis-auth-scaffold/blob/1c098fba002f3354842fb2067412b98a4fc0b8b1/commands/MakeAuth.js#L311) 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](https://join.utopian.io/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](https://review.utopian.io/result/3/2-3-2-1-2-2-1-).

---- 
Need help? Chat with us on [Discord](https://discord.gg/uTyJkNm).

[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorjustyy
permlinkre-creatrixity-adonis-auth-scaffold-development-update-api-http-client-authentication-code-generation-20190314t212110987z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://github.com/creatrixity/adonis-auth-scaffold/blob/1c098fba002f3354842fb2067412b98a4fc0b8b1/commands/MakeAuth.js#L311","https://join.utopian.io/guidelines","https://review.utopian.io/result/3/2-3-2-1-2-2-1-","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2019-03-14 21:21:15
last_update2019-03-14 21:21:15
depth1
children2
last_payout2019-03-21 21:21:15
cashout_time1969-12-31 23:59:59
total_payout_value7.758 HBD
curator_payout_value2.450 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,139
author_reputation280,616,224,641,976
root_title"Adonis-Auth-Scaffold Development Update: Api/HTTP Client Authentication Code Generation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,320,349
net_rshares14,996,329,284,408
author_curate_reward""
vote details (23)
@creatrixity ·
Great insight. The v1.1.1 fix will address these issues.
properties (22)
authorcreatrixity
permlinkre-justyy-re-creatrixity-adonis-auth-scaffold-development-update-api-http-client-authentication-code-generation-20190315t061618935z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2019-03-15 06:20:15
last_update2019-03-15 06:20:15
depth2
children0
last_payout2019-03-22 06:20:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length56
author_reputation16,463,772,859,893
root_title"Adonis-Auth-Scaffold Development Update: Api/HTTP Client Authentication Code Generation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,341,617
net_rshares0
@utopian-io ·
Thank you for your review, @justyy! Keep up the good work!
properties (22)
authorutopian-io
permlinkre-re-creatrixity-adonis-auth-scaffold-development-update-api-http-client-authentication-code-generation-20190314t212110987z-20190317t040239z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-03-17 04:02:39
last_update2019-03-17 04:02:39
depth2
children0
last_payout2019-03-24 04:02:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length58
author_reputation152,955,367,999,756
root_title"Adonis-Auth-Scaffold Development Update: Api/HTTP Client Authentication Code Generation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,442,817
net_rshares0
@steem-ua ·
#### Hi @creatrixity!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
**Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
πŸ‘  
πŸ‘Ž  , , , , , , , , , ,
properties (23)
authorsteem-ua
permlinkre-adonis-auth-scaffold-development-update-api-http-client-authentication-code-generation-20190315t045116z
categoryutopian-io
json_metadata"{"app": "beem/0.20.18"}"
created2019-03-15 04:51:18
last_update2019-03-15 04:51:18
depth1
children0
last_payout2019-03-22 04:51:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length290
author_reputation23,214,230,978,060
root_title"Adonis-Auth-Scaffold Development Update: Api/HTTP Client Authentication Code Generation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,338,308
net_rshares-224,729,089,507
author_curate_reward""
vote details (12)
@steemitboard ·
Congratulations @creatrixity! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

<table><tr><td>https://steemitimages.com/60x70/http://steemitboard.com/@creatrixity/votes.png?201903200209</td><td>You made more than 18000 upvotes. Your next target is to reach 19000 upvotes.</td></tr>
</table>

<sub>_You can view [your badges on your Steem Board](https://steemitboard.com/@creatrixity) and compare to others on the [Steem Ranking](http://steemitboard.com/ranking/index.php?name=creatrixity)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Do not miss the last post from @steemitboard:**
<table><tr><td><a href="https://steemit.com/carnival/@steemitboard/carnival-challenge-here-are-the-winners"><img src="https://steemitimages.com/64x128/http://i.cubeupload.com/rltzHT.png"></a></td><td><a href="https://steemit.com/carnival/@steemitboard/carnival-challenge-here-are-the-winners">Carnival Challenge - Here are the winners</a></td></tr></table>

###### [Vote for @Steemitboard as a witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1) to get one more award and increased upvotes!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-creatrixity-20190320t025345000z
categoryutopian-io
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2019-03-20 02:53:45
last_update2019-03-20 02:53:45
depth1
children0
last_payout2019-03-27 02:53:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,250
author_reputation38,975,615,169,260
root_title"Adonis-Auth-Scaffold Development Update: Api/HTTP Client Authentication Code Generation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,603,082
net_rshares0
@steemitboard ·
Congratulations @creatrixity! You received a personal award!

<table><tr><td>https://steemitimages.com/70x70/http://steemitboard.com/@creatrixity/birthday2.png</td><td>Happy Birthday! - You are on the Steem blockchain for 2 years!</td></tr></table>

<sub>_You can view [your badges on your Steem Board](https://steemitboard.com/@creatrixity) and compare to others on the [Steem Ranking](https://steemitboard.com/ranking/index.php?name=creatrixity)_</sub>


###### [Vote for @Steemitboard as a witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1) to get one more award and increased upvotes!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-creatrixity-20191207t223741000z
categoryutopian-io
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2019-12-07 22:37:42
last_update2019-12-07 22:37:42
depth1
children0
last_payout2019-12-14 22:37:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length632
author_reputation38,975,615,169,260
root_title"Adonis-Auth-Scaffold Development Update: Api/HTTP Client Authentication Code Generation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id93,205,981
net_rshares0
@utopian-io ·
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](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

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

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-adonis-auth-scaffold-development-update-api-http-client-authentication-code-generation-20190316t050000z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-03-16 05:00:06
last_update2019-03-16 05:00:06
depth1
children0
last_payout2019-03-23 05:00:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length593
author_reputation152,955,367,999,756
root_title"Adonis-Auth-Scaffold Development Update: Api/HTTP Client Authentication Code Generation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,392,404
net_rshares0