Skip to content

Commit

Permalink
fixes error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Oct 9, 2024
1 parent 364ccaa commit 0797226
Show file tree
Hide file tree
Showing 7 changed files with 383 additions and 10 deletions.
8 changes: 3 additions & 5 deletions supertokens_python/recipe/accountlinking/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ def __init__(self, accounts_already_linked: bool, user: User):
class LinkAccountsRecipeUserIdAlreadyLinkedError:
def __init__(
self,
primary_user_id: Optional[str] = None,
user: Optional[User] = None,
description: Optional[str] = None,
primary_user_id: str,
user: User,
description: str,
):
self.status: Literal[
"RECIPE_USER_ID_ALREADY_LINKED_WITH_ANOTHER_PRIMARY_USER_ID_ERROR"
Expand All @@ -236,14 +236,12 @@ class LinkAccountsAccountInfoAlreadyAssociatedError:
def __init__(
self,
primary_user_id: Optional[str] = None,
user: Optional[User] = None,
description: Optional[str] = None,
):
self.status: Literal[
"ACCOUNT_INFO_ALREADY_ASSOCIATED_WITH_ANOTHER_PRIMARY_USER_ID_ERROR"
] = "ACCOUNT_INFO_ALREADY_ASSOCIATED_WITH_ANOTHER_PRIMARY_USER_ID_ERROR"
self.primary_user_id = primary_user_id
self.user = user
self.description = description


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ async def link_accounts(
):
return LinkAccountsRecipeUserIdAlreadyLinkedError(
primary_user_id=response["primaryUserId"],
user=response["user"],
description=response["description"],
)
elif (
Expand Down
7 changes: 4 additions & 3 deletions supertokens_python/recipe/accountlinking/syncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

from supertokens_python.async_to_sync_wrapper import sync

from ..types import AccountInfoWithRecipeId, User, RecipeUserId
from ..types import AccountInfoWithRecipeId
from supertokens_python.types import RecipeUserId
from supertokens_python.recipe.session import SessionContainer


Expand All @@ -24,7 +25,7 @@ def create_primary_user_id_or_link_accounts(
recipe_user_id: RecipeUserId,
session: Optional[SessionContainer] = None,
user_context: Optional[Dict[str, Any]] = None,
) -> User:
):
from ..asyncio import (
create_primary_user_id_or_link_accounts as async_create_primary_user_id_or_link_accounts,
)
Expand All @@ -40,7 +41,7 @@ def get_primary_user_that_can_be_linked_to_recipe_user_id(
tenant_id: str,
recipe_user_id: RecipeUserId,
user_context: Optional[Dict[str, Any]] = None,
) -> Optional[User]:
):
from ..asyncio import (
get_primary_user_that_can_be_linked_to_recipe_user_id as async_get_primary_user_that_can_be_linked_to_recipe_user_id,
)
Expand Down
2 changes: 1 addition & 1 deletion supertokens_python/recipe/session/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ def anti_csrf_function(
(
overwrite_session_during_sign_in_up
if overwrite_session_during_sign_in_up is not None
else True
else False
),
)

Expand Down
284 changes: 284 additions & 0 deletions tests/test-server/accountlinking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
from flask import Flask, request, jsonify
from supertokens_python import async_to_sync_wrapper, convert_to_recipe_user_id
from supertokens_python.recipe.accountlinking.syncio import can_create_primary_user
from supertokens_python.recipe.accountlinking.recipe import AccountLinkingRecipe
from supertokens_python.recipe.accountlinking.syncio import is_sign_in_allowed
from supertokens_python.recipe.accountlinking.syncio import is_sign_up_allowed
from supertokens_python.recipe.accountlinking.syncio import (
get_primary_user_that_can_be_linked_to_recipe_user_id,
)
from supertokens_python.recipe.accountlinking.syncio import (
create_primary_user_id_or_link_accounts,
)
from supertokens_python.recipe.accountlinking.syncio import unlink_account
from supertokens_python.recipe.accountlinking.syncio import is_email_change_allowed
from supertokens_python.recipe.accountlinking.syncio import (
link_accounts,
create_primary_user,
)
from supertokens_python.recipe.accountlinking.interfaces import (
CanCreatePrimaryUserOkResult,
CanCreatePrimaryUserRecipeUserIdAlreadyLinkedError,
CreatePrimaryUserOkResult,
CreatePrimaryUserRecipeUserIdAlreadyLinkedError,
LinkAccountsAccountInfoAlreadyAssociatedError,
LinkAccountsOkResult,
LinkAccountsRecipeUserIdAlreadyLinkedError,
)
from supertokens_python.recipe.accountlinking.types import AccountInfoWithRecipeId
from supertokens_python.recipe.thirdparty.types import ThirdPartyInfo
from supertokens_python.types import User
from utils import serialize_user # pylint: disable=import-error


