Skip to content

Commit

Permalink
Add python code
Browse files Browse the repository at this point in the history
  • Loading branch information
nkshah2 committed Oct 4, 2023
1 parent 6722fb1 commit 2f4f948
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
56 changes: 56 additions & 0 deletions v2/thirdparty/custom-ui/thirdparty-login.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ import (
func main() {
^{codeImportRecipeName}.Init(^{goModelNameForInit}.TypeInput{
Override: &^{goModelName}.OverrideStruct{
// highlight-start
APIs: func(originalImplementation ^{goModelName}.APIInterface) ^{goModelName}.APIInterface {
originalAppleRedirectPost := *originalImplementation.AppleRedirectHandlerPOST

Expand Down Expand Up @@ -511,6 +512,7 @@ func main() {
return originalImplementation
},
},
// highlight-end
^{goSignInUpFeatureStart}
Providers: []tpmodels.ProviderInput{
// ...
Expand All @@ -524,6 +526,60 @@ func main() {

<TabItem value="python">

```python
from supertokens_python.recipe import ^{codeImportRecipeName}
from supertokens_python.recipe.^{codeImportRecipeName}.interfaces import APIInterface, APIOptions
from typing import Union, Dict, Any
from supertokens_python.recipe.thirdparty.provider import Provider, RedirectUriInfo

# highlight-start
def override_thirdparty_apis(original_implementation: APIInterface):
original_apple_redirect_post = original_implementation.apple_redirect_handler_post

async def apple_redirect_handler_post(
form_post_info: Dict[str, Any],
api_options: APIOptions,
user_context: Dict[str, Any]
):
# form_post_info contains all the query params attached by Apple
state = form_post_info["state"]

# The web SDKs add a default state
if state is None:
query_items = []

for key, value in form_post_info.items():
query_items.append(f"{key}={value}")

query_string = "&".join(query_items)

# Refer to the README of sign_in_with_apple to understand what this url is
redirect_url = f"intent://callback?${query_string}#Intent;package=YOUR.PACKAGE.IDENTIFIER;scheme=signinwithapple;end"

api_options.response.set_header("Location", redirect_url)
api_options.response.set_status_code(303)
api_options.response.set_html_content("")
else:
return original_apple_redirect_post(form_post_info, api_options, user_context)

original_implementation.apple_redirect_handler_post = apple_redirect_handler_post
return original_implementation
# highlight-end

^{codeImportRecipeName}.init(
# highlight-start
override=^{codeImportRecipeName}.InputOverrideConfig(
apis=override_thirdparty_apis
),
# highlight-end
^{pythonSignInUpFeatureStart}
providers=[
# ...
]
^{pythonSignInUpFeatureEnd}
)
```

</TabItem>

</BackendSDKTabs>
Expand Down
56 changes: 56 additions & 0 deletions v2/thirdpartyemailpassword/custom-ui/thirdparty-login.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ import (
func main() {
^{codeImportRecipeName}.Init(^{goModelNameForInit}.TypeInput{
Override: &^{goModelName}.OverrideStruct{
// highlight-start
APIs: func(originalImplementation ^{goModelName}.APIInterface) ^{goModelName}.APIInterface {
originalAppleRedirectPost := *originalImplementation.AppleRedirectHandlerPOST

Expand Down Expand Up @@ -514,6 +515,7 @@ func main() {
return originalImplementation
},
},
// highlight-end
^{goSignInUpFeatureStart}
Providers: []tpmodels.ProviderInput{
// ...
Expand All @@ -527,6 +529,60 @@ func main() {

<TabItem value="python">

```python
from supertokens_python.recipe import ^{codeImportRecipeName}
from supertokens_python.recipe.^{codeImportRecipeName}.interfaces import APIInterface, APIOptions
from typing import Union, Dict, Any
from supertokens_python.recipe.thirdparty.provider import Provider, RedirectUriInfo

# highlight-start
def override_thirdparty_apis(original_implementation: APIInterface):
original_apple_redirect_post = original_implementation.apple_redirect_handler_post

async def apple_redirect_handler_post(
form_post_info: Dict[str, Any],
api_options: APIOptions,
user_context: Dict[str, Any]
):
# form_post_info contains all the query params attached by Apple
state = form_post_info["state"]

# The web SDKs add a default state
if state is None:
query_items = []

for key, value in form_post_info.items():
query_items.append(f"{key}={value}")

query_string = "&".join(query_items)

# Refer to the README of sign_in_with_apple to understand what this url is
redirect_url = f"intent://callback?${query_string}#Intent;package=YOUR.PACKAGE.IDENTIFIER;scheme=signinwithapple;end"

api_options.response.set_header("Location", redirect_url)
api_options.response.set_status_code(303)
api_options.response.set_html_content("")
else:
return original_apple_redirect_post(form_post_info, api_options, user_context)

original_implementation.apple_redirect_handler_post = apple_redirect_handler_post
return original_implementation
# highlight-end

^{codeImportRecipeName}.init(
# highlight-start
override=^{codeImportRecipeName}.InputOverrideConfig(
apis=override_thirdparty_apis
),
# highlight-end
^{pythonSignInUpFeatureStart}
providers=[
# ...
]
^{pythonSignInUpFeatureEnd}
)
```

</TabItem>

</BackendSDKTabs>
Expand Down
56 changes: 56 additions & 0 deletions v2/thirdpartypasswordless/custom-ui/thirdparty-login.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ import (
func main() {
^{codeImportRecipeName}.Init(^{goModelNameForInit}.TypeInput{
Override: &^{goModelName}.OverrideStruct{
// highlight-start
APIs: func(originalImplementation ^{goModelName}.APIInterface) ^{goModelName}.APIInterface {
originalAppleRedirectPost := *originalImplementation.AppleRedirectHandlerPOST

Expand Down Expand Up @@ -514,6 +515,7 @@ func main() {
return originalImplementation
},
},
// highlight-end
^{goSignInUpFeatureStart}
Providers: []tpmodels.ProviderInput{
// ...
Expand All @@ -527,6 +529,60 @@ func main() {

<TabItem value="python">

```python
from supertokens_python.recipe import ^{codeImportRecipeName}
from supertokens_python.recipe.^{codeImportRecipeName}.interfaces import APIInterface, APIOptions
from typing import Union, Dict, Any
from supertokens_python.recipe.thirdparty.provider import Provider, RedirectUriInfo

# highlight-start
def override_thirdparty_apis(original_implementation: APIInterface):
original_apple_redirect_post = original_implementation.apple_redirect_handler_post

async def apple_redirect_handler_post(
form_post_info: Dict[str, Any],
api_options: APIOptions,
user_context: Dict[str, Any]
):
# form_post_info contains all the query params attached by Apple
state = form_post_info["state"]

# The web SDKs add a default state
if state is None:
query_items = []

for key, value in form_post_info.items():
query_items.append(f"{key}={value}")

query_string = "&".join(query_items)

# Refer to the README of sign_in_with_apple to understand what this url is
redirect_url = f"intent://callback?${query_string}#Intent;package=YOUR.PACKAGE.IDENTIFIER;scheme=signinwithapple;end"

api_options.response.set_header("Location", redirect_url)
api_options.response.set_status_code(303)
api_options.response.set_html_content("")
else:
return original_apple_redirect_post(form_post_info, api_options, user_context)

original_implementation.apple_redirect_handler_post = apple_redirect_handler_post
return original_implementation
# highlight-end

^{codeImportRecipeName}.init(
# highlight-start
override=^{codeImportRecipeName}.InputOverrideConfig(
apis=override_thirdparty_apis
),
# highlight-end
^{pythonSignInUpFeatureStart}
providers=[
# ...
]
^{pythonSignInUpFeatureEnd}
)
```

</TabItem>

</BackendSDKTabs>
Expand Down

0 comments on commit 2f4f948

Please sign in to comment.