1

I am trying to register a new user through node-xmpp from node.js to an ejabberd but the authentication and registration events are not invoking neither it is not connecting.

var clientXMPP = require('node-xmpp-client');
var serverXMPP = require('node-xmpp-server');
var c2s = new serverXMPP.C2SServer({
        jid: 'testuser@192.168.1.1',
        password: 'test'
    });

    c2s.on("connect", function(client) {
        console.log(client)
        c2s.on('register', function(opts, cb) {
            console.log('REGISTER')
            cb(false)
        })
        c2s.on("authenticate", function(opts, cb) {
            console.log("AUTH" + opts.jid + " -> " +opts.password);
            cb(false);
        });
        c2s.on('disconnect', function () {
            console.log('DISCONNECT')
        })
    });

How can I register a new user and authenticate them in node-xmpp.

JN_newbie
  • 5,492
  • 14
  • 59
  • 97
  • Do you have registration enabled in ejabberd ? What is the issues you are having ? Any errors sent by your code ? – Mickaël Rémond Jul 27 '16 at 10:35
  • @MickaëlRémond, it is not invoking connect. and registration is also enabled in ejabberd. but I want to register a new user in ejabberd using node-xmpp. I guess there is something wrong in code. – JN_newbie Jul 27 '16 at 12:06

1 Answers1

1

If you just need to create new user from nodejs to ejabberd server please make sure you have in-band registration enabled in ejabberd.

you don't need node-xmpp-server, you can only us node-xmpp-client. Client library will help you to connect with ejabberd admin user.

here is an example

const Client = require('node-xmpp-client');

let admin = new Client({
    jid: '{adminJID}',
    password: '{adminPassword}'
});

// check for errors to connect with socket
admin.connection.socket.on('error', function (error) {
    // handle error
});

admin.on('online', function (data) {

    // Register stanza
    let stanza = new Client.Stanza('iq', {type: 'set', id: 'reg1', to: '192.168.1.1'})
        .c('query', {xmlns: 'jabber:iq:register'})
        .c('username').t('{username}').up()  // Give a new username
        .c('password').t('{password}')  // Give a new user's password

    admin.send(stanza); // send a stanza
});

// response stanzas
admin.on('stanza', function (stanza) {
    // check for error/success
});
jD Saifi
  • 21
  • 1