Check whether login succeeds

This commit is contained in:
Pascal Engélibert 2022-01-30 10:13:18 +01:00
parent e307c24e06
commit 4bd0e718a2
Signed by: tuxmain
GPG Key ID: 3504BC6D362F7DCA
1 changed files with 22 additions and 19 deletions

View File

@ -79,21 +79,21 @@ end
function MatrixChat:sync(timeout)
http.fetch(MatrixChat:get_sync_table(timeout),
function(res)
if res == nil then -- received nothing from server
minetest.log("error", "matrix_bridge - sync response is nil")
elseif res.code == 0 then
minetest.log("info", "matrix_bridge - not found / timeout")
elseif res.code == 404 then
else
local response = minetest.parse_json(res.data)
if response ~= nil then
MatrixChat:minechat(response)
self.since = response.next_batch
function(res)
if res == nil then -- received nothing from server
minetest.log("error", "matrix_bridge - sync response is nil")
elseif res.code == 0 then
minetest.log("info", "matrix_bridge - not found / timeout")
elseif res.code == 404 then
else
local response = minetest.parse_json(res.data)
if response ~= nil then
MatrixChat:minechat(response)
self.since = response.next_batch
end
end
end
end
)
)
end
-- https://matrix.org/docs/api/client-server/#post-/login
@ -107,17 +107,20 @@ function MatrixChat:login()
if res.code == 200 then
minetest.log("action", res.data)
local data = minetest.parse_json(res.data)
self.token = data.access_token
self.userid = data.user_id
MatrixChat:sync()
minetest.log("action", "Matrix authenticated")
if data.access_token ~= nil and data.user_id ~= nil then
self.token = data.access_token
self.userid = data.user_id
MatrixChat:sync()
minetest.log("action", "Matrix authenticated")
else
minetest.log("error", "Matrix login failed")
end
else
minetest.log("error", to_string(res))
end
end
)
end
MatrixChat:login()
-- https://matrix.org/docs/api/client-server/#put-/rooms/-roomId-/send/-eventType-/-txnId-
-- PUT /rooms/{roomId}/send/{eventType}/{txnId}
@ -240,4 +243,4 @@ minetest.register_on_chat_message(function(name, message)
MatrixChat:send("<"..name.."> "..message)
end)
minetest.register_on_mods_loaded(function() MatrixChat:login() end)