Skip to content

Commit edfff2e

Browse files
committed
Add auth functions, register, login and logout
1 parent 649a0f9 commit edfff2e

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

database/auth.py

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
from db import DB
2-
from db import Client
1+
from database.db import DB
2+
from supabase import Client
33
from gotrue.errors import AuthApiError
4+
from gotrue.types import AuthResponse
5+
from gotrue.types import OAuthResponse
6+
from postgrest import SyncPostgrestClient
47

58

69
# TODO: not complete, initial commit.
@@ -9,24 +12,46 @@ class User():
912
def __init__(self) -> None:
1013
pass
1114

12-
def register(self, email: str, password: str) -> DB:
13-
db = DB()
14-
db.supabase.auth.sign_up({"email": email, "password": password})
15-
return db
15+
def register(self, username: str, email: str, password: str) -> AuthResponse:
16+
supabase = DB().supabase
17+
response = supabase.auth.sign_up({
18+
"email": email,
19+
"password": password,
20+
"options": {
21+
"data": {
22+
"username": username
23+
}
24+
}
25+
})
26+
return response
1627

17-
def login(self, email: str, password: str) -> DB:
28+
def login(self, username: str, password: str) -> AuthResponse:
1829
""" returns login token """
19-
try:
20-
session = DB().supabase.auth.sign_in_with_password(
21-
{"email": email, "password": password})
22-
# TODO: Improve auth error
23-
except AuthApiError as err:
24-
# temporal print
25-
print(err)
26-
else:
27-
db = DB()
28-
db.supabase.postgrest.auth(session.session.access_token)
29-
return db
30+
supabase = DB().supabase
31+
response = supabase.auth.sign_in_with_password(
32+
{"email": username, "password": password})
33+
# response.session.access_token
34+
return response
35+
36+
37+
def login_github(self) -> OAuthResponse:
38+
""" returns login token """
39+
provider = 'github'
40+
supabase = DB().supabase
41+
response = supabase.auth.sign_in_with_oauth({'provider': provider})
42+
# response.session.access_token
43+
return response
44+
45+
def login_google(self) -> OAuthResponse:
46+
""" returns login token """
47+
provider = 'google'
48+
supabase = DB().supabase
49+
response = supabase.auth.sign_in_with_oauth({'provider': provider})
50+
# response.session.access_token
51+
return response
3052

31-
def logout(self, supabase: Client) -> DB:
53+
def logout(self, token: str) -> None:
54+
supabase = DB().supabase
55+
supabase.postgrest.auth(token=token)
3256
supabase.auth.sign_out()
57+

0 commit comments

Comments
 (0)