Search code examples
javaandroidxmppejabberdsmack

Adding new user to ejabberd through Smack android api


I have installed ejabberd on my local server. This was then tested in spark for its functionality and it worked fine. Now I want to add a new user through the android app.

I tried adding a new user through spark and it worked fine. The fields I have given are uesrname, password, confirm password, server. But when I tried to do it using the smack api in the android app it gave the following error:

org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: forbidden - auth

I was using createAccount(), seen in the code I was using below, to create the new account in smack.

AccountManager accountManager = AccountManager.getInstance(conn1);

try {
    accountManager.createAccount("tryuser", "qwerty");
    Log.i("log", "created user successfully");
} catch (SmackException.NoResponseException e) {
    e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
    e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
    e.printStackTrace();
}

I have checked if it supports new account creation by supportsAccountCreation() and it returned true.

I have changed my register rule to allow all in ejabberd server. and i don't think it has any problem because i can create account from spark, but getting error in smack.

I have looked into the following SO questions related to this topic but no luck.

Does anyone have any suggestions on how to solve this?


Solution

  • Please give a try with below -

    AccountManager accountManager = AccountManager.getInstance(connection);
                    try {
                        if (accountManager.supportsAccountCreation()) {
                            accountManager.sensitiveOperationOverInsecureConnection(true);
                            accountManager.createAccount("userName", "password");
    
                        }
                    } catch (SmackException.NoResponseException e) {
                        e.printStackTrace();
                    } catch (XMPPException.XMPPErrorException e) {
                        e.printStackTrace();
                    } catch (SmackException.NotConnectedException e) {
                        e.printStackTrace();
                    }
    

    And you also need to set below in ejabberd.cfg (config file)

    {access, register, [{allow, all}]}.
    

    which means - In-band registration allows registration of any possible username. To disable in-band registration, replace 'allow' with 'deny'.

    And in mod_register (module in same config file) please set below -

    {access_from, register} 
    

    & before that please check you are connected to XMPP server.

    Probably this will resolve your issue.