66class_name FirebaseAuth
77extends HTTPRequest
88
9+ const _API_VERSION : String = "v1"
10+
911# Emitted for each Auth request issued.
1012# `result_code` -> Either `1` if auth succeeded or `error_code` if unsuccessful auth request
1113# `result_content` -> Either `auth_result` if auth succeeded or `error_message` if unsuccessful auth request
@@ -26,15 +28,17 @@ const RESPONSE_ASSERTION : String = "identitytoolkit#VerifyAssertionResponse"
2628const RESPONSE_USERDATA : String = "identitytoolkit#GetAccountInfoResponse"
2729const RESPONSE_CUSTOM_TOKEN : String = "identitytoolkit#VerifyCustomTokenResponse"
2830
29- var _signup_request_url : String = "https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=%s "
30- var _signin_with_oauth_request_url : String = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp?key=%s "
31- var _signin_request_url : String = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=%s "
32- var _signin_custom_token_url : String = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=%s "
33- var _userdata_request_url : String = "https://identitytoolkit.googleapis.com/v1/accounts:lookup?key=%s "
31+ var _base_url : String = ""
32+ var _signup_request_url : String = "accounts:signUp?key=%s "
33+ var _signin_with_oauth_request_url : String = "accounts:signInWithIdp?key=%s "
34+ var _signin_request_url : String = "accounts:signInWithPassword?key=%s "
35+ var _signin_custom_token_url : String = "accounts:signInWithCustomToken?key=%s "
36+ var _userdata_request_url : String = "accounts:lookup?key=%s "
37+ var _oobcode_request_url : String = "accounts:sendOobCode?key=%s "
38+ var _delete_account_request_url : String = "accounts:delete?key=%s "
39+ var _update_account_request_url : String = "accounts:update?key=%s "
40+
3441var _refresh_request_url : String = "https://securetoken.googleapis.com/v1/token?key=%s "
35- var _oobcode_request_url : String = "https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode?key=%s "
36- var _delete_account_request_url : String = "https://identitytoolkit.googleapis.com/v1/accounts:delete?key=%s "
37- var _update_account_request_url : String = "https://identitytoolkit.googleapis.com/v1/accounts:update?key=%s "
3842var _google_auth_request_url : String = "https://accounts.google.com/o/oauth2/v2/auth?"
3943var _google_token_request_url : String = "https://oauth2.googleapis.com/token?"
4044
@@ -170,6 +174,19 @@ func _set_config(config_json : Dictionary) -> void:
170174 _update_account_request_url %= _config .apiKey
171175
172176 connect ("request_completed" , self , "_on_FirebaseAuth_request_completed" )
177+ _check_emulating ()
178+
179+
180+ func _check_emulating () -> void :
181+ ## Check emulating
182+ if not Firebase .emulating :
183+ _base_url = "https://identitytoolkit.googleapis.com/{version} /" .format ({ version = _API_VERSION })
184+ else :
185+ var port : String = _config .emulators .ports .authentication
186+ if port == "" :
187+ Firebase ._printerr ("You are in 'emulated' mode, but the port for Authentication has not been configured." )
188+ else :
189+ _base_url = "http://localhost:{port} /{version} /" .format ({ version = _API_VERSION ,port = port })
173190
174191
175192# Function is used to check if the auth script is ready to process a request. Returns true if it is not currently processing
@@ -195,7 +212,7 @@ func signup_with_email_and_password(email : String, password : String) -> void:
195212 _login_request_body .email = email
196213 _login_request_body .password = password
197214 auth_request_type = Auth_Type .SIGNUP_EP
198- request (_signup_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_login_request_body ))
215+ request (_base_url + _signup_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_login_request_body ))
199216
200217
201218# Called with Firebase.Auth.anonymous_login()
@@ -206,7 +223,7 @@ func login_anonymous() -> void:
206223 if _is_ready ():
207224 is_busy = true
208225 auth_request_type = Auth_Type .LOGIN_ANON
209- request (_signup_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_anonymous_login_request_body ))
226+ request (_base_url + _signup_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_anonymous_login_request_body ))
210227
211228
212229# Called with Firebase.Auth.login_with_email_and_password(email, password)
@@ -218,7 +235,7 @@ func login_with_email_and_password(email : String, password : String) -> void:
218235 _login_request_body .email = email
219236 _login_request_body .password = password
220237 auth_request_type = Auth_Type .LOGIN_EP
221- request (_signin_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_login_request_body ))
238+ request (_base_url + _signin_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_login_request_body ))
222239
223240# Login with a custom valid token
224241# The token needs to be generated using an external service/function
@@ -227,15 +244,17 @@ func login_with_custom_token(token : String) -> void:
227244 is_busy = true
228245 _custom_token_body .token = token
229246 auth_request_type = Auth_Type .LOGIN_CT
230- request (_signin_custom_token_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_custom_token_body ))
247+ request (_base_url + _signin_custom_token_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_custom_token_body ))
231248
232249# Open a web page in browser redirecting to Google oAuth2 page for the current project
233250# Once given user's authorization, a token will be generated.
234251# NOTE** with this method, the authorization process will be copy-pasted
252+
235253func get_google_auth (redirect_uri : String = "urn:ietf:wg:oauth:2.0:oob" , client_id : String = _config .clientId ) -> void :
236254 var url_endpoint : String = _google_auth_request_url
237255 _google_auth_body .redirect_uri = redirect_uri
238256
257+
239258func get_google_auth_manual () -> void :
240259 var url_endpoint : String = _google_auth_request_url
241260 _google_auth_body .redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
@@ -290,7 +309,6 @@ func _tcp_stream_timer() -> void:
290309 login_with_oauth (token , _google_auth_body .redirect_uri )
291310
292311
293-
294312# Login with Google oAuth2.
295313# A token is automatically obtained using an authorization code using @get_google_auth()
296314# @provider_id and @request_uri can be changed
@@ -304,7 +322,7 @@ func login_with_oauth(_google_token: String, request_uri : String = "urn:ietf:wg
304322 _oauth_login_request_body .requestUri = _request_uri .replace ("[REQUEST_URI]" , request_uri if request_uri != "urn:ietf:wg:oauth:2.0:oob" else "http://localhost" )
305323 requesting = Requests .LOGIN_WITH_OAUTH
306324 auth_request_type = Auth_Type .LOGIN_OAUTH
307- request (_signin_with_oauth_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_oauth_login_request_body ))
325+ request (_base_url + _signin_with_oauth_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_oauth_login_request_body ))
308326
309327
310328# Exchange the authorization oAuth2 code obtained from browser with a proper access id_token
@@ -337,7 +355,7 @@ func manual_token_refresh(auth_data):
337355 refresh_token = auth .refresh_token
338356 _needs_refresh = true
339357 _refresh_request_body .refresh_token = refresh_token
340- request (_refresh_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_refresh_request_body ))
358+ request (_base_url + _refresh_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_refresh_request_body ))
341359
342360
343361# This function is called whenever there is an authentication request to Firebase
@@ -450,7 +468,7 @@ func change_user_email(email : String) -> void:
450468 is_busy = true
451469 _change_email_body .email = email
452470 _change_email_body .idToken = auth .idtoken
453- request (_update_account_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_change_email_body ))
471+ request (_base_url + _update_account_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_change_email_body ))
454472
455473
456474# Function used to change the password for the currently logged in user
@@ -459,7 +477,7 @@ func change_user_password(password : String) -> void:
459477 is_busy = true
460478 _change_password_body .password = password
461479 _change_password_body .idToken = auth .idtoken
462- request (_update_account_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_change_password_body ))
480+ request (_base_url + _update_account_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_change_password_body ))
463481
464482
465483# User Profile handlers
@@ -471,15 +489,15 @@ func update_account(idToken : String, displayName : String, photoUrl : String, d
471489 _update_profile_body .photoUrl = photoUrl
472490 _update_profile_body .deleteAttribute = deleteAttribute
473491 _update_profile_body .returnSecureToken = returnSecureToken
474- request (_update_account_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_update_profile_body ))
492+ request (_base_url + _update_account_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_update_profile_body ))
475493
476494
477495# Function to send a account verification email
478496func send_account_verification_email () -> void :
479497 if _is_ready ():
480498 is_busy = true
481499 _account_verification_body .idToken = auth .idtoken
482- request (_oobcode_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_account_verification_body ))
500+ request (_base_url + _oobcode_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_account_verification_body ))
483501
484502
485503# Function used to reset the password for a user who has forgotten in.
@@ -488,7 +506,7 @@ func send_password_reset_email(email : String) -> void:
488506 if _is_ready ():
489507 is_busy = true
490508 _password_reset_body .email = email
491- request (_oobcode_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_password_reset_body ))
509+ request (_base_url + _oobcode_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print (_password_reset_body ))
492510
493511
494512# Function called to get all
@@ -500,14 +518,14 @@ func get_user_data() -> void:
500518 is_busy = false
501519 return
502520
503- request (_userdata_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print ({"idToken" :auth .idtoken }))
521+ request (_base_url + _userdata_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print ({"idToken" :auth .idtoken }))
504522
505523
506524# Function used to delete the account of the currently authenticated user
507525func delete_user_account () -> void :
508526 if _is_ready ():
509527 is_busy = true
510- request (_delete_account_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print ({"idToken" :auth .idtoken }))
528+ request (_base_url + _delete_account_request_url , _headers , true , HTTPClient .METHOD_POST , JSON .print ({"idToken" :auth .idtoken }))
511529
512530
513531# Function is called when a new token is issued to a user. The function will yield until the token has expired, and then request a new one.
0 commit comments