def add_accountlinking_routes(app: Flask):
@app.route("/test/accountlinking/createprimaryuser", methods=["POST"]) # type: ignore
def create_primary_user_api(): # type: ignore
try:
assert request.json is not None
recipe_user_id = convert_to_recipe_user_id(request.json["recipeUserId"])
response = create_primary_user(
recipe_user_id, request.json.get("userContext")
)
if isinstance(response, CreatePrimaryUserOkResult):
return jsonify(
{
"status": "OK",
**serialize_user(
response.user, request.headers.get("fdi-version", "")
),
"wasAlreadyAPrimaryUser": response.was_already_a_primary_user,
}
)
elif isinstance(response, CreatePrimaryUserRecipeUserIdAlreadyLinkedError):
return jsonify(
{
"description": response.description,
"primaryUserId": response.primary_user_id,
"status": response.status,
}
)
elif isinstance(response, CreatePrimaryUserRecipeUserIdAlreadyLinkedError):
return jsonify(
{
"description": response.description,
"primaryUserId": response.primary_user_id,
"status": response.status,
}
)
else:
return jsonify(
{
"description": response.description,
"primaryUserId": response.primary_user_id,
"status": response.status,
}
)
except Exception as e:
return jsonify({"error": str(e)}), 500

@app.route("/test/accountlinking/linkaccounts", methods=["POST"]) # type: ignore
def link_accounts_api(): # type: ignore
try:
assert request.json is not None
recipe_user_id = convert_to_recipe_user_id(request.json["recipeUserId"])
response = link_accounts(
recipe_user_id,
request.json["primaryUserId"],
request.json.get("userContext"),
)
if isinstance(response, LinkAccountsOkResult):
return jsonify(
{
"status": "OK",
**serialize_user(
response.user, request.headers.get("fdi-version", "")
),
"accountsAlreadyLinked": response.accounts_already_linked,
}
)
elif isinstance(response, LinkAccountsRecipeUserIdAlreadyLinkedError):
return jsonify(
{
"description": response.description,
"primaryUserId": response.primary_user_id,
"status": response.status,
**serialize_user(
response.user, request.headers.get("fdi-version", "")
),
}
)
elif isinstance(response, LinkAccountsAccountInfoAlreadyAssociatedError):
return jsonify(
{
"description": response.description,
"primaryUserId": response.primary_user_id,
"status": response.status,
}
)
else:
return jsonify(
{
"description": response.description,
"status": response.status,
}
)
except Exception as e:
return jsonify({"error": str(e)}), 500

@app.route("/test/accountlinking/isemailchangeallowed", methods=["POST"]) # type: ignore
def is_email_change_allowed_api(): # type: ignore
try:
assert request.json is not None
recipe_user_id = convert_to_recipe_user_id(request.json["recipeUserId"])
response = is_email_change_allowed(
recipe_user_id,
request.json["newEmail"],
request.json["isVerified"],
request.json["session"],
request.json.get("userContext"),
)
return jsonify(response)
except Exception as e:
return jsonify({"error": str(e)}), 500

@app.route("/test/accountlinking/unlinkaccount", methods=["POST"]) # type: ignore
def unlink_account_api(): # type: ignore
try:
assert request.json is not None
recipe_user_id = convert_to_recipe_user_id(request.json["recipeUserId"])
response = unlink_account(
recipe_user_id,
request.json.get("userContext"),
)
return jsonify(
{
"status": response.status,
"wasRecipeUserDeleted": response.was_recipe_user_deleted,
"wasLinked": response.was_linked,
}
)
except Exception as e:
return jsonify({"error": str(e)}), 500

