getMe() and oginUser() confusion - PLEASE HELP

Hi! I am extremely confused about the correct way to use the getMe and loginUser functions. I keep getting this error: error: Session expired and sometimes my user data does not load correctly. 

A few questions:

  1. Should I not be using these functions more than one in a scene? If I can’t then how would I “get” my user information in certain functions where I need to add/remove data from their objects. I need to use getMe quite a few times in a scene. 

  2. Which goes first? I am currently using them in the following sequence: (Here is a snipper of my code that populates my menu wiith information I get from getMe):

    mc:loginUser(userEmailCheck, userPasswordCheck, function( event1 ) mc:getMe( function(e) if( signedIn == “1”) then OneSignal.SendTags({ [“shop1”] = e.result.Shop1.name, [“shop1”] = e.result.Shop2.name, [“shop1”] = e.result.Shop3.name, }) registerText(title, “G’day,”…" "…e.result.name, -15, 220, 15, myAccountEvent) menuText(title, “View Account”, -15, 180, 15, myAccountEvent) – menuText(history, “My Active Plans”, -15, 120, 15, plansEvent) menuText(suggest, “My Payment History”, -15, 80, 15, paymentsEvent) menuText(what, “What is this?”, -15, 40, 15, aboutEvent) menuText(faq, “FAQ’s”, -15, 0, 15, faqEvent) termsText(terms, “Terms of Service & Privacy”, -15, -230, 15, termsEvent) end end) end)

I am just not sure when during a scene I should use the loginUser and getMe functions (how frequently, which goes first, can I use them inside functions, etc) and not sure if I should use any sessionToken functions etc.

Please help!

Thanks!

I have only ever called loginUser once per session. I believe that is all that is required. 

As to the order, you can (obviously) only successfully call getMe once you have a logged in user, so your example code has the correct order. I often call getMe multiple times in a session because the user data might have been updated since the last getMe call. Hope that helps.

I’m having a similar issue.  I’m in the process of moving from Parse to Coronium.  I’m making progress, but having an issue with a user logging in.  

I can create the user just fine and then login immediately with the register code below:

[lua]local function onCreateUser(event)

if event.error then

print(“Error creating the user”)

local function onComplete2( event )

if “clicked” == event.action then

local i = event.index

if 1 == i then

– Do nothing

elseif 2 == i then

if username then

username:removeSelf();username=nil

end

if password then

password:removeSelf();password=nil

end

if name then

name:removeSelf();name=nil

end

if email then

email:removeSelf();email=nil

end

composer.gotoScene( “login”, “slideUp”, 500 )

end

end

end 

local alert = native.showAlert( “Sorry”, “An error occurred, please try again.”, { “Try Again”, “Cancel” }, onComplete2 )

else

print(“User was created successfully”)

local function onSuccess(event)

if “clicked” == event.action then

if 1 == event.index then

local function onLoginUser( event )

globals.sessionToken = event.result.sessionToken

globals.userId = event.result.objectId

print("objectId is… "…globals.userId) --results in error saying nil value                   

end

coronium:loginUser( globals.email, globals.password , onLoginUser ) 

if username then

username:removeSelf();username=nil

end

if password then

password:removeSelf();password=nil

end

if name then

name:removeSelf();name=nil

end

if email then

email:removeSelf();email=nil

end

composer.gotoScene( “menu”, “fade”, 1400 ) --go straight into the app

elseif 2 == event.index then

local function onLoginUser( event )

globals.sessionToken = event.result.sessionToken

globals.userId = event.result.objectId                        

end

coronium:loginUser( globals.email, globals.password, onLoginUser )

if username then

username:removeSelf();username=nil

end

if password then

password:removeSelf();password=nil

end

if name then

name:removeSelf();name=nil

end

if email then

email:removeSelf();email=nil

end

composer.gotoScene( “login”, “slideUp”, 500 )

end

end

end

local successalert = native.showAlert(“Account created successfully.”, “Would you like to login now?”, {‘Yes’, ‘No’}, onSuccess)

end

end    

local userData = {

[“username”] = globals.username, 

[“password”] = globals.password, 

[“email”] = globals.email,

[“name”] = globals.name,

}

