jbujalance / biwenger-transfers

A Biwenger transfer recorder that stores the market movements in a MongoDB database
0 stars 0 forks source link

No authorization token was found #13

Open Titogelo opened 5 years ago

Titogelo commented 5 years ago

First of all, thanks for such an interesting work.

I've been having a look at your work and I would like to give it a try, but I'm getting the following error.

http://localhost:8000/api/balances

{"message":"No authorization token was found"}

The error itself makes sense to me, but I would like to know how to overcome the problem.

I've generated via curl post the JWT token, and I added it to the .env file, however it doesn't seem to work so far.

Any suggestions?

Thanks in advance.

jbujalance commented 5 years ago

Hi @Titogelo , the documentation on this project is pretty terrible because I just did it for myself, but I'll be happy to help you if you want to give it a try.

First of all, just to make sure we are in the same page, there are two different token-related variables in the .env file:

The error you're getting is related to the authentication tokens. Actually, in order to consume the API (http://localhost:8000/api/balances for example), you must register before. The registration is needed because you can actually manage access roles for the different users in your database.

If you take a look at the file auth-controller.js, you'll see that when a user is registered, it is saved in the database and an authentication token is returned to the client (line 25). The same happens when hitting the login endpoint (line 44). The token is generated in user.js line 31, and it is signed with the JWT_SECRET from the .env file. So, the first thing you need is to generate a JWT secret (any string you want) and set it in the .env file. Once you have a JWT secret, you'll be able to register or login a user hitting the corresponding endpoint, and you'll get in the response the authorization token for that user.

This is how it looks like in real life: The login request: imagen And then the token in the response: imagen

This is the token that you are missing in your request. The token is needed to authenticate the logged user when requesting any of the API endpoints. It must be added to the requests in the header Authorization: imagen

Let me know if it isn't still clear.

Also, you may want to take a look at the front-end project: https://github.com/jbujalance/biwenger-companion-front Once again the documentation is quite poor, but you may want to dive in and adapt it for yourself. Mine runs on Heroku and allows things like these: imagen imagen

Titogelo commented 5 years ago

Hi @jbujalance, thanks for the detailed explanation. It's been really helpful.

What I have done:

Problems I have:

To sum up, I managed to login but I can't see any data. Am I missing anything?

Many thanks in advanced.

Titogelo commented 5 years ago

Hi @jbujalance,

I think the problem I have right now has to do with the permissions. I am getting the following back:

image

What do I need to do now?

Thanks in advanced.

jbujalance commented 5 years ago

Ok so rigth now it looks like almost everything is set up. You're missing just some steps :

Initialize the Biwenguer users table

First, let's import in your Mongo database the Biwenguer users of your league. In the file app/worker/users-initializer.js, you'll find a class UsersInitializer that will query the Biwenguer API to retrieve all the needed details of the users in your league. Then, it will create a collection in your database called biwenguerusers and store the users details there: imagen As you see the file app/worker/users-initializer.js is not executable, you'll need to write a little script creating an instance of UsersInitializer and calling the method UsersInitializer#initializeUsersCollection(). Just run it locally once and check that the collection has been succesfully created. It should work rigth out of the box, but I haven't check that class since the beginning of the season.

Populate the database with the Biwenguer data

As described in the README, the Biwenguer Transfers tracker is based on regular and incremental pollings of the Biwenguer API, instead of query the whole season data each time. The data is stored in the Mongo cluster, and from then, the aggregates are calculated and returned on each query. There are two main reasons to do this:

There are a few scripts in the bin folder that are in charge of polling the Biwenguer API to import the most recent market movements and bonuses. These scripts are meant to be plannified in a CRON task so that you can choose polling interval to the Biwenguer API. If you're using Heroku, you can schedule these scripts with the Heroku Scheduler (it's free).

As your database is empty, we'll need to run these scripts at least one time telling them to not just import the most recent data, but all the available data. For the transfers and bonuses recording, you can override the env variables BONUS_RETRIEVAL_LIMIT and TRANSFER_RETRIEVAL_LIMIT. These variables specify the number of entries to retrieve from the Biwenguer API at each poll. For example, if TRANSFER_RETRIEVAL_LIMIT=5, then the 5 most recent transfers will be queried everytime the bin/retrieve-transfers script is run. As you want to get all the data since the beginning of the season, just set a very big value for these two variables and run the corresponding scripts. However, for the standings synchronization, you're going to need to modify the code a little bit in order to get all the standings since the beggining of the season, as the number of queried standings is hard-coded. Basically, the simplest and ugliest way to do it is to go to the app/rest/biwenguer-client.js file and edit the 1 in line 94, method getLastFinishedRound(). Then run the script bin/retrieve-standings.

At the end you should have the following tables in your database: imagen

Grant roles to users

Finally, you'll need to grant some roles to the users you want. This is going to be totally manual, I did not make a GUI for the role management. Just edit the users documents in your users table like this: imagen

Additional info

There have been some recent modifications in the Biwenguer API:

I may have forgot something, so you tell me if it isn't clear.