LoginExample + dbExtension

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

User avatar
goodguy20k
Posts: 71
Joined: 10 Jan 2006, 23:58
Location: Texas, USA
Contact:

LoginExample + dbExtension

Postby goodguy20k » 19 Jan 2006, 17:53

Howdy, first time to post a question.

I'm trying to get a serverside extension built that will take a users login and password and check them against a MySQL database. I took the elements from dbExtension.as and through them into loginExample.as and all goes well! Except... When I go to check input password against database password, it says they don't match. I've been tracing the varibables, and everything looks correct to me. Here's some code snippets:

Code: Select all

   
if (evt.name == "loginRequest")
   {
      var error = ""
      
      var nick = evt["nick"]
      var pass = evt["pass"]
      var chan = evt["chan"]
      
      // create a SQL statement
      var sql = "SELECT * FROM userid WHERE usr='" + nick + "'"
      trace(sql)

Code: Select all

      if (queryRes != null)
      {
         // Get a record
         var tempRow = queryRes.get(0)
         
         // From the record object we can get each field value
         item.id    = tempRow.getItem("id")
         item.usr    = tempRow.getItem("usr")
         dbPass      = tempRow.getItem("pwd")
         item.pwd   = tempRow.getItem("pwd")
         item.email    = tempRow.getItem("email")
         item.power    = tempRow.getItem("power")
         //trace("ID:" + item.id + " Username: " + item.usr + " Password: " + dbPass + ":" + item.pwd + ":" + pass + " Email: " + item.email + " Power: " + item.power)
         
         response.db.push( item )
      }
      else
         error = "Authentication failed - User not found"


And this is where it dies:

Code: Select all

      trace("ID:" + item.id + " Username: " + item.usr + " Password: " + item.pwd + ":" + pass + " Email: " + item.email + " Power: " + item.power)
      if (pass != dbPass)
      {
         error = "Authentication failed - Wrong Password"
      }


[dbVIP.as]: dbVIP extension loaded and running.
[dbVIP.as]: Event received: loginRequest
[dbVIP.as]: SELECT * FROM userid WHERE usr='gg20k'
[dbVIP.as]: ID:2 Username: gg20k Password: goodguy:goodguy Email: none@none.com Power: 1


Any thoughts?
Last edited by goodguy20k on 19 Jan 2006, 18:00, edited 1 time in total.
User avatar
goodguy20k
Posts: 71
Joined: 10 Jan 2006, 23:58
Location: Texas, USA
Contact:

Postby goodguy20k » 19 Jan 2006, 17:55

P.S. It's a mess, I know. things like dbPass are just vars I'm trying to test things with. (I defined it as a string, for example, to make sure it wasn't two types of data not compairing correctly.)
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 20 Jan 2006, 07:03

hmm..
in this code:

Code: Select all

if (queryRes != null)
      {
         // Get a record
         var tempRow = queryRes.get(0)
         
         // From the record object we can get each field value
         item.id    = tempRow.getItem("id")
         item.usr    = tempRow.getItem("usr")
...
...

where is the item object defined initially?
If it's not defined ( item = new Object() ) it could cause problems... don't you get an error?

Another thing: try tracing the data types of the two variables you're comparing, like this:

trace(typeof dbPass)
trace(typeof pass)


what does it trace?
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
goodguy20k
Posts: 71
Joined: 10 Jan 2006, 23:58
Location: Texas, USA
Contact:

Postby goodguy20k » 20 Jan 2006, 14:54

It traces:

Code: Select all

[dbVIP.as]: object
[dbVIP.as]: object


And the item is defined with:

Code: Select all

   if (evt.name == "loginRequest")
   {
      var error = ""
      
      var nick = evt["nick"]
      var pass = evt["pass"]
      var chan = evt["chan"]
      
      // create a SQL statement
      var sql = "SELECT * FROM userid WHERE usr='" + nick + "'"
      trace(sql)
      
      // execute query on DB
      // queryRes is a ResultSet object
      var queryRes = dbase.executeQuery(sql)

      // prepare the response object
      var response = {}
      
      // Here we create an array for storing the database data
      response.db = []
      
      // This object will hold the record data that we'll send to the client
      var item = {}
      
      // This variable will hold the password retrieved from the database
      var dbPass = ""
      
      // queryRes is ResultSet object
      // Methods available:
      //
      // size()    the number of records contained
      // get(n)   get the nth record in the RecordSet
      //
      // Example:
      // var record = queryRes.get(0)
      //
      // Gets the first record in the RecordSet
      //
      if (queryRes != null)
      {
         // Get a record
         var tempRow = queryRes.get(0)
         
         // From the record object we can get each field value
         item.id    = tempRow.getItem("id")
         item.usr    = tempRow.getItem("usr")
         dbPass      = tempRow.getItem("pwd")
         item.pwd   = tempRow.getItem("pwd")
         item.email    = tempRow.getItem("email")
         item.power    = tempRow.getItem("power")
         //trace("ID:" + item.id + " Username: " + item.usr + " Password: " + dbPass + ":" + item.pwd + ":" + pass + " Email: " + item.email + " Power: " + item.power)
         
         response.db.push( item )
      }
      else
         error = "Authentication failed - User not found"


      /*
      *
      * The event passes 3 arguments: the nickname used by the client, the password
      * and its "channel"
      *
      * At the moment of login the user is only recognized by its communication channel
      * and User object for the client hasn't been already created.
      *
      * It will be created only if the login process is successfull
      *
      */
      
      trace("ID:" + item.id + " Username: " + item.usr + " Password: " + item.pwd + ":" + pass + " Email: " + item.email + " Power: " + item.power)
      trace(typeof dbPass);
      trace(typeof pass);
      if (pass != dbPass)
      {
         error = "Authentication failed - Wrong Password"
      }
      else
      {
         /*
         * The loginUser() method allows to ask the server to login
         * a new user.
         *
         * In return you will get an object with 2 properties:
         *
         * success = a boolean. True if the login was successfull
         * error = an error message if login failed.
         */
         var obj = _server.loginUser(nick, pass, chan)
         
         if (obj.success == false)
            error = obj.error
   
      }
      
      // Send response to client
      var response = new Object()
      
      if (error == "")
      {
         response._cmd = "logOK"
      }
      else
      {
         response._cmd = "logKO"
         response.err = error      }
      
      /*
      * NOTE:
      * usually the 4th parameter of the sendResponse is a list of users object that will
      * receive this message.
      *
      * Only when handling a login request you can pass a channel instead of a list of Users
      */
      
      _server.sendResponse(response, -1, null, chan)

   }
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 20 Jan 2006, 16:24

I've done a little test and I think you've spotted somethin interesting. :D

The problem is that both password variables are Java strings, as they come from two different Java objects. (One is the event object and the other is the database row)

This is confirmed by tracing the type of both variables which output "object" as a response.

There's a very simple solution to this. Just cast one of the two to an Actionscript String object and everything will work without problems.

Example:
instead of this code

Code: Select all

var nick = evt["nick"]
var pass = evt["pass"]
var chan = evt["chan"]


use this

Code: Select all

var nick = String(evt["nick"])
var pass = String(evt["pass"])
var chan = evt["chan"]


We will either fix it or update our documentation about this issue in the next release.

Hope it helps :)
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
goodguy20k
Posts: 71
Joined: 10 Jan 2006, 23:58
Location: Texas, USA
Contact:

Postby goodguy20k » 20 Jan 2006, 16:33

It works! Thank you!

Return to “Server Side Extension Development”

Who is online

Users browsing this forum: No registered users and 27 guests