Skip to content

Commit 5d1d36f

Browse files
committed
added support for phone authentications
1 parent 2b0f5ad commit 5d1d36f

File tree

4 files changed

+93
-9
lines changed

4 files changed

+93
-9
lines changed

addons/supabase/.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[supabase/config]
22

3-
supabaseUrl=""
4-
supabaseKey=""
3+
supabaseUrl="<url>"
4+
supabaseKey="<service key>"

addons/supabase/Auth/auth.gd

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ class Providers:
1212
const TWITTER := "twitter"
1313

1414
signal signed_up(signed_user)
15+
signal signed_up_phone(signed_user)
1516
signal signed_in(signed_user)
16-
signal logged_out()
17+
signal signed_in_otp(signed_user)
18+
signal otp_verified()
19+
signal signed_in_anonyous()
20+
signal signed_out()
1721
signal got_user()
1822
signal user_updated(updated_user)
1923
signal magic_link_sent()
@@ -25,6 +29,8 @@ signal error(supabase_error)
2529
const _auth_endpoint : String = "/auth/v1"
2630
const _provider_endpoint : String = _auth_endpoint+"/authorize"
2731
const _signin_endpoint : String = _auth_endpoint+"/token?grant_type=password"
32+
const _signin_otp_endpoint : String = _auth_endpoint+"/otp"
33+
const _verify_otp_endpoint : String = _auth_endpoint+"/verify"
2834
const _signup_endpoint : String = _auth_endpoint+"/signup"
2935
const _refresh_token_endpoint : String = _auth_endpoint+"/token?grant_type=refresh_token"
3036
const _logout_endpoint : String = _auth_endpoint+"/logout"
@@ -50,11 +56,18 @@ var client : SupabaseUser
5056
func _init(conf : Dictionary, head : PoolStringArray) -> void:
5157
_config = conf
5258
_header = head
53-
name = "Authentication"
59+
name = "Authentication"
5460

61+
func _check_auth() -> AuthTask:
62+
printerr("User already authenticated. Please, logout before authenticating again.")
63+
var auth_task : AuthTask = AuthTask.new(-1, "", [], {})
64+
auth_task.emit_signal("completed")
65+
return auth_task
66+
5567