@app.route("/test/accountlinking/createprimaryuseridorlinkaccounts", methods=["POST"]) # type: ignore
def create_primary_user_id_or_link_accounts_api(): # type: ignore
try:
assert request.json is not None
recipe_user_id = convert_to_recipe_user_id(request.json["recipeUserId"])
response = create_primary_user_id_or_link_accounts(
request.json["tenantId"],
recipe_user_id,
request.json.get("session", None),
request.json.get("userContext", None),
)
return jsonify(response.to_json())
except Exception as e:
return jsonify({"error": str(e)}), 500

@app.route("/test/accountlinking/getprimaryuserthatcanbelinkedtorecipeuserid", methods=["POST"]) # type: ignore
def get_primary_user_that_can_be_linked_to_recipe_user_id_api(): # type: ignore
try:
assert request.json is not None
recipe_user_id = convert_to_recipe_user_id(request.json["recipeUserId"])
response = get_primary_user_that_can_be_linked_to_recipe_user_id(
request.json["tenantId"],
recipe_user_id,
request.json.get("userContext", None),
)
return jsonify(response.to_json() if response else None)
except Exception as e:
return jsonify({"error": str(e)}), 500

@app.route("/test/accountlinking/issignupallowed", methods=["POST"]) # type: ignore
def is_signup_allowed_api(): # type: ignore
try:
assert request.json is not None
response = is_sign_up_allowed(
request.json["tenantId"],
AccountInfoWithRecipeId(
recipe_id=request.json["newUser"]["recipeId"],
email=request.json["newUser"]["email"],
phone_number=request.json["newUser"]["phoneNumber"],
third_party=ThirdPartyInfo(
third_party_user_id=request.json["newUser"]["thirdParty"]["id"],
third_party_id=request.json["newUser"]["thirdParty"][
"thirdPartyId"
],
),
),
request.json["isVerified"],
request.json.get("session", None),
request.json.get("userContext", None),
)
return jsonify(response)
except Exception as e:
return jsonify({"error": str(e)}), 500

@app.route("/test/accountlinking/issigninallowed", methods=["POST"]) # type: ignore
def is_signin_allowed_api(): # type: ignore
try:
assert request.json is not None
recipe_user_id = convert_to_recipe_user_id(request.json["recipeUserId"])
response = is_sign_in_allowed(
request.json["tenantId"],
recipe_user_id,
request.json.get("session", None),
request.json.get("userContext", None),
)
return jsonify(response)
except Exception as e:
return jsonify({"error": str(e)}), 500

@app.route("/test/accountlinking/verifyemailforrecipeuseriflinkedaccountsareverified", methods=["POST"]) # type: ignore
def verify_email_for_recipe_user_if_linked_accounts_are_verified_api(): # type: ignore
try:
assert request.json is not None
recipe_user_id = convert_to_recipe_user_id(request.json["recipeUserId"])
user = User.from_json(request.json["user"])
async_to_sync_wrapper.sync(
AccountLinkingRecipe.get_instance().verify_email_for_recipe_user_if_linked_accounts_are_verified(
user=user,
recipe_user_id=recipe_user_id,
user_context=request.json.get("userContext"),
)
)
return jsonify({})
except Exception as e:
return jsonify({"error": str(e)}), 500

@app.route("/test/accountlinking/cancreateprimaryuser", methods=["POST"]) # type: ignore
def can_create_primary_user_api(): # type: ignore
try:
assert request.json is not None
recipe_user_id = convert_to_recipe_user_id(request.json["recipeUserId"])
response = can_create_primary_user(
recipe_user_id, request.json.get("userContext")
)
if isinstance(response, CanCreatePrimaryUserOkResult):
return jsonify(
{
"status": response.status,
"wasAlreadyAPrimaryUser": response.was_already_a_primary_user,
}
)
elif isinstance(
response, CanCreatePrimaryUserRecipeUserIdAlreadyLinkedError
):
return jsonify(
{
"description": response.description,
"primaryUserId": response.primary_user_id,
"status": response.status,
}
)
else:
return jsonify(
{
"description": response.description,
"status": response.status,
"primaryUserId": response.primary_user_id,
}
)
except Exception as e:
return jsonify({"error": str(e)}), 500
Loading

0 comments on commit 0797226

Please sign in to comment.