Authentication

Login

This section provides you with the required information regarding the user authentication mechanism.

The below-mentioned methods need to be used to log in the user. Please follow the steps below to log in the user into CometChat.

Product:
Using the CometChat object created during the initialization phase, call the login method below:

Method Signature:

For Android,

Login(String username, String password, ICallbacks callback);
For iOS,

Login(String username, String password, Action < NSDictionary > success, Action < NSError > failure);

The parameters needed for the login method are as follows:
userName – Username of the user.
password – Password for the user.
callback – This is an instance of the ICallback interface which contains the success and failure methods.This helps you to know if the login attempt was successful. Usage:

For Android,

    cometChat.Login("user1","password123",new ICallbacks(success => successObject(success), fail => failObject(fail)));
    public void failObject(JSONObject p0)
    {
       //block of code
    }

    public void successObj(JSONObject p0)
    {
    //block of code
    }
For iOS,

    cometChat.Login("test1","test123",(dict) => { this.OnSuccess(dict); /* code block */ }, (err) => { this.OnFailure(err); /* code block */ });

The initialization steps remain the same for the cloud variant. The login mechanism uses just the userId of the user instead of the username and password. Use the CometChat object created during the initialization, and call the login method below Method Signature:

For Android,

    Login(String userId, ICallbacks callback); 
For iOS,

    Login(String userID, Action < NSDictionary > success, Action < NSError > failure)

Parameters
userId – User ID of the user.
callback – This is an instance of the Callback interface which contains the success and failure methods.This helps you to know if the login attempt was successful. Usage:

For Android

    cometChat.login("10", new ICallbacks(success => successObject(success), fail => failObject(fail)));
    public void failObject(JSONObject p0)
    {
       //block of code
    }

    public void successObj(JSONObject p0)
    {
    //block of code
    }
For iOS,

cometChat.Login("10",(dict) => { this.OnSuccess(dict); /* code block */ }, (err) => { this.OnFailure(err); /* code block */ });

Logout

Calling this function will end the current session and clears user data. Method Signature:

For Android,

    logout(ICallbacks callback)
For iOS,

    Logout(Action < NSDictionary > success, Action < NSError > failure)

ICallback Interface: The callback interface contains the methods successCallback and failCallback. The method successCallback triggers with successful logout and contains “logout successful” message in the JSON object. The method failCallback triggers with the unsuccessful logout and contains error information in a JSON object. Usage:

For Android,

    cometChat.logout(new ICallbacks(success => successObject(success), fail => failObject(fail)));
    public void failObject(JSONObject p0)
    {
       //block of code
    }

    public void successObj(JSONObject p0)
    {
    //block of code
    }

For iOS,

     cometChat.Logout((dict) => { this.OnSuccess(dict); }, (err) => { this.OnFailure(err);});

  • Was this helpful?
  • Yes   No