Check if Parse Username already exists HELP!

Hi Corona Community!  

Today I have a question, regarding checking already existing usernames via Parse.

On my login page, I am allowing my user to create an account(username + password).  I am using Parse backend for this.

 I need help writing code that will handle if the username they choose to use already exists on my parse user database!  

My code so far is below of my account creation file.

ALL help is appreciated! Thanks!

[lua]–remove status bar

display.setStatusBar( display.HiddenStatusBar )

–require modules

local composer = require “composer”

local scene = composer.newScene()

local parse = require(‘plugin.parse’)

parse.config:applicationId(“MY ID HERE”)

parse.config:restApiKey(“MY REST API KEY HERE”)

parse.config:debugEnabled( true )

parse.showStatus = true

local usernameInput

local passwordInput

local bg

local createAccountBtn

local createAccount

local function onUsername( event )

    if ( “began” == event.phase ) then

        – This is the “keyboard appearing” event.

        – In some cases you may want to adjust the interface while the keyboard is open.

    elseif ( “submitted” == event.phase ) then

        – Automatically tab to password field if user clicks “Return” on virtual keyboard.

        native.setKeyboardFocus( passwordInput)

    end

end

local function onPassword( event )

    – Hide keyboard when the user clicks “Return” in this field

    if ( “submitted” == event.phase ) then

        native.setKeyboardFocus( nil )

    end

end

–create account

local function createAccount(event)

if (event.phase == “began”) then 

parse.request( parse.User.query )

  :where( { username = “”…usernameInput.text } )

  :response(cb)

  

–check here if username already exists!

–if it doesn’t exist, create account!

–add account to parse database

parse.request( parse.User.create ) 

:data( { username = “”…usernameInput.text, password = “”…passwordInput.text} )

:response(cb)

end

end

–create scene

function scene:create( event )

local group = self.view

bg = display.newRect(display.contentCenterX, display.contentCenterY, 800, 800)

bg:setFillColor( 1, .6, 0 )

– Create username text field

usernameInput = native.newTextField( display.contentCenterX, 150, 260, 40 )

usernameInput:setReturnKey( “next” )

usernameInput.placeholder = " Username"

group:insert(usernameInput)

usernameInput:addEventListener( “userInput”, onUsername  )

– Create password text field

passwordInput = native.newTextField( display.contentCenterX, display.contentCenterY, 260, 40 )

passwordInput:setReturnKey( “go” )

passwordInput.placeholder = " Password"

group:insert(passwordInput)

passwordInput:addEventListener( “userInput”, onPassword )

– Create register account button

createAccountBtn = display.newImage(“registerBtn.png”)

createAccountBtn.x = display.contentCenterX; createAccountBtn.y = display.contentCenterY + 80

createAccountBtn:scale(.15, .1 )

end

–enter scene

function scene:show( event )

local group = self.view

local phase = event.phase

   if ( phase == “will” ) then

     

     

     

   elseif ( phase == “did” ) then

createAccountBtn:addEventListener( “touch”, createAccount )

end

end

–exit scene

function scene:exitScene( event )

local group = self.view

end

–destroy scene

function scene:destroyScene( event )

local group = self.view

local phase = event.phase

 if ( phase == “will” ) then

     

     

     

   elseif ( phase == “did” ) then

   

   

   

   

   

end

end

scene:addEventListener( “create”, scene)

scene:addEventListener( “show”, scene)

scene:addEventListener( “hide”, scene)

scene:addEventListener( “destroy”, scene)

return scene[/lua]

@develephant

I am not familiar with the workflow you want to put in place, but I would suggest doing things in a slightly different way that would save a query and simplify your code.

You can simply try to create the user account and act based on the return value. As a pseudo code example (not tested)  :

 local function onCreateUser( ok, res, info ) if ok == true then if res.code and res.error then print("onCreateUser - error :", res.code, res.error) -- invalid email address if res.code == 125 then -- Error code indicating that the username has already been taken. elseif res.code == 202 then -- Error code indicating that the email has already been taken. elseif res.code == 203 then -- manage other possible errors.... end else -- user account created with success end end parse.request( parse.User.create ) :data( { username = theUserName, email = theUserEmail, password = theUserPswd } ) :response(onCreateUser)

Please look here for the possible error code depending on the method you call : http://parse.com/docs/dotnet/api/html/T_Parse_ParseException_ErrorCode.htm

As Nick pointed out, Parse will respond with the appropriate error if the user already exists. It’s up to you at that point to handle it however needed.

Cheers.

@develephant

I am not familiar with the workflow you want to put in place, but I would suggest doing things in a slightly different way that would save a query and simplify your code.

You can simply try to create the user account and act based on the return value. As a pseudo code example (not tested)  :

 local function onCreateUser( ok, res, info ) if ok == true then if res.code and res.error then print("onCreateUser - error :", res.code, res.error) -- invalid email address if res.code == 125 then -- Error code indicating that the username has already been taken. elseif res.code == 202 then -- Error code indicating that the email has already been taken. elseif res.code == 203 then -- manage other possible errors.... end else -- user account created with success end end parse.request( parse.User.create ) :data( { username = theUserName, email = theUserEmail, password = theUserPswd } ) :response(onCreateUser)

Please look here for the possible error code depending on the method you call : http://parse.com/docs/dotnet/api/html/T_Parse_ParseException_ErrorCode.htm

As Nick pointed out, Parse will respond with the appropriate error if the user already exists. It’s up to you at that point to handle it however needed.

Cheers.