i might not know what im doing all the time....most of the time... but im very persistent and im glad to share what i have learned using firebase with agk studio tier1. Im very excited to see what can be done using firebase with tier1 given i was under the impression that i would have to transition to tier2 for a lot of the functionality i wanted in my app. this is my current code for signing up new users with email and password, signing them in , sending and getting database data. i tried to remove most of my project specific code but im confident this will work for anyone who has a firebase real time database at least. ive added as many note in the code as i could to help out and im sure this can be improved upon by someone with more knowledge then me. Please let me know if you find anything i can improve upon
SetErrorMode(2)
// set window properties
SetWindowTitle( "firebaselogging" )
SetWindowSize( 1280, 720, 0 )
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 )
// set window properties
FirebaseSetup()
#option_explicit
///
type LocalData_type//my local data
data as string[]
endtype
/////
type cloudData_type // my database data
data as string[]
endtype
///
type userdata_type
uid as string ///
email as string//mainly work with these three
password as string///
returnSecureToken as string
idToken as string
refreshtoken as string
endtype
///
global LocalData as LocalData_type
global cloudData as cloudData_type
global userdata as userdata_type
global returndata as string[-1]
global teststring$ as string
global response$ as string
global statuscode# as integer
global IHTTP as integer
global user as string
global user$ as string
global password$ as string
global getmessage as string
global testreturndata as String
#constant identitytoolkit "identitytoolkit.googleapis.com" //googles tool link for signingup, and in users
#constant sendemailcode "/v1/accounts:sendOobCode?key="//key = your api
#constant realtdatabase "your_app_url.firebaseio.com"//your firebase database link
#constant API "*****************************" // your api is found in the fiebase console: project overview/ project settings/ general/ web api key
#constant signinaccount "/v1/accounts:signInWithPassword?key="//key = your api
#constant signupaccount "/v1/accounts:signUp?key="//key = your api
IHTTP = CreateHTTPConnection()
userinputbox()
//main loop
do
sync()
loop
////the esult$ from each function is the return data from server
// data seent to the server must be in a type and then use .tojson() when sending it in the payload
// i used postman to help me figure out the http and test
/*
these are my firebase realtime database rules that this project was built around
{
"rules": {
"users": {
"$uid": {
// Allow only authenticated content owners access to their data
".read": "true",
".write": "true",
},
},
},
}
*/
/// post = post data, get = request data, patch = updates current data
function GetDataBase()
local i as integer
local result$ as string
local idtoken as integer
local passString as string
SetHTTPHost(IHTTP, realtdatabase, 1)
AddHTTPHeader( iHTTP,'X-HTTP-Method-Override','get')
passString = "/users/"+userdata.uid+"/data.json"
result$ = SendHTTPRequest(IHTTP,passString)
while GetHTTPResponseReady( iHTTP ) = 0
print("connecting.....")
endwhile
RemoveHTTPHeader( iHTTP,'X-HTTP-Method-Override')
endfunction result$
////
function SendDataBase()
local result$ as string
local idtoken as string
local passString as string
SetHTTPHost(IHTTP, realtdatabase, 1)
AddHTTPHeader( iHTTP,'X-HTTP-Method-Override','patch')/// the 'X-HTTP-Method-Override' removed the unique token that firebase added to every post
//send file to database // userdata.uid = firebase uid
passString = "/users/"+userdata.uid+"/.json"
result$ = SendHTTPRequest(IHTTP,passString,LocalData.tojson())
while GetHTTPResponseReady( iHTTP ) = 0
print("connecting.....")
endwhile
RemoveHTTPHeader( iHTTP,'X-HTTP-Method-Override')
endfunction result$
////
function signin(email$ as string, password$ as string)
local result$ as string
SetHTTPHost(IHTTP, identitytoolkit, 1)
while GetHTTPResponseReady( iHTTP ) = 0
print("connecting.....")
sync()
endwhile
AddHTTPHeader( iHTTP,'Content-Type: application/json','POST')
userdata.email = email$
userdata.password = password$
userdata.returnSecureToken = "true"
result$ = SendHTTPRequest(IHTTP,signinaccount + api,userdata.tojson())
while GetHTTPResponseReady( iHTTP ) = 0
print("connecting.....")
sync()
endwhile
RemoveHTTPHeader( iHTTP,'Content-Type: application/json')
endfunction result$
////
function CreateNewUser(email$ as string, password$ as string)
local result$ as string
SetHTTPHost(IHTTP, identitytoolkit, 1)
while GetHTTPResponseReady( iHTTP ) = 0
print("connecting.....")
sync()
endwhile
AddHTTPHeader( iHTTP,'Content-Type: application/json','POST')
userdata.email = email$////
userdata.password = password$//// these three are required to signup with email
userdata.returnSecureToken = "true"////
result$ = SendHTTPRequest(IHTTP,signupaccount + api,userdata.tojson())
while GetHTTPResponseReady( iHTTP ) = 0
print("connecting.....")
sync()
endwhile
RemoveHTTPHeader( iHTTP,'Content-Type: application/json')
SendDataBase()//new user has no cloud/ firebase data so we send it
endfunction result$
/////
function userinputbox()
local sendingtest$ as string
local returntext$ as string
local boxtext as integer
local boxtext2 as integer
local SignInUser as integer
local CreateUser as integer
local lastpointerx as float
local lastpointery as float
local displaymessage as integer
local stringcount as integer
local ExitMenu as integer
local signout as integer
local i as integer
///
if GetEditBoxExists(1) = 0
CreateEditBox(1)
SetEditBoxSize(1,60,10)
SetEditBoxMultiLIne(1,1)
seteditboxtextsize(1,8)
SetEditBoxBorderSize(1,.5)
SetEditBoxBorderColor(1,0,250,0,255)
boxtext = createtext("Enter Email")
settextsize(boxtext,8)
SetEditBoxPosition(1,GetVirtualWidth()/2-GetEditBoxWidth(1)/2,GetVirtualHeight()/2-GetEditBoxHeight(1)*2+GetTextTotalHeight(boxtext)*2/2)
settextposition(boxtext,GetEditBoxX(1),GetEditBoxY(1)-GetTextTotalHeight(boxtext))
CreateEditBox(2)
SetEditBoxSize(2,60,10)
SetEditBoxPosition(2,GetVirtualWidth()/2-GetEditBoxWidth(1)/2,GetEditBoxY(1)+GetEditBoxHeight(1)+GetTextTotalHeight(boxtext))
SetEditBoxMultiLIne(2,1)
seteditboxtextsize(2,8)
SetEditBoxBorderColor(2,0,250,0,255)
SetEditBoxBorderSize(2,.5)
SetEditBoxPasswordMode(2,1)
boxtext2 = createtext("Enter password")
settextsize(boxtext2,8)
settextposition(boxtext2,GetEditBoxX(2),GetEditBoxY(2)-GetTextTotalHeight(boxtext))
SignInUser = createtext("Sign in")
settextsize(SignInUser,5)
SetTextBold(SignInUser,1)
SetTextPosition(SignInUser,geteditboxx(2)+geteditboxwidth(2)-gettexttotalwidth(SignInUser),geteditboxy(2)+geteditboxheight(2))
CreateUser = createtext("Create User")
settextsize(CreateUser,5)
SetTextBold(CreateUser,1)
SetTextPosition(CreateUser,geteditboxx(2),geteditboxy(2)+geteditboxheight(2))
endif
displaymessage = createtext("")
settextsize(displaymessage,10)
SetTextColor(displaymessage,250,0,0,250)
settextposition(displaymessage,gettextx(boxtext),gettexty(boxtext) - GetTextTotalHeight(boxtext)*2)
ExitMenu = createtext("Exit menu")
settextsize(ExitMenu,14)
SetTextPosition(ExitMenu,0,GetVirtualHeight()-GetTextTotalHeight(ExitMenu)*2)
signout = createtext("signout")
settextsize(signout,14)
SetTextPosition(signout,GetVirtualWidth()-GetTextTotalWidth(signout),GetVirtualHeight()-GetTextTotalHeight(ExitMenu)*2)
do
if GetPointerState() = 1
lastpointerx = screentoworldx(GetPointerX ( ))
lastpointery = screentoworldy(GetPointerY ( ))
endif
if GetPointerReleased() = 1
user$ = GetEditBoxText(1)
password$ = GetEditBoxText(2)
//create new user
if GetTextHitTest(CreateUser,lastpointerx, lastpointery) = 1
sendingtest$ = CreateNewUser(user$, password$)
//signin
elseif GetTextHitTest(SignInUser,lastpointerx, lastpointery) = 1
sendingtest$ = signin(user$, password$)
//exitmenu
elseif GetTextHitTest(ExitMenu,lastpointerx, lastpointery) = 1
end //change in app to just exit log in screen :)
//signout
elseif GetTextHitTest(signout,lastpointerx, lastpointery) = 1
CloseHTTPConnection( iHTTP )
getmessage = user$+" ,signed out"
endif
if getstringtoken(getmessage,",",2) <> "signed out"
sendingtest$ = StripString(sendingtest$," {}[]"+CHR(10)+CHR(34))
stringcount = CountStringTokens(sendingtest$,"," )
for i = 1 to stringcount
returndata.length = returndata.length + 1
returndata[returndata.length] = StripString(sendingtest$," ")
returndata[returndata.length] = getstringtoken(sendingtest$,",",i)
next i
for i = 0 to returndata.length
teststring$ = GetStringToken(returndata[i],":",1)
if teststring$ = "message"
getmessage = GetStringToken(returndata[i],":",2)// message from the server in regards to log in credentials
elseif teststring$ = "registered"
getmessage = user$ + " signed in"
elseif teststring$ = "idToken"
userdata.idToken = GetStringToken(returndata[i],":",2)
elseif teststring$ = "localId"
userdata.uid = GetStringToken(returndata[i],":",2)//// very important //// also can be found with the email under authentication in firebase console
elseif teststring$ = "refreshToken"
userdata.refreshToken = GetStringToken(returndata[i],":",2)
endif
next i
endif
//response$ = GetHTTPResponse( iHTTP )
//statuscode# = GetHTTPstatuscode( iHTTP )
getmessage = StripString(getmessage,",")
settextstring(displaymessage,getmessage)
SetTextPosition(displaymessage,geteditboxx(1)+geteditboxwidth(1)/2-GetTextTotalWidth(displaymessage)/2,gettexty(displaymessage))
endif
sync()
loop
deletetext(boxtext)
deletetext(boxtext2)
deletetext(SignInUser)
deletetext(CreateUser)
deletetext(displaymessage)
deletetext(signout)
deletetext(exitmenu)
DeleteEditBox(1)
DeleteEditBox(2)
endfunction