question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't get logged user in 'protected' function

See original GitHub issue

Hi,

I’m trying to add some security when downloading files but I can’t get the user info inside the ‘protected’ function. My configuration looks like this:

Documents = new Meteor.Files({
  storagePath: "D:/temp",
  collectionName: 'documents',
  downloadRoute: '/doc',
  allowClientCode: true,
  chunkSize: 256*128,
  protected: (fileObj) => {
    console.log(fileObj);
    return true;
  }
});

The file is downloaded and I can get the info of the fileObj but I tried this.userId (undefined), this.user() -gives an TypeError… has no method ‘user’.

What am I doing wrong?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
danielruiz7commented, Nov 16, 2016

Hello @thomasspiesser, the following code is working for me now:

Documents = new Meteor.Files({
  storagePath: "/Users/danielruiz/dev/temp",
  collectionName: 'documents',
  downloadRoute: '/doc',
  onbeforeunloadMessage: 'Sure?',
  allowClientCode: true,
  chunkSize: 4096*128,
  protected: function(fileObj) {
    // console.log('protected js userId: ' + this.userId);
    if (!this.userId) {
      return 401;
    }
    return true;
  },
  downloadCallback: function(fileObj) {
    // console.log(fileObj);
    // console.log('downloadCallback js userId: ' + this.userId);
    // console.log('downloadCallback js user() ' + this.user());
    // return true;
    if (!this.userId || !fileObj)
      return 401;
    if (this.user()._id == fileObj.meta.owner_id) {
      return true;
    }
    return 401;
  }
});

Regards

1reaction
danielruiz7commented, Mar 15, 2016

Hello,

I finally solve the problem with the combination of protected and downloadCallback. This is the code:

...
  protected: function(fileObj) {
    console.log('protected js userId: ' + this.userId);
    if (!this.userId) {
      return 401;
    }
    return true;
  },
  downloadCallback: function(fileObj) {
    console.log(fileObj);
    console.log('js userId: ' + this.userId);
    console.log('js user() ' + this.user());

    if (!this.userId || !fileObj)
      return 401;
    if (this.user()._id == fileObj.meta.owner_id) {
      return true;
    }
    return 401;
  }
...

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

overridden authenticated method in Login Controller doesn't ...
That's because you are overwriting the login function, hence the authenticated function is never called. If you take a look at the trait:...
Read more >
I can't get the auth user. - Laracasts
use Illuminate\Auth\Guard; class MyController extends Controller { protected $currentUser; public function __construct(Guard $auth) { $this->currentUser ...
Read more >
Authentication - Laravel - The PHP Framework For Web Artisans
While handling an incoming request, you may access the authenticated user via the Auth facade's user method: use Illuminate\Support\Facades\Auth;. // Retrieve ...
Read more >
Auth::user() not working inside apply function in global scope ...
I was trying to create global scope dependent on a role of logged in user (typically, user with some acces rights based on...
Read more >
Manage Users in Firebase - Google
You create a new user in your Firebase project by calling the createUserWithEmailAndPassword method or by signing in a user for the first...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found