|
33 | 33 | from supertokens_python.recipe.thirdpartypasswordless.asyncio import ( |
34 | 34 | get_user_by_id as tppless_get_user_by_id, |
35 | 35 | ) |
| 36 | +from supertokens_python.types import User |
36 | 37 | from supertokens_python.utils import Awaitable |
37 | 38 |
|
38 | 39 | from ...normalised_url_path import NormalisedURLPath |
|
53 | 54 | if TYPE_CHECKING: |
54 | 55 | from .interfaces import APIInterface, RecipeInterface |
55 | 56 |
|
56 | | -from supertokens_python.recipe.dashboard.interfaces import UserWithMetadata |
57 | | -from supertokens_python.types import User |
| 57 | + |
| 58 | +class UserWithMetadata: |
| 59 | + user_id: str |
| 60 | + time_joined: int |
| 61 | + recipe_id: Optional[str] = None |
| 62 | + email: Optional[str] = None |
| 63 | + phone_number: Optional[str] = None |
| 64 | + tp_info: Optional[Dict[str, Any]] = None |
| 65 | + first_name: Optional[str] = None |
| 66 | + last_name: Optional[str] = None |
| 67 | + |
| 68 | + def from_user( |
| 69 | + self, |
| 70 | + user: User, |
| 71 | + first_name: Optional[str] = None, |
| 72 | + last_name: Optional[str] = None, |
| 73 | + ): |
| 74 | + self.first_name = first_name |
| 75 | + self.last_name = last_name |
| 76 | + |
| 77 | + self.user_id = user.user_id |
| 78 | + self.recipe_id = user.recipe_id |
| 79 | + self.time_joined = user.time_joined |
| 80 | + self.email = user.email |
| 81 | + self.phone_number = user.phone_number |
| 82 | + self.tp_info = ( |
| 83 | + None if user.third_party_info is None else user.third_party_info.__dict__ |
| 84 | + ) |
| 85 | + |
| 86 | + return self |
| 87 | + |
| 88 | + def from_dict( |
| 89 | + self, |
| 90 | + user_dict: Dict[str, Any], |
| 91 | + first_name: Optional[str] = None, |
| 92 | + last_name: Optional[str] = None, |
| 93 | + ): |
| 94 | + self.first_name = first_name |
| 95 | + self.last_name = last_name |
| 96 | + |
| 97 | + self.user_id = user_dict["user_id"] |
| 98 | + self.recipe_id = user_dict.get("recipe_id") |
| 99 | + self.time_joined = user_dict["time_joined"] |
| 100 | + self.email = user_dict.get("email") |
| 101 | + self.phone_number = user_dict.get("phone_number") |
| 102 | + self.tp_info = ( |
| 103 | + None |
| 104 | + if user_dict.get("third_party_info") is None |
| 105 | + else user_dict["third_party_info"].__dict__ |
| 106 | + ) |
| 107 | + |
| 108 | + return self |
| 109 | + |
| 110 | + def to_json(self) -> Dict[str, Any]: |
| 111 | + user_json = { |
| 112 | + "id": self.user_id, |
| 113 | + "timeJoined": self.time_joined, |
| 114 | + } |
| 115 | + if self.tp_info is not None: |
| 116 | + user_json["thirdParty"] = { |
| 117 | + "id": self.tp_info["id"], |
| 118 | + "userId": self.tp_info["user_id"], |
| 119 | + } |
| 120 | + if self.phone_number is not None: |
| 121 | + user_json["phoneNumber"] = self.phone_number |
| 122 | + if self.email is not None: |
| 123 | + user_json["email"] = self.email |
| 124 | + if self.first_name is not None: |
| 125 | + user_json["firstName"] = self.first_name |
| 126 | + if self.last_name is not None: |
| 127 | + user_json["lastName"] = self.last_name |
| 128 | + |
| 129 | + if self.recipe_id is not None: |
| 130 | + return { |
| 131 | + "recipeId": self.recipe_id, |
| 132 | + "user": user_json, |
| 133 | + } |
| 134 | + return user_json |
58 | 135 |
|
59 | 136 |
|
60 | 137 | class InputOverrideConfig: |
@@ -161,35 +238,60 @@ def __init__(self, user: UserWithMetadata, recipe: str): |
161 | 238 | self.recipe = recipe |
162 | 239 |
|
163 | 240 |
|
| 241 | +if TYPE_CHECKING: |
| 242 | + from supertokens_python.recipe.emailpassword.types import User as EmailPasswordUser |
| 243 | + from supertokens_python.recipe.passwordless.types import User as PasswordlessUser |
| 244 | + from supertokens_python.recipe.thirdparty.types import User as ThirdPartyUser |
| 245 | + from supertokens_python.recipe.thirdpartyemailpassword.types import ( |
| 246 | + User as ThirdPartyEmailPasswordUser, |
| 247 | + ) |
| 248 | + from supertokens_python.recipe.thirdpartypasswordless.types import ( |
| 249 | + User as ThirdPartyPasswordlessUser, |
| 250 | + ) |
| 251 | + |
| 252 | + GetUserResult = Union[ |
| 253 | + EmailPasswordUser, |
| 254 | + ThirdPartyUser, |
| 255 | + PasswordlessUser, |
| 256 | + None, |
| 257 | + ThirdPartyEmailPasswordUser, |
| 258 | + ThirdPartyPasswordlessUser, |
| 259 | + ] |
| 260 | + |
| 261 | + |
164 | 262 | async def get_user_for_recipe_id( |
165 | 263 | user_id: str, recipe_id: str |
166 | 264 | ) -> Optional[GetUserForRecipeIdResult]: |
167 | 265 | user: Optional[UserWithMetadata] = None |
168 | 266 | recipe: Optional[str] = None |
169 | 267 |
|
170 | 268 | async def update_user_dict( |
171 | | - get_user_func1: Callable[[str], Awaitable[Optional[User]]], |
172 | | - get_user_func2: Callable[[str], Awaitable[Optional[User]]], |
| 269 | + get_user_func1: Callable[[str], Awaitable[GetUserResult]], |
| 270 | + get_user_func2: Callable[[str], Awaitable[GetUserResult]], |
173 | 271 | recipe1: str, |
174 | 272 | recipe2: str, |
175 | 273 | ): |
176 | 274 | nonlocal user, user_id, recipe |
177 | 275 |
|
178 | 276 | try: |
179 | | - matching_user = await get_user_func1(user_id) # type: ignore |
| 277 | + recipe_user = await get_user_func1(user_id) # type: ignore |
180 | 278 |
|
181 | | - if matching_user is not None: |
182 | | - user = UserWithMetadata(matching_user, first_name="", last_name="") |
| 279 | + if recipe_user is not None: |
| 280 | + user = UserWithMetadata().from_dict( |
| 281 | + recipe_user.__dict__, first_name="", last_name="" |
| 282 | + ) |
183 | 283 | recipe = recipe1 |
184 | 284 | except Exception: |
185 | 285 | pass |
186 | 286 |
|
187 | 287 | if user is None: |
188 | 288 | try: |
189 | | - matching_user = await get_user_func2(user_id) |
| 289 | + recipe_user = await get_user_func2(user_id) |
190 | 290 |
|
191 | | - if matching_user is not None: |
192 | | - user = UserWithMetadata(matching_user, first_name="", last_name="") |
| 291 | + if recipe_user is not None: |
| 292 | + user = UserWithMetadata().from_dict( |
| 293 | + recipe_user.__dict__, first_name="", last_name="" |
| 294 | + ) |
193 | 295 | recipe = recipe2 |
194 | 296 | except Exception: |
195 | 297 | pass |
|
0 commit comments