Red Devil
Red Devil

Reputation: 349

Where to place remote hooks (beforeRemote, afterRemote)code in yo loopback generated code

I'm using loopback generator to generate models and rest APIs service. Now I wanted to modify a rest api such that everytime the api is called, some spcific logging/actions are taken. I've come to know that by using remote hooks(beforeRemote, afterRemote), we can specify actions to be taken for different remote method calls. But what I don't know is that where to place the code of remote hooks. In which file this code will go when the project has been created using 'yo loopback'.

Upvotes: 0

Views: 730

Answers (2)

perseus
perseus

Reputation: 1381

If you want to protect the REST API from a non logged in user or anonymous user you should use ACL. Have a look here:

Define access control from the intermediate tutorial

Authentication, authorization, and permissions

The REST API will respond with codes if someone unauthorized tries to get access (depending on what you define), for example 401. Then in the app if you receive that code, you should redirect to the login.

Now, every time you create a new model with slc loopback:model, it will generate 2 files in the common/models folder. One is a .js and the ohter a .json. You have to add the code in the .js file for the remote hooks. Have a look to this link that explains how to add remote methods.

Upvotes: 1

snathan
snathan

Reputation: 535

You would add code to the files under /common/models.

If you are using a Person model. You would add the following code in /common/models/person.js:

Upvotes: 2

Related Questions