这是indexloc提供的服务,不要输入任何密码
Skip to content

Dev #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ icon.png
icon.png.import
project.godot
scn/
override.cfg

# Godot-specific ignores
.import/
Expand Down
45 changes: 38 additions & 7 deletions addons/supabase/Auth/auth.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ class_name SupabaseAuth

signal signed_up(signed_user)
signal signed_in(signed_user)
signal logged_out()
signal got_user()
signal error(supabase_error)

const _auth_endpoint : String = "/auth/v1/"
const _auth_endpoint : String = "/auth/v1"
const _signup_endpoint : String = _auth_endpoint+"/signup"
const _signin_endpoint : String = _auth_endpoint+"/token?grant_type=password"
const _logout_endpoint : String = _auth_endpoint+"/logout"
const _user_endpoint : String = _auth_endpoint+"/user"

var requests_queue : Array = []

enum REQUEST_CODES {
NONE
SIGNUP
SIGNIN
NONE,
SIGNUP,
SIGNIN,
LOGOUT,
USER
}

Expand All @@ -28,6 +31,8 @@ var _request_code : int = REQUEST_CODES.NONE
var _auth : String = ""
var _expires_in : float = 0

var client : SupabaseUser

func _init(conf : Dictionary, head : PoolStringArray) -> void:
_config = conf
_header = head
Expand All @@ -39,37 +44,62 @@ func sign_up(email : String, password : String) -> void:
requests_queue.append([REQUEST_CODES.SIGNUP, email, password])
return
_request_code = REQUEST_CODES.SIGNUP
request(_config.supabaseUrl+_signup_endpoint, _header, true, HTTPClient.METHOD_POST, JSON.print({"email":email, "password":password}))
request(_config.supabaseUrl + _signup_endpoint, _header, true, HTTPClient.METHOD_POST, JSON.print({"email":email, "password":password}))

# If an account is created, users can login to your app.
func sign_in(email : String, password : String) -> void:
if _request_code != REQUEST_CODES.NONE:
requests_queue.append([REQUEST_CODES.SIGNIN, email, password])
return
_request_code = REQUEST_CODES.SIGNIN
request(_config.supabaseUrl+_signin_endpoint, _header, true, HTTPClient.METHOD_POST, JSON.print({"email":email, "password":password}))
request(_config.supabaseUrl + _signin_endpoint, _header, true, HTTPClient.METHOD_POST, JSON.print({"email":email, "password":password}))

# If a user is logged in, this will log it out
func log_out() -> void:
if client == null : return
if _request_code != REQUEST_CODES.NONE:
requests_queue.append([REQUEST_CODES.LOGOUT])
return
_request_code = REQUEST_CODES.LOGOUT
print(_config.supabaseUrl + _logout_endpoint)
print(_header + _bearer)
request(_config.supabaseUrl + _logout_endpoint, _header + _bearer, true, HTTPClient.METHOD_POST)


# Get the JSON object for the logged in user.
func user(user_access_token : String = _auth) -> void:
if _request_code != REQUEST_CODES.NONE:
requests_queue.append([REQUEST_CODES.USER, user_access_token])
return
_request_code = REQUEST_CODES.USER
request(_config.supabaseUrl+_user_endpoint, _header + _bearer, true, HTTPClient.METHOD_GET)
request(_config.supabaseUrl + _user_endpoint, _header + _bearer, true, HTTPClient.METHOD_GET)


func _on_request_completed(result : int, response_code : int, headers : PoolStringArray, body : PoolByteArray) -> void:
var result_body : Dictionary = JSON.parse(body.get_string_from_utf8()).result if body.get_string_from_utf8() else {}
if response_code == 200:
match _request_code:
REQUEST_CODES.SIGNUP:
var signed_user : SupabaseUser = SupabaseUser.new(result_body)
client = signed_user
_bearer[0] = _bearer[0] % _auth
_expires_in = float(result_body.expires_in)
emit_signal("signed_up", signed_user)
REQUEST_CODES.SIGNIN:
var signed_user : SupabaseUser = SupabaseUser.new(result_body)
client = signed_user
_auth = result_body.access_token
_bearer[0] = _bearer[0] % _auth
_expires_in = float(result_body.expires_in)
emit_signal("signed_in", signed_user)
elif response_code in [0, 204]:
match _request_code:
REQUEST_CODES.LOGOUT:
_auth = ""
_bearer[0] = "Authorization: Bearer %s"
_expires_in = 0
client = null
emit_signal("logged_out")
REQUEST_CODES.USER:
emit_signal("got_user")
else:
Expand All @@ -85,4 +115,5 @@ func check_queue() -> void:
match request[0]:
REQUEST_CODES.SIGNUP: sign_up(request[1], request[2])
REQUEST_CODES.SIGNIN: sign_in(request[1], request[2])
REQUEST_CODES.LOGOUT: log_out()
REQUEST_CODES.USER: user(request[1])
3 changes: 3 additions & 0 deletions addons/supabase/Auth/auth_error.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ func _init(dictionary : Dictionary = {}) -> void:
type = str(_error.code)
description = _error.msg
# different body for same api source ???

func _to_string():
return "%s >> %s" % [type, description]
2 changes: 1 addition & 1 deletion addons/supabase/Database/database_error.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ func _init(dictionary : Dictionary = {}) -> void:
### always different behavior ???

func _to_string():
return ""
return "%s >> %s: %s (%s)" % [code, message, details, hint]
6 changes: 3 additions & 3 deletions override.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[supabase/config]
[supabase]

supabaseUrl=""
supabaseKey=""
config/supabaseUrl=""
config/supabaseKey=""