Skip to content

Commit

Permalink
feat: add create user param to sign in (#75)
Browse files Browse the repository at this point in the history
* feat: add create user param to sign in

ref: supabase/auth#318

* 'Refactored by Sourcery' (#76)

Co-authored-by: Sourcery AI <>

* chore: format code

* chore: format code

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
  • Loading branch information
leynier and sourcery-ai[bot] authored Jan 20, 2022
1 parent c6d4d09 commit 57ec6d8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 56 deletions.
7 changes: 4 additions & 3 deletions gotrue/_async/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ async def send_magic_link_email(
self,
*,
email: str,
create_user: bool,
redirect_to: Optional[str] = None,
) -> None:
"""Sends a magic login link to an email address.
Expand All @@ -269,12 +270,12 @@ async def send_magic_link_email(
if redirect_to:
redirect_to_encoded = encode_uri_component(redirect_to)
query_string = f"?redirect_to={redirect_to_encoded}"
data = {"email": email}
data = {"email": email, "create_user": create_user}
url = f"{self.url}/magiclink{query_string}"
response = await self.http_client.post(url, json=data, headers=headers)
return check_response(response)

async def send_mobile_otp(self, *, phone: str) -> None:
async def send_mobile_otp(self, *, phone: str, create_user: bool) -> None:
"""Sends a mobile OTP via SMS. Will register the account if it doesn't already exist
Parameters
Expand All @@ -288,7 +289,7 @@ async def send_mobile_otp(self, *, phone: str) -> None:
If an error occurs
"""
headers = self.headers
data = {"phone": phone}
data = {"phone": phone, "create_user": create_user}
url = f"{self.url}/otp"
response = await self.http_client.post(url, json=data, headers=headers)
return check_response(response)
Expand Down
54 changes: 29 additions & 25 deletions gotrue/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(
"headers": {**empty_or_default_headers, **headers},
"cookie_options": cookie_options,
}
self.api = api if api else AsyncGoTrueAPI(**args)
self.api = api or AsyncGoTrueAPI(**args)

async def __aenter__(self) -> AsyncGoTrueClient:
return self
Expand Down Expand Up @@ -158,6 +158,7 @@ async def sign_in(
provider: Optional[Provider] = None,
redirect_to: Optional[str] = None,
scopes: Optional[str] = None,
create_user: bool = False,
) -> Optional[Union[Session, str]]:
"""Log in an existing user, or login via a third-party provider.
If email and phone are provided, email will be used and phone will be ignored.
Expand Down Expand Up @@ -204,18 +205,26 @@ async def sign_in(
If an error occurs
"""
await self._remove_session()
if email and not password:
response = await self.api.send_magic_link_email(email=email)
elif email and password:
response = await self._handle_email_sign_in(
email=email,
password=password,
redirect_to=redirect_to,
)
elif phone and not password:
response = await self.api.send_mobile_otp(phone=phone)
elif phone and password:
response = await self._handle_phone_sign_in(phone=phone, password=password)
if email:
if password:
response = await self._handle_email_sign_in(
email=email,
password=password,
redirect_to=redirect_to,
)
else:
response = await self.api.send_magic_link_email(
email=email, create_user=create_user
)
elif phone:
if password:
response = await self._handle_phone_sign_in(
phone=phone, password=password
)
else:
response = await self.api.send_mobile_otp(
phone=phone, create_user=create_user
)
elif refresh_token:
# current_session and current_user will be updated to latest
# on _call_refresh_token using the passed refresh_token
Expand Down Expand Up @@ -290,8 +299,7 @@ async def refresh_session(self) -> Session:
"""
if not self.current_session:
raise ValueError("Not logged in.")
response = await self._call_refresh_token()
return response
return await self._call_refresh_token()

async def update(self, *, attributes: UserAttributes) -> User:
"""Updates user data, if there is a logged in user.
Expand Down Expand Up @@ -523,12 +531,11 @@ async def _handle_provider_sign_in(
scopes: Optional[str],
) -> str:
"""Sign in with provider."""
response = await self.api.get_url_for_provider(
return await self.api.get_url_for_provider(
provider=provider,
redirect_to=redirect_to,
scopes=scopes,
)
return response

async def _recover_common(self) -> Optional[Tuple[Session, int, int]]:
"""Recover common logic"""
Expand Down Expand Up @@ -565,15 +572,12 @@ async def _recover_and_refresh(self) -> None:
if not result:
return
session, expires_at, time_now = result
if expires_at < time_now:
if self.auto_refresh_token and session.refresh_token:
try:
await self._call_refresh_token(refresh_token=session.refresh_token)
except APIError:
await self._remove_session()
else:
if expires_at < time_now and self.auto_refresh_token and session.refresh_token:
try:
await self._call_refresh_token(refresh_token=session.refresh_token)
except APIError:
await self._remove_session()
elif not session or not session.user:
elif expires_at < time_now or not session or not session.user:
await self._remove_session()
else:
await self._save_session(session=session)
Expand Down
7 changes: 4 additions & 3 deletions gotrue/_sync/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ def send_magic_link_email(
self,
*,
email: str,
create_user: bool,
redirect_to: Optional[str] = None,
) -> None:
"""Sends a magic login link to an email address.
Expand All @@ -269,12 +270,12 @@ def send_magic_link_email(
if redirect_to:
redirect_to_encoded = encode_uri_component(redirect_to)
query_string = f"?redirect_to={redirect_to_encoded}"
data = {"email": email}
data = {"email": email, "create_user": create_user}
url = f"{self.url}/magiclink{query_string}"
response = self.http_client.post(url, json=data, headers=headers)
return check_response(response)

def send_mobile_otp(self, *, phone: str) -> None:
def send_mobile_otp(self, *, phone: str, create_user: bool) -> None:
"""Sends a mobile OTP via SMS. Will register the account if it doesn't already exist
Parameters
Expand All @@ -288,7 +289,7 @@ def send_mobile_otp(self, *, phone: str) -> None:
If an error occurs
"""
headers = self.headers
data = {"phone": phone}
data = {"phone": phone, "create_user": create_user}
url = f"{self.url}/otp"
response = self.http_client.post(url, json=data, headers=headers)
return check_response(response)
Expand Down
52 changes: 27 additions & 25 deletions gotrue/_sync/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(
"headers": {**empty_or_default_headers, **headers},
"cookie_options": cookie_options,
}
self.api = api if api else SyncGoTrueAPI(**args)
self.api = api or SyncGoTrueAPI(**args)

def __enter__(self) -> SyncGoTrueClient:
return self
Expand Down Expand Up @@ -158,6 +158,7 @@ def sign_in(
provider: Optional[Provider] = None,
redirect_to: Optional[str] = None,
scopes: Optional[str] = None,
create_user: bool = False,
) -> Optional[Union[Session, str]]:
"""Log in an existing user, or login via a third-party provider.
If email and phone are provided, email will be used and phone will be ignored.
Expand Down Expand Up @@ -204,18 +205,24 @@ def sign_in(
If an error occurs
"""
self._remove_session()
if email and not password:
response = self.api.send_magic_link_email(email=email)
elif email and password:
response = self._handle_email_sign_in(
email=email,
password=password,
redirect_to=redirect_to,
)
elif phone and not password:
response = self.api.send_mobile_otp(phone=phone)
elif phone and password:
response = self._handle_phone_sign_in(phone=phone, password=password)
if email:
if password:
response = self._handle_email_sign_in(
email=email,
password=password,
redirect_to=redirect_to,
)
else:
response = self.api.send_magic_link_email(
email=email, create_user=create_user
)
elif phone:
if password:
response = self._handle_phone_sign_in(phone=phone, password=password)
else:
response = self.api.send_mobile_otp(
phone=phone, create_user=create_user
)
elif refresh_token:
# current_session and current_user will be updated to latest
# on _call_refresh_token using the passed refresh_token
Expand Down Expand Up @@ -290,8 +297,7 @@ def refresh_session(self) -> Session:
"""
if not self.current_session:
raise ValueError("Not logged in.")
response = self._call_refresh_token()
return response
return self._call_refresh_token()

def update(self, *, attributes: UserAttributes) -> User:
"""Updates user data, if there is a logged in user.
Expand Down Expand Up @@ -523,12 +529,11 @@ def _handle_provider_sign_in(
scopes: Optional[str],
) -> str:
"""Sign in with provider."""
response = self.api.get_url_for_provider(
return self.api.get_url_for_provider(
provider=provider,
redirect_to=redirect_to,
scopes=scopes,
)
return response

def _recover_common(self) -> Optional[Tuple[Session, int, int]]:
"""Recover common logic"""
Expand Down Expand Up @@ -565,15 +570,12 @@ def _recover_and_refresh(self) -> None:
if not result:
return
session, expires_at, time_now = result
if expires_at < time_now:
if self.auto_refresh_token and session.refresh_token:
try:
self._call_refresh_token(refresh_token=session.refresh_token)
except APIError:
self._remove_session()
else:
if expires_at < time_now and self.auto_refresh_token and session.refresh_token:
try:
self._call_refresh_token(refresh_token=session.refresh_token)
except APIError:
self._remove_session()
elif not session or not session.user:
elif expires_at < time_now or not session or not session.user:
self._remove_session()
else:
self._save_session(session=session)
Expand Down

0 comments on commit 57ec6d8

Please sign in to comment.