meteorrestivusmeteor-restivus

Restivus authentication for Meteor methods


I'm trying to make my meteor-app for REST services available. For this I use the package "Restivus" which also works just fine. But once I'd like to run a meteor method this.userId is undefined.

Api.addRoute('addArticle', {authRequired: true}, {
        post: function () {
            console.log(this.userId); //<-- hwuqtFXf8aKperJ5p
            try {
                Meteor.call("addArticle",this.bodyParams);
            } catch (e) {
                return {code:500,type:e.error,reason:e.reason};
            }
        }
    });

the method:

new ValidatedMethod({
    name: 'addArticle',
....
if (!this.userId) {
        throw new Meteor.Error(...); //is thrown
    }

What am I doing wrong?


Solution

  • In Meteor methods you get the current userId by doing

        Meteor.userId() 
    

    and not

        this.userId
    

    So you would need to update your code to

        if(!Meteor.userId()){ 
            throw new Meteor.Error(403, '403:Forbidden', 'You shall not pass!')
        }