Created
July 19, 2021 15:16
-
-
Save mars3142/b8f8c092ae4c067b34402db167cf3d68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Control | |
var _api_key = "secret" | |
var _api_url = "https://project_id.cloudfunctions.net/api" | |
onready var _username = $vb/input_area/username | |
onready var _password = $vb/input_area/password | |
func _ready() -> void: | |
Firebase.Auth.load_auth() | |
Firebase.Auth.connect("login_succeeded", self, "_login_succeeded") | |
Firebase.Auth.connect("userdata_received", self, "_userdata_received") | |
func _on_register_pressed() -> void: | |
var url : String = _api_url + "/v1/accounts/signup" | |
_request_custom_backend("register", url, _username.text, _password.text, HTTPClient.METHOD_POST) | |
func _on_login_pressed() -> void: | |
var url : String = _api_url + "/v1/accounts/signin" | |
_request_custom_backend("login", url, _username.text, _password.text, HTTPClient.METHOD_POST) | |
func _on_get_user_data_pressed() -> void: | |
#Firebase.Auth.change_user_email("[email protected]") | |
Firebase.Auth.get_user_data() | |
func _on_delete_pressed() -> void: | |
var url : String = _api_url + "/v1/accounts/" | |
_request_custom_backend("delete", url, _username.text, _password.text, HTTPClient.METHOD_DELETE) | |
func _request_custom_backend(action: String, url: String, username: String, password: String, method) -> void: | |
var body : String = JSON.print({ "username": username, "password": password }) | |
var headers: PoolStringArray = [ | |
"Content-Type: application/json", | |
"Content-Length: " + str(body.length()), | |
"x-api-key: " + _api_key, | |
"x-action: " + action, | |
] | |
$http_request.request(url, headers, true, method, body) | |
func _on_http_request_request_completed(result: int, response_code: int, headers: PoolStringArray, body: PoolByteArray) -> void: | |
var content: String = body.get_string_from_utf8(); | |
var data : Dictionary = parse_json(content) | |
# extract header data | |
var action : String | |
var sender_id : int | |
var player_id : int | |
for header in headers: | |
var value : String = header.split(" ")[1] | |
if header.begins_with("X-Action"): | |
action = value | |
# extract response data | |
var response: Dictionary | |
if response_code == 200: | |
response = data["data"] | |
if action == "register": | |
$vb/result.text = str(response) | |
if action == "login": | |
if response_code == 200: | |
var token: String = response["token"] | |
Firebase.Auth.login_with_custom_token(token) | |
else: | |
response = data["error"] | |
var message : String = response["message"] | |
$vb/result.text = message | |
func _login_succeeded(auth: Dictionary) -> void: | |
print("Login succeeded") | |
print(auth) | |
Firebase.Auth.save_auth(auth) | |
func _userdata_received(user_data: Dictionary) -> void: | |
print("Userdata received") | |
print(user_data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment