Forced login in Meteor.method?

Is it possible in some ways to force a login inside a method? I have cron job that needs to run as a user. For the cron, I use percolate:synced-cron.

Thanks,

Hi, as your cron runs independently of a user in the server I don’t think it makes sense to authenticate someone there.

You probably should fetch a user using another criteria and not the logged user.

Usually the job that you are running is related to a user, especially if you need a user, so this user should be retrieved from the db and not the logged user as the job in the background hasn’t a user.

You can use Meteor.loginWithPassword either by chaining it so it gets called in your method callback or you can actually call it inside your method. I recommend reading into accounts package to figure how it works.

But loginWithPassword is only for client side?

This mail was clumsily typed on a tiny apparatus.

Yes, that is why I want the cron script to login as a user…

This mail was clumsily typed on a tiny apparatus.

No, you should not login with password a user in the server for a background job, this doesn’t make sense.

2 Likes

“should” and “sense” depends, in life and programming, on the situation. “Could”, on the other hand, is something that can have a definite answer.

You can setup the Meteor method, then the cron script will call this method.

const theMethod = Meteor.server.method_handlers['some.method'];
const thisContext = {
  userId: 'someUserIdYouWantToRunWith',
};
const result = theMethod.apply(thisContext, [someParams]);

I used this to test the methods. It could work in your case.

2 Likes

Ahh, that’s smart! I’ll try it!