Stremio / stremio-addons

OBSOLETE; GO TO https://github.com/stremio/stremio-addon-sdk
144 stars 45 forks source link

Pass request object as argument to middleware methods #7

Open altaywtf opened 6 years ago

altaywtf commented 6 years ago

Hello there Stremio team, I'm currently playing with your addon-sdk and I needed to implement some kind of authentication logic into it.

In my addon, I have an auth middleware as the initial middleware which handles the user recognition/authentication logic and adds the user object into the req object (like passport does) and makes it available to further parts of the chain.

Unfortunately, req object is consumed as input but it's not available at the output in your addon methods. In order to achieve this, I've done a little trick and it provides me the solution that I need.

I hope I made my problem clear and if you think that's a considerable one, I'd be glad to open a PR for it.

Ivshti commented 6 years ago

We are currently re-working the add-on system (see https://github.com/Stremio/stremio-addon-sdk and https://github.com/Stremio/stremio-addon-client - would also appreciate your opinion on those)

So we are wondering how to properly implement auth in the new system.

The quick and dirty way, which would also work for your case, without patching, is to just have a user-specific add-on URL generated after web-based authentication: e.g. my-addon.com/AUTHTOKEN/stremio/v1. This is not ideal because it keeps the auth token in a GET url, but is proven to work.

The second solution we're thinking of is to allow setting custom headers on the response. Would this be good enough?

altaywtf commented 6 years ago

Hello @Ivshti , thank you so much for the answer.

I'm actually doing the exact myaddon.com/AUTHTOKEN/ trick that you've pointed out but how can I access the URL and extract token inside stremio.find method, without having the request object.

My flow is similar to this:

I'm using auth middleware since I have some other endpoints that depends on that req.user object. However, if I only had stremio middleware, I'd try something like this:

Before this I've actually tried session authentication but as you've mentioned, client does not use custom headers and I think it'd be great if you guys could enable that one. 🙏

I'll definitely check out those new sdk's.