Hey,
i am trying to trigger my Firebase authentication with HTTP and firebase REST API.
Because i wasnt able to get this (https://github.com/rokf/lua-firebase) to run, i decided to port it into LuaSocket.
This is how he does his authentication signup:
idtoolkit_reqcore("signupNewUser",{email=email,password=password,returnSecureToken=true},M.key)
local function idtoolkit_reqcore(appstr,t,key)
local uri = string.format("https://www.googleapis.com/identitytoolkit/v3/relyingparty/%s?key=%s",appstr,key)
local r = request.new_from_uri(uri)
r.headers:upsert(":method","POST",false)
r.headers:upsert("content-type","application/json",false)
local datastring = cjson.encode(t)
r.headers:upsert("content-length",tostring(#datastring),false)
r:set_body(datastring)
local h,s = r:go()
if h:get(":status") ~= "200" then return nil, h:get(":status"), s:get_body_as_string() end
return cjson.decode(s:get_body_as_string())
end
I used his code and the firebase reference (https://firebase.google.com/docs/reference/rest/auth/#section-create-email-password) to code the following:
function M.signup_email (email, password)
local endpoint = "signupNewUser"
local url = string.format("https://www.googleapis.com/identitytoolkit/v3/relyingparty/%s?key=%s",endpoint, M.apiKey)
local headers = {}
headers["content-type"] = "application/json"
headers[":method"] = "POST"
local datastring = myJson.encode({ email = email, password = password, returnSecureToken = true })
local request = {url = url, method = "POST", headers = headers, post_data = datastring, callback = callback}
pprint(request)
M.result = http.request(request)
pprint(M.result)
end
My result just prints “1” and i dont know why.
My request looks like this if i print it:
{
method = POST,
url = https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=[MyApiKey],
post_data = {"password":"testuser1220","email":"test@test.com","returnSecureToken":true},
callback = function: 0x1a4b6390,
headers = {
content-type = application/json,
:method = POST,
}
}
Can anyone help me?