Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwriting Meteor's default login handler

I'm using NPM ldapjs and this Meteor LDAP project to try to enable LDAP on my test Telescope website. The main issue is that I want LDAP to be the sole method of logging in. My plan is to have a wrapper around the built-in Telescope (Meteor?) login method. If the LDAP credentials pass, it runs the Telescope login script and continues as normal.

Perhaps this is a hacky solution and a better one exists? In any case, I need LDAP to work. Right now, I'm trying to overwrite the default login method with this:

Template.signin.events({
    'submit': function(event, template){
        Session.set('errorMessage', null);
        event.preventDefault();
        console.log("My login script ran!"); // I never see this message =(
        return Meteor.loginWithLDAP(template.find('#login-username').value,
            template.find('#login-password').value, function(error) {

            return Session.set('errorMessage', 'Login failed');
        });
    }
});

As the comment says, the log command never runs (I know because I'm using Chrome, and the console after attempting to log in is blank), and additionally, I get this with every page load:

Uncaught TypeError: Cannot read property 'events' of undefined
    (anonymous function) @ ldap_client.js:45
    (anonymous function) @ typ_accounts-ldap.js?0ad074ecfc292bededc7d318da4746392aa0f5f8:94
    (anonymous function) @ typ_accounts-ldap.js?0ad074ecfc292bededc7d318da4746392aa0f5f8:101

Line 45 is Template.signin.events({, so I have concluded that Template does not have a signin member. I've seen that Template.signin.events({...}) code a few different places (just google "Template.signin.events" with the quotes), but I guess they took that stuff out with a Meteor update?

Another version I have tried is

Template.loginButtons.events({
    'submit #login-form': function(event, template){
        ...blah blah blah...

but *gasp* that doesn't work either. It does NOT give me the Uncaught TypeError that I get with the original code, but I guess it just fails to overwrite the correct handler.

Just to be clear, this is all code that runs client-side within a custom package of mine. My JS file is a direct child of the custom package I have in Telescope.

How do I overwrite the default login handler in Meteor? (i.e. how do I make my code run when you click the "log in" button instead of Meteor's code?)


Update:

Upon request, I have tried the steps in this short walkthrough that shows an example of "Extending Meteor Accounts". I get lots of errors in the console on the client side, and one error in the console on the server side. I get the feeling that is happening because the suggestion is one that doesn't play well with Telescope specifically, but perhaps if I had a plain old instance of Meteor, it would work.

like image 749
Carter Pape Avatar asked Jul 17 '15 19:07

Carter Pape


1 Answers

The proper way to achieve this would be to do it the same way that Telescope does it in their own project:

First, clone telescope into your packages directory so you can customize it. Notice that Telescope is entirely made of packages. These are the core Telescope modules which make up the app. I believe you should only have to edit the following 2 modules: telescope-core, and telescope-users.

In telescope-core, the configuration for the sign in views are defined at packages/telescope-core/lib/config.js. I would add/remove any fields here, and you can also override the templates as well as the redirects.

In telescope-users, you'll probably be most interested in packages/telescope-users/lib/callbacks.js. This is where you would put the code that you are putting into your event handlers.

Note that Telescope uses their own custom library for dealing with callbacks, which you'll have to also use. Luckily, it's very simple. You simply add callbacks with Telescope.callbacks.add(hook, callback) and remove them with Telescope.callbacks.remove(hook, callback). Also an async versions of Telescope.add is available at Telescope.runAsync.

You'll first want to look through the existing callbacks and get rid of whatever creates the account by default. The specific hook you'll want to look for is most likely onCreateUser. So just search their github for Telescope.callbacks.add('onCreateUser',... You'll want to do the same thing with their collection hooks which are also defined in that same file and replace those as you see fit.

Hope this helps.

like image 70
Cooper Maruyama Avatar answered Oct 01 '22 04:10

Cooper Maruyama