Not receiving LOGIN_ERROR

Post here your questions about the Java client / Android API for SFS2X

Moderators: Lapo, Bax

iandr0idos
Posts: 7
Joined: 27 Nov 2011, 19:07

Not receiving LOGIN_ERROR

Postby iandr0idos » 28 Nov 2011, 05:23

Hi!

I'm trying to write a simple login system for my game, and right now, I have everything working BUT the response that the client's supposed to get when the login fails.

My LoginHandler code is:

Code: Select all

package sfs2x.extension.eok.src;

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import com.smartfoxserver.bitswarm.sessions.ISession;
import com.smartfoxserver.v2.entities.data.SFSArray;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.exceptions.SFSLoginException;
import com.smartfoxserver.v2.exceptions.SFSErrorData;
import com.smartfoxserver.v2.exceptions.SFSErrorCode;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;
import com.smartfoxserver.v2.extensions.ExtensionLogLevel;

public class LoginHandler extends BaseServerEventHandler {

   public void handleServerEvent(ISFSEvent event) throws SFSException
   {
        String username = (String) event.getParameter(SFSEventParam.LOGIN_NAME);
        String password = (String) event.getParameter(SFSEventParam.LOGIN_PASSWORD);

        ISession session = (ISession)event.getParameter(SFSEventParam.SESSION);

        try {
            Connection conn = getParentExtension().getParentZone().getDBManager().getConnection();

            PreparedStatement sql = conn.prepareStatement("SELECT password FROM users WHERE username = ?");
            sql.setString(1, username);

            ResultSet result = sql.executeQuery();

            SFSArray row = SFSArray.newFromResultSet(result);

            if (password.equals(""))
            {
                SFSErrorData data = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD);
                data.addParameter(username);
                throw new SFSLoginException("You must enter a password.", data);
            }

           if (!getApi().checkSecurePassword(session, row.getSFSObject(0).getUtfString("password"), password))
           {
                SFSErrorData data = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD);

                data.addParameter(username);

                throw new SFSLoginException("Login failed for user: "  + username, data);
            }

            if (username.equals("Gonzo") || username.equals("Kermit"))
            {

                SFSErrorData errData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_USERNAME);
                errData.addParameter(username);

                throw new SFSLoginException("Gonzo and Kermit are not allowed in this Zone!", errData);
            }

            conn.close();

            trace("Login successful, joining room!");

        } catch (SQLException e) {
            trace(ExtensionLogLevel.WARN, " SQL Failed: " + e.toString());
        }
    }
}


Is there anything wrong with my code?

Thanks[/code]
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 28 Nov 2011, 10:58

Hi,

I do this:

Code: Select all

   @Override
   public void handleServerEvent(ISFSEvent event) throws SFSException {
      String name = (String) event.getParameter(SFSEventParam.LOGIN_NAME);
      String pwd = (String) event.getParameter(SFSEventParam.LOGIN_PASSWORD);
      ISession session = (Session) event.getParameter( SFSEventParam.SESSION );

      if (!verifyLogin(session, name, pwd)) {
        // Create the error code to send to the client
            SFSErrorData errData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_USERNAME);
            errData.addParameter(name);

         throw new SFSLoginException(errorMessage, errData);
      }
   }


So more or less exactly what you do. I dont think its your server side end that is a problem.

How is your client side set up?

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 28 Nov 2011, 11:55

Just made sure - I do get the login error on the client side:

Client code snippets:

Code: Select all

   sfs.addEventListener(SFSEvent.LOGIN_ERROR, new OnLoginError());
   sfs.connect("127.0.0.1", 9933);

.....
   class OnConnection implements IEventListener {
      public void dispatch(BaseEvent evt) throws SFSException {
         boolean success = (Boolean)evt.getArguments().get("success");
         System.out.println("On Connection callback got: " + success);

         if (success) {
            sfs.send(new LoginRequest("testuser", "testpwd", zone));
         }
      }
   }

....

   class OnLoginError implements IEventListener {
      public void dispatch(BaseEvent evt) throws SFSException {
         System.out.println("Login error");
      }
   }


Server log from my extension code pasted above (testuser triggers an error)

Code: Select all

12:52:31,049 INFO  [pool-1-thread-2] Extensions     - {heroesRUs}: No user found with username testuser
12:52:31,052 WARN  [pool-1-thread-2] managers.SFSExtensionManager     - com.smartfoxserver.v2.exceptions.SFSLoginException: No user found with username testuser


and client side log:

Code: Select all

383 [New I/O client worker #1-1] INFO sfs2x.client.controllers.SystemController - Message: ResLogin { Message id: 1 }
{ Dump: }

   (short) ec: 2
   (utf_string_array) ep: [testuser]
   

399 [New I/O client worker #1-1] INFO sfs2x.client.core.EventDispatcher - Dispatching event loginError to 1 listeners
Login error


Hope that helps you along

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games

Follow on twitter: http://twitter.com/thomas_h_lund
iandr0idos
Posts: 7
Joined: 27 Nov 2011, 19:07

Postby iandr0idos » 28 Nov 2011, 17:15

ACK! I forgot to add the event listener! I'm kicking myself...

Thanks!
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 28 Nov 2011, 22:58

anytime!
Full Control - maker of Unity/C# and Java SFS API and indie games

Follow on twitter: http://twitter.com/thomas_h_lund

Return to “SFS2X Java / Android API”

Who is online

Users browsing this forum: No registered users and 3 guests