coronium:registerUser( userData, onCreateUser )[/lua]

The register code above works fine, and the embedded login code above gets the user into their data appropriately; but event.result is not returning an objectId.  The problem doesn’t arise, though, until the user logs out and then another user tries to login with an existing account using the code below:  

[lua]local function onLoginUser( event )

if not event.error then

globals.sessionToken = event.result.sessionToken

globals.userId = event.result.objectId

print("userid1 "…event.result.objectId)  --results in error saying nil value   

print("sessionToken "…event.result.sessionToken)

coronium:setSessionToken( globals.sessionToken )

local function callback(event)

print("userid2 "…event.result.objectId)  --Gets an objectId, but it’s the last user logged in, not the current user logging in   

globals.name = event.result.name

globals.email = event.result.email

print("name "…globals.name)

print("email "…globals.email)

composer.gotoScene( “menu”, “fade”, 400 ) 

end 

coronium:getMe( callback )

else

local function onComplete(event)

composer.gotoScene( “login”, “fade”, 400 ) 

end

local alert = native.showAlert( “Error”, event.error, { “OK” }, onComplete )

end

end

coronium:loginUser( globals.email, globals.password, onLoginUser )[/lua]

What seems to be happening is:  even though the newly logging in user is logging in with their credentials, it logs in the user that was previously logged in.  I’m sure it’s a problem with the flow of my code, but I don’t understand Coronium well enough yet to understand why.  

With Parse, when a user logged in, event.result provided a sessionToken and an objectId.  However, in Coronium, I’m only receiving a sessionToken and objectId returns nil as noted in the print statement comments.  However, when I run getMe(), I can get an objectId returned, but in the case above, it returns the objectId for the previously logged in user.  

I truly appreciate any help anyone can provide.

Nevermind… I found my issue… all good now.

I have only ever called loginUser once per session. I believe that is all that is required. 

As to the order, you can (obviously) only successfully call getMe once you have a logged in user, so your example code has the correct order. I often call getMe multiple times in a session because the user data might have been updated since the last getMe call. Hope that helps.

I’m having a similar issue.  I’m in the process of moving from Parse to Coronium.  I’m making progress, but having an issue with a user logging in.  

I can create the user just fine and then login immediately with the register code below:

[lua]local function onCreateUser(event)

if event.error then

print(“Error creating the user”)

local function onComplete2( event )

if “clicked” == event.action then

local i = event.index

if 1 == i then

– Do nothing

elseif 2 == i then

if username then

username:removeSelf();username=nil

end

if password then

password:removeSelf();password=nil

end

if name then

name:removeSelf();name=nil

end

if email then

email:removeSelf();email=nil

end

composer.gotoScene( “login”, “slideUp”, 500 )

end

end

end 

local alert = native.showAlert( “Sorry”, “An error occurred, please try again.”, { “Try Again”, “Cancel” }, onComplete2 )

else

print(“User was created successfully”)

local function onSuccess(event)

if “clicked” == event.action then

if 1 == event.index then

local function onLoginUser( event )

globals.sessionToken = event.result.sessionToken

globals.userId = event.result.objectId

print("objectId is… "…globals.userId) --results in error saying nil value                   

end

coronium:loginUser( globals.email, globals.password , onLoginUser ) 

if username then

username:removeSelf();username=nil

end

if password then

password:removeSelf();password=nil

end

if name then

name:removeSelf();name=nil

end

if email then

email:removeSelf();email=nil

end

composer.gotoScene( “menu”, “fade”, 1400 ) --go straight into the app

elseif 2 == event.index then

local function onLoginUser( event )

globals.sessionToken = event.result.sessionToken

globals.userId = event.result.objectId                        

end

coronium:loginUser( globals.email, globals.password, onLoginUser )

if username then

username:removeSelf();username=nil

end

if password then

password:removeSelf();password=nil

end

if name then

name:removeSelf();name=nil

end

if email then

email:removeSelf();email=nil

end

composer.gotoScene( “login”, “slideUp”, 500 )

end

end

end

local successalert = native.showAlert(“Account created successfully.”, “Would you like to login now?”, {‘Yes’, ‘No’}, onSuccess)

end

