Create a boot script to run code at the start of a LoopBack API

Bram Borggreve
InstructorBram Borggreve

Share this video with your friends

Send Tweet
Published 5 years ago
Updated 3 years ago

In this lesson we will learn how to create a boot script.

We will use a boot script to create or update a predefined admin user, and give that user an Access Token. That way we don't have to log in to the API each time we want to use it as an authenticated user.

[00:00] In order to access the API as an authenticated user, we need to have an access token. We can create an access token by logging into the API. Because we don't have a user yet, we first need to create one. In the API Explorer, we go to the post/users endpoint.

[00:15] In the data field, we enter a JSON object that has an email and password property. When we hit Try It Out, we see that the user got created.

[00:23] Now, if we scroll all the way down, we see that there's a /user/login endpoint. When we enter the same credentials and we hit Try It Out, we see that we got a valid response. The actual response body of the API is our access token that got created. We can use the ID fields of these results to identify ourselves.

[00:42] We copy the ID and paste it in the box in the top right corner. We can now make authenticated requests to the API. When we go to the /post/categories endpoints and we try to add a new category, we see that this works as expected.

[00:59] If we remove the access token and we try it again, we see that we get an authorization required error message.

[01:06] To have our API create an access token on start, we can use a boot script. In our project, we run LB bootscript and as a name, we enter createAccessToken. As a type, we select async. When we open our newly created script, we remove the code that got generated.

[01:23] The first things we want to include is a reference to the access token and the user model. We set three variables -- email, password, and the access token that we want to have created. We start our promise chain by returning promise.resolve.

[01:38] In the first then() block, we try to find the user by its email address. In the second then() block, we return the user if it got found, and otherwise, we create it using user.create. In the third then() block, we will create an access token using the variable accessToken that we set above, and the ID of the user that we got passed in as the user ID.

[01:58] We then console log the ID of the generated access token to the terminal. We finalize the chain by using Bluebird's ask callback CB method to return CB. When we start our server, we see that there is an error message, "Ask callback is not a function."

[02:16] To fix this, we go to the top of our boot script and require Bluebird. Because Bluebird is shipped with LoopBack 3.0we don't need to explicitly install it as a dependency. We see that our server now actually starts and that our creative access token got printed in the terminal.

[02:31] When we go to the API Explorer, we can verify that we can use this token to authenticate.

~ 5 years ago

Hi, tnx for great tutorial! I have one question, after I require bluebird I'm getting a new error:

Unhandled rejection TypeError: cb is not a function at /Projects/playground/egghead/loopback-tutorial/node_modules/loopback-boot/lib/executor.js:318:34 at tryCatcher (/Projects/playground/egghead/loopback-tutorial/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/Projects/playground/egghead/loopback-tutorial/node_modules/bluebird/js/release/promise.js:512:31) at Promise._settlePromise (/Projects/playground/egghead/loopback-tutorial/node_modules/bluebird/js/release/promise.js:569:18) at Promise._fulfillPromises (/Projects/playground/egghead/loopback-tutorial/node_modules/bluebird/js/release/promise.js:668:14) at Promise._settlePromises (/Projects/playground/egghead/loopback-tutorial/node_modules/bluebird/js/release/promise.js:694:18) at Async._drainQueue (/Projects/playground/egghead/loopback-tutorial/node_modules/bluebird/js/release/async.js:133:16) at Async._drainQueues (/Projects/playground/egghead/loopback-tutorial/node_modules/bluebird/js/release/async.js:143:10) at Immediate.Async.drainQueues (/Projects/playground/egghead/loopback-tutorial/node_modules/bluebird/js/release/async.js:17:14) at runCallback (timers.js:637:20) at tryOnImmediate (timers.js:610:5) at processImmediate [as _immediateCallback] (timers.js:582:5)

Somebody maybe know how to solve this? Tnx :)

Sergii Shvager
Sergii Shvager
~ 2 years ago

Hi, I've replaced .asCallback with regular .then and it worked. This way I don't need to require bluebird