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]