@@ -10,6 +10,7 @@ hide_title: true
10
10
import AccountLinkingPaidBanner from ' ../../../community/reusableMD/accountlinking/AccountLinkingPaidBanner.mdx'
11
11
import BackendSDKTabs from " /src/components/tabs/BackendSDKTabs" ;
12
12
import TabItem from ' @theme/TabItem' ;
13
+ import PythonSyncAsyncSubTabs from " /src/components/tabs/PythonSyncAsyncSubTabs" ;
13
14
14
15
<AccountLinkingPaidBanner />
15
16
@@ -43,13 +44,7 @@ supertokens.init({
43
44
},
44
45
recipeList: [
45
46
// highlight-start
46
- AccountLinking .init ({
47
- shouldDoAutomaticAccountLinking : async (newAccountInfo : AccountInfoWithRecipeId & { recipeUserId? : RecipeUserId }, user : User | undefined , session : SessionContainerInterface | undefined , tenantId : string , userContext : any ) => {
48
- return {
49
- shouldAutomaticallyLink: false
50
- }
51
- }
52
- })
47
+ AccountLinking .init ()
53
48
// highlight-end
54
49
]
55
50
});
@@ -65,9 +60,26 @@ Coming Soon
65
60
</TabItem >
66
61
<TabItem value = " python" >
67
62
68
- :::note
69
- Coming Soon
70
- :::
63
+ ``` python
64
+ from supertokens_python import init, InputAppInfo, SupertokensConfig
65
+ from supertokens_python.recipe import accountlinking
66
+
67
+
68
+ init(
69
+ app_info = InputAppInfo(
70
+ app_name = " ..." ,
71
+ api_domain = " ..." ,
72
+ website_domain = " ..." ,
73
+ ),
74
+ supertokens_config = SupertokensConfig(
75
+ connection_uri = " ..." ,
76
+ ),
77
+ framework = ' ...' , # type: ignore
78
+ recipe_list = [
79
+ accountlinking.init()
80
+ ],
81
+ )
82
+ ```
71
83
72
84
</TabItem >
73
85
</BackendSDKTabs >
@@ -116,9 +128,62 @@ Coming Soon
116
128
</TabItem >
117
129
<TabItem value = " python" >
118
130
119
- :::note
120
- Coming Soon
121
- :::
131
+ <PythonSyncAsyncSubTabs >
132
+ <TabItem value = " asyncio" >
133
+
134
+ ``` python
135
+ from supertokens_python.recipe.accountlinking.asyncio import create_primary_user
136
+ from supertokens_python.types import RecipeUserId
137
+
138
+ async def make_user_primary (recipe_user_id : RecipeUserId):
139
+ response = await create_primary_user(recipe_user_id)
140
+ if response.status == " OK" :
141
+ if response.was_already_a_primary_user:
142
+ # The input user was already a primary user and accounts can be linked to it.
143
+ pass
144
+ else :
145
+ # User is now primary and accounts can be linked to it.
146
+ pass
147
+ modified_user = response.user
148
+ print (modified_user.is_primary_user) # will print True
149
+ elif response.status == " ACCOUNT_INFO_ALREADY_ASSOCIATED_WITH_ANOTHER_PRIMARY_USER_ID_ERROR" :
150
+ # This happens if there already exists another primary user with the same email or phone number
151
+ # in at least one of the tenants that this user belongs to.
152
+ pass
153
+ elif response.status == " RECIPE_USER_ID_ALREADY_LINKED_WITH_PRIMARY_USER_ID_ERROR" :
154
+ # This happens if this user is already linked to another primary user.
155
+ pass
156
+ ```
157
+
158
+ </TabItem >
159
+ <TabItem value = " syncio" >
160
+
161
+ ``` python
162
+ from supertokens_python.recipe.accountlinking.syncio import create_primary_user
163
+ from supertokens_python.types import RecipeUserId
164
+
165
+ def make_user_primary (recipe_user_id : RecipeUserId):
166
+ response = create_primary_user(recipe_user_id)
167
+ if response.status == " OK" :
168
+ if response.was_already_a_primary_user:
169
+ # The input user was already a primary user and accounts can be linked to it.
170
+ pass
171
+ else :
172
+ # User is now primary and accounts can be linked to it.
173
+ pass
174
+ modified_user = response.user
175
+ print (modified_user.is_primary_user) # will print True
176
+ elif response.status == " ACCOUNT_INFO_ALREADY_ASSOCIATED_WITH_ANOTHER_PRIMARY_USER_ID_ERROR" :
177
+ # This happens if there already exists another primary user with the same email or phone number
178
+ # in at least one of the tenants that this user belongs to.
179
+ pass
180
+ elif response.status == " RECIPE_USER_ID_ALREADY_LINKED_WITH_PRIMARY_USER_ID_ERROR" :
181
+ # This happens if this user is already linked to another primary user.
182
+ pass
183
+ ```
184
+
185
+ </TabItem >
186
+ </PythonSyncAsyncSubTabs >
122
187
123
188
</TabItem >
124
189
</BackendSDKTabs >
@@ -168,9 +233,72 @@ Coming Soon
168
233
</TabItem >
169
234
<TabItem value = " python" >
170
235
171
- :::note
172
- Coming Soon
173
- :::
236
+ <PythonSyncAsyncSubTabs >
237
+ <TabItem value = " asyncio" >
238
+
239
+ ``` python
240
+ from supertokens_python.recipe.accountlinking.asyncio import link_accounts
241
+ from supertokens_python.types import RecipeUserId
242
+
243
+ async def link_accounts_helper (primary_user_id : str , recipe_user_id : RecipeUserId):
244
+ response = await link_accounts(recipe_user_id, primary_user_id)
245
+ if response.status == " OK" :
246
+ if response.accounts_already_linked:
247
+ # The input users were already linked
248
+ pass
249
+ else :
250
+ # The two users are now linked
251
+ pass
252
+ modified_user = response.user
253
+ print (modified_user.login_methods) # this will now contain the login method of the recipeUserId as well.
254
+ elif response.status == " ACCOUNT_INFO_ALREADY_ASSOCIATED_WITH_ANOTHER_PRIMARY_USER_ID_ERROR" :
255
+ # This happens if there already exists another primary user with the same email or phone number
256
+ # as the recipeUserId's account.
257
+ pass
258
+ elif response.status == " INPUT_USER_IS_NOT_A_PRIMARY_USER" :
259
+ # This happens if the input primaryUserId is not actually a primary user ID.
260
+ # You can call create_primary_user and call link_accounts again
261
+ pass
262
+ elif response.status == " RECIPE_USER_ID_ALREADY_LINKED_WITH_ANOTHER_PRIMARY_USER_ID_ERROR" :
263
+ # This happens if the input recipe user ID is already linked to another primary user.
264
+ # You can call unlink_accounts on the recipe user ID and then try linking again.
265
+ pass
266
+ ```
267
+
268
+ </TabItem >
269
+ <TabItem value = " syncio" >
270
+
271
+ ``` python
272
+ from supertokens_python.recipe.accountlinking.syncio import link_accounts
273
+ from supertokens_python.types import RecipeUserId
274
+
275
+ def link_accounts_helper (primary_user_id : str , recipe_user_id : RecipeUserId):
276
+ response = link_accounts(recipe_user_id, primary_user_id)
277
+ if response.status == " OK" :
278
+ if response.accounts_already_linked:
279
+ # The input users were already linked
280
+ pass
281
+ else :
282
+ # The two users are now linked
283
+ pass
284
+ modified_user = response.user
285
+ print (modified_user.login_methods) # this will now contain the login method of the recipeUserId as well.
286
+ elif response.status == " ACCOUNT_INFO_ALREADY_ASSOCIATED_WITH_ANOTHER_PRIMARY_USER_ID_ERROR" :
287
+ # This happens if there already exists another primary user with the same email or phone number
288
+ # as the recipeUserId's account.
289
+ pass
290
+ elif response.status == " INPUT_USER_IS_NOT_A_PRIMARY_USER" :
291
+ # This happens if the input primaryUserId is not actually a primary user ID.
292
+ # You can call create_primary_user and call link_accounts again
293
+ pass
294
+ elif response.status == " RECIPE_USER_ID_ALREADY_LINKED_WITH_ANOTHER_PRIMARY_USER_ID_ERROR" :
295
+ # This happens if the input recipe user ID is already linked to another primary user.
296
+ # You can call unlink_accounts on the recipe user ID and then try linking again.
297
+ pass
298
+ ```
299
+
300
+ </TabItem >
301
+ </PythonSyncAsyncSubTabs >
174
302
175
303
</TabItem >
176
304
</BackendSDKTabs >
@@ -221,9 +349,68 @@ Coming Soon
221
349
</TabItem >
222
350
<TabItem value = " python" >
223
351
224
- :::note
225
- Coming Soon
226
- :::
352
+ <PythonSyncAsyncSubTabs >
353
+ <TabItem value = " asyncio" >
354
+
355
+ ``` python
356
+ from supertokens_python.recipe.accountlinking.asyncio import unlink_account
357
+ from supertokens_python.types import RecipeUserId
358
+
359
+ async def unlink_account_helper (recipe_user_id : RecipeUserId):
360
+ response = await unlink_account(recipe_user_id)
361
+ if response.was_linked:
362
+ # This means that we unlinked the account from its primary user ID
363
+ pass
364
+ else :
365
+ # This means that the user was never linked in the first place
366
+ pass
367
+
368
+ if response.was_recipe_user_deleted:
369
+ # This is true if we call unlink_account on the recipe user ID of the primary user ID user.
370
+ # We delete this user because if we don't and we call get_user_by_id() on this user's ID, SuperTokens
371
+ # won't know which user info to return - the primary user, or the recipe user.
372
+ # Note that even though the recipe user is deleted, the session, metadata, roles etc for this
373
+ # primary user is still intact, and calling get_user_by_id(primary_user_id) will still return
374
+ # the user object with the other login methods.
375
+ pass
376
+ else :
377
+ # There now exists a user account which is not a primary user, with the recipe_user_id equal to the
378
+ # input recipe_user_id.
379
+ pass
380
+ ```
381
+
382
+ </TabItem >
383
+ <TabItem value = " syncio" >
384
+
385
+ ``` python
386
+ from supertokens_python.recipe.accountlinking.syncio import unlink_account
387
+ from supertokens_python.types import RecipeUserId
388
+
389
+ def unlink_account_helper (recipe_user_id : RecipeUserId):
390
+ response = unlink_account(recipe_user_id)
391
+ if response.was_linked:
392
+ # This means that we unlinked the account from its primary user ID
393
+ pass
394
+ else :
395
+ # This means that the user was never linked in the first place
396
+ pass
397
+
398
+ if response.was_recipe_user_deleted:
399
+ # This is true if we call unlink_account on the recipe user ID of the primary user ID user.
400
+ # We delete this user because if we don't and we call get_user_by_id() on this user's ID, SuperTokens
401
+ # won't know which user info to return - the primary user, or the recipe user.
402
+ # Note that even though the recipe user is deleted, the session, metadata, roles etc for this
403
+ # primary user is still intact, and calling get_user_by_id(primary_user_id) will still return
404
+ # the user object with the other login methods.
405
+ pass
406
+ else :
407
+ # There now exists a user account which is not a primary user, with the recipe_user_id equal to the
408
+ # input recipe_user_id.
409
+ pass
410
+ ```
411
+
412
+ </TabItem >
413
+ </PythonSyncAsyncSubTabs >
227
414
228
415
</TabItem >
229
416
</BackendSDKTabs >
@@ -252,9 +439,12 @@ Coming Soon
252
439
</TabItem >
253
440
<TabItem value = " python" >
254
441
255
- :::note
256
- Coming Soon
257
- :::
442
+ ``` python
443
+ from supertokens_python.types import RecipeUserId
444
+
445
+ user_id = " some_user_id" ;
446
+ recipe_user_id = RecipeUserId(user_id)
447
+ ```
258
448
259
449
</TabItem >
260
450
</BackendSDKTabs >
0 commit comments