5668
# Allow your users to sign up and create a new account.
5769
func sign_up(email : String, password : String) -> AuthTask:
70+
if _auth != "": return _check_auth()
5871
var payload : Dictionary = {"email":email, "password":password}
5972
var auth_task : AuthTask = AuthTask.new(
6073
AuthTask.Task.SIGNUP,
@@ -65,8 +78,23 @@ func sign_up(email : String, password : String) -> AuthTask:
6578
return auth_task
6679

6780

81+
# Allow your users to sign up and create a new account using phone/password combination.
82+
# NOTE: the OTP sent to the user must be verified.
83+
func sign_up_phone(phone : String, password : String) -> AuthTask:
84+
if _auth != "": return _check_auth()
85+
var payload : Dictionary = {"phone":phone, "password":password}
86+
var auth_task : AuthTask = AuthTask.new(
87+
AuthTask.Task.SIGNUPPHONEPASSWORD,
88+
_config.supabaseUrl + _signup_endpoint,
89+
_header,
90+
payload)
91+
_process_task(auth_task)
92+
return auth_task
93+
94+
6895
# If an account is created, users can login to your app.
6996
func sign_in(email : String, password : String = "") -> AuthTask:
97+
if _auth != "": return _check_auth()
7098
var payload : Dictionary = {"email":email, "password":password}
7199
var auth_task : AuthTask = AuthTask.new(
72100
AuthTask.Task.SIGNIN,
@@ -77,12 +105,57 @@ func sign_in(email : String, password : String = "") -> AuthTask:
77105
return auth_task
78106

79107

108+
# If an account is created, users can login to your app using phone/password combination.
109+
# NOTE: this requires sign_up_phone() and verify_otp() to work
110+
func sign_in_phone(phone : String, password : String = "") -> AuthTask:
111+
if _auth != "": return _check_auth()
112+
var payload : Dictionary = {"phone":phone, "password":password}
113+
var auth_task : AuthTask = AuthTask.new(
114+
AuthTask.Task.SIGNIN,
115+
_config.supabaseUrl + _signin_endpoint,
116+
_header,
117+
payload)
118+
_process_task(auth_task)
119+
return auth_task
120+
121+
122+
# Sign in using OTP - the user won't need to use a password but the token must be validated.
123+
# This method always requires to use OTP verification, unlike sign_in_phone()
124+
func sign_in_otp(phone : String) -> AuthTask:
125+
if _auth != "": return _check_auth()
126+
var payload : Dictionary = {"phone":phone}
127+
var auth_task : AuthTask = AuthTask.new(
128+
AuthTask.Task.SIGNINOTP,
129+
_config.supabaseUrl + _signin_otp_endpoint,
130+
_header,
131+
payload)
132+
_process_task(auth_task)
133+
return auth_task
134+
135+
136+
# Verify the OTP token sent to a user as an SMS
137+
func verify_otp(phone : String, token : String) -> AuthTask:
138+
if _auth != "": return _check_auth()
139+
var payload : Dictionary = {phone = phone, token = token, type = "sms"}
140+
var auth_task : AuthTask = AuthTask.new(
141+
AuthTask.Task.VERIFYOTP,
142+
_config.supabaseUrl + _verify_otp_endpoint,
143+
_header,
144+
payload)
145+
_process_task(auth_task)
146+
return auth_task
147+
148+
149+
80150
# Sign in as an anonymous user
81151
func sign_in_anonymous() -> void:
152+
if _auth != "": return
82153
_auth = _config.supabaseKey
83154
_bearer[0] = _bearer[0] % _auth
84-
emit_signal("signed_in", null)
155+
emit_signal("signed_in")
156+
85157

158+
# [ CURRENTLY UNSUPPORTED ]
86159
# Sign in with a Provider
87160
# @provider = Providers.PROVIDER
88161
func sign_in_with_provider(provider : String, grab_from_browser : bool = true, port : int = 3000) -> void:
@@ -114,6 +187,7 @@ func send_magic_link(email : String) -> AuthTask:
114187
_process_task(auth_task)
115188
return auth_task
116189

190+
117191
# Get the JSON object for the logged in user.
118192
func user(user_access_token : String = _auth) -> AuthTask:
119193
var auth_task : AuthTask = AuthTask.new(
@@ -163,7 +237,7 @@ func invite_user_by_email(email : String) -> AuthTask:
163237
# Refresh the access_token of the authenticated client using the refresh_token
164238
# No need to call this manually except specific needs, since the process will be handled automatically
165239
func refresh_token(refresh_token : String = client.refresh_token, expires_in : float = client.expires_in) -> AuthTask:
166-
yield(get_tree().create_timer(expires_in), "timeout")
240+
yield(get_tree().create_timer(expires_in-10), "timeout")
167241
var payload : Dictionary = {refresh_token = refresh_token}
168242
var auth_task : AuthTask = AuthTask.new(
169243
AuthTask.Task.REFRESH,
@@ -185,6 +259,7 @@ func _get_link_response(delta : float) -> void:
185259
else:
186260
_get_link_response(delta)
187261

262+
188263
# Process a specific task
189264
func _process_task(task : AuthTask) -> void:
190265
var httprequest : HTTPRequest = HTTPRequest.new()
@@ -204,12 +279,18 @@ func _on_task_completed(task : AuthTask) -> void:
204279
match task._code:
205280
AuthTask.Task.SIGNUP:
206281
emit_signal("signed_up", client)
282+
AuthTask.Task.SIGNUPPHONEPASSWORD:
283+
emit_signal("signed_up_phone", client)
207284
AuthTask.Task.SIGNIN:
208285
emit_signal("signed_in", client)
286+
AuthTask.Task.SIGNINOTP:
287+
emit_signal("signed_in_otp", client)
209288
AuthTask.Task.UPDATE:
210289
emit_signal("user_updated", client)
211290
AuthTask.Task.REFRESH:
212291
emit_signal("token_refreshed", client)
292+
AuthTask.Task.VERIFYOTP:
293+
emit_signal("otp_verified")
213294
refresh_token()
214295
elif task.data == null:
215296
match task._code:
@@ -220,7 +301,7 @@ func _on_task_completed(task : AuthTask) -> void:
220301
AuthTask.Task.INVITE:
221302
emit_signal("user_invited")
222303
AuthTask.Task.LOGOUT:
223-
emit_signal("logged_out")
304+
emit_signal("signed_out")
224305
client = null
225306
_auth = ""
226307
_bearer = ["Authorization: Bearer %s"]

addons/supabase/Auth/auth_task.gd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ signal completed(task)
66
enum Task {
77
NONE,
88
SIGNUP,
9+
SIGNUPPHONEPASSWORD,
910
SIGNIN,
11+
SIGNINOTP,
1012
MAGICLINK,
1113
LOGOUT,
1214
USER,
1315
UPDATE,
1416
RECOVER,
1517
REFRESH,
16-
INVITE
18+
INVITE,
19+
VERIFYOTP
1720
}
1821

1922
var _code : int

addons/supabase/plugin.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
name="Supabase"
44
description="A lightweight addon which integrates Supabase APIs for Godot Engine out of the box."
55
author="Nicolò (fenix-hub) Santilio"
6-
version="3.0.3"
6+
version="3.0.5"
77
script="plugin.gd"

0 commit comments

Comments
 (0)