end    

local userData = {

[“username”] = globals.username, 

[“password”] = globals.password, 

[“email”] = globals.email,

[“name”] = globals.name,

}

coronium:registerUser( userData, onCreateUser )[/lua]

The register code above works fine, and the embedded login code above gets the user into their data appropriately; but event.result is not returning an objectId.  The problem doesn’t arise, though, until the user logs out and then another user tries to login with an existing account using the code below:  

[lua]local function onLoginUser( event )

if not event.error then

globals.sessionToken = event.result.sessionToken

globals.userId = event.result.objectId

print("userid1 "…event.result.objectId)  --results in error saying nil value   

print("sessionToken "…event.result.sessionToken)

coronium:setSessionToken( globals.sessionToken )

local function callback(event)

print("userid2 "…event.result.objectId)  --Gets an objectId, but it’s the last user logged in, not the current user logging in   

globals.name = event.result.name

globals.email = event.result.email

print("name "…globals.name)

print("email "…globals.email)

composer.gotoScene( “menu”, “fade”, 400 ) 

end 

coronium:getMe( callback )

else

local function onComplete(event)

composer.gotoScene( “login”, “fade”, 400 ) 

end

local alert = native.showAlert( “Error”, event.error, { “OK” }, onComplete )

end

end

coronium:loginUser( globals.email, globals.password, onLoginUser )[/lua]

What seems to be happening is:  even though the newly logging in user is logging in with their credentials, it logs in the user that was previously logged in.  I’m sure it’s a problem with the flow of my code, but I don’t understand Coronium well enough yet to understand why.  

With Parse, when a user logged in, event.result provided a sessionToken and an objectId.  However, in Coronium, I’m only receiving a sessionToken and objectId returns nil as noted in the print statement comments.  However, when I run getMe(), I can get an objectId returned, but in the case above, it returns the objectId for the previously logged in user.  

I truly appreciate any help anyone can provide.

Nevermind… I found my issue… all good now.

Hi… I am having the same problem. Would you be so kind as to tell me how you resolved it?

Thanks!

Here is the code I came up with to make it work.  It seems a bit redundant looking back at it, but it works.  I hope it helps.

local function onLoginUser( event ) if not event.error then globals.sessionToken = event.result.sessionToken globals.userId = event.result.objectId --print("userid1 "..event.result.objectId) --print("sessionToken "..event.result.sessionToken) coronium:setSessionToken( globals.sessionToken ) local function callback(event) globals.userId = event.result.objectId globals.name = event.result.name globals.email = event.result.email end local function getMyData() coronium:getMe( callback ) end timer.performWithDelay(500, getMyData) else local function onComplete(event) composer.gotoScene( "login", "fade", 400 ) end local alert = native.showAlert( "Error Logging In", event.error, { "OK" }, onComplete ) end end coronium:loginUser( globals.email, globals.password, onLoginUser )

If I remember correctly the Login function already returns the user’s object. So no need to call getMe(). 

Yes, it was suppose to… or that was my understanding of it… but it didn’t work that way for me; which is why I added the getMe() call.

Hi… I am having the same problem. Would you be so kind as to tell me how you resolved it?

Thanks!

Here is the code I came up with to make it work.  It seems a bit redundant looking back at it, but it works.  I hope it helps.

local function onLoginUser( event ) if not event.error then globals.sessionToken = event.result.sessionToken globals.userId = event.result.objectId --print("userid1 "..event.result.objectId) --print("sessionToken "..event.result.sessionToken) coronium:setSessionToken( globals.sessionToken ) local function callback(event) globals.userId = event.result.objectId globals.name = event.result.name globals.email = event.result.email end local function getMyData() coronium:getMe( callback ) end timer.performWithDelay(500, getMyData) else local function onComplete(event) composer.gotoScene( "login", "fade", 400 ) end local alert = native.showAlert( "Error Logging In", event.error, { "OK" }, onComplete ) end end coronium:loginUser( globals.email, globals.password, onLoginUser )

If I remember correctly the Login function already returns the user’s object. So no need to call getMe(). 

Yes, it was suppose to… or that was my understanding of it… but it didn’t work that way for me; which is why I added the getMe() call.