Skip to content

Commit

Permalink
Merge pull request #753 from supertokens/feat/network-interceptor-hook
Browse files Browse the repository at this point in the history
feat: adds docs for `networkInterceptor`
  • Loading branch information
rishabhpoddar authored Nov 28, 2023
2 parents a56d486 + 7ae2ac8 commit dd7cbc7
Show file tree
Hide file tree
Showing 13 changed files with 797 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
---
id: backend-sdk-core-interceptor
title: Backend SDK Core Interceptor
hide_title: true
---

import BackendSDKTabs from "/src/components/tabs/BackendSDKTabs"
import TabItem from '@theme/TabItem';

<!-- COPY DOCS -->
<!-- ./emailpassword/advanced-customizations/backend-sdk-core-interceptor.mdx -->

# Backend SDK Core Interceptor

:::important
This feature is only available for SDKs versions:
- NodeJS >= `v16.5.0`
- Python >= `v0.16.8`
- GoLang >= `v0.6.6`
:::

This hook can be used to intercept all outgoing requests from the backend SDK to the core. The request can be captured and modified before it is sent to the core.

Users can modify the HTTP method, query params, headers and body of the request.

## Example Use

<BackendSDKTabs>
<TabItem value="nodejs">

```tsx
import { HttpRequest } from "supertokens-node/types";
import SuperTokens from "supertokens-node";

SuperTokens.init({
supertokens: {
connectionURI: "...",
apiKey: "...",
// highlight-start
networkInterceptor: (request: HttpRequest, userContext: any) => {
console.log("http request to core: ", request)
// this can also be used to return a modified request object.
return request;
},
// highlight-end
},
appInfo: {
apiDomain: "...",
appName: "...",
websiteDomain: "...",
},
recipeList: [
// ...
],
});
```

</TabItem>
<TabItem value="go">

```go
import (
"log"
"net/http"

"github.com/supertokens/supertokens-golang/supertokens"
)

func main() {
supertokens.Init(supertokens.TypeInput{
Supertokens: &supertokens.ConnectionInfo{
ConnectionURI: "...",
APIKey: "...",
// highlight-start
NetworkInterceptor: func(request *http.Request, context supertokens.UserContext) *http.Request {
log.Print("http request to core: %+v", request)
return request
},
// highlight-end
},
AppInfo: supertokens.AppInfo{
AppName: "...",
APIDomain: "...",
WebsiteDomain: "...",
},
RecipeList: []supertokens.Recipe{/*...*/},
})
}
```

</TabItem>
<TabItem value="python">

```python
from typing import Dict, Any, Optional
from supertokens_python import init, InputAppInfo, SupertokensConfig

# highlight-start
def intercept(
url: str,
method: str,
headers: Dict[str, Any],
params: Optional[Dict[str, Any]],
body: Optional[Dict[str, Any]],
user_context: Optional[Dict[str, Any]],
):
print("http request to core: ", url, method, headers, params, body)
return url, method, headers, params, body
# highlight-end

init(
app_info=InputAppInfo(
app_name="...",
api_domain="...",
website_domain="...",
),
supertokens_config=SupertokensConfig(
connection_uri="...",
api_key="...",
# highlight-next-line
network_interceptor=intercept,
),
framework="django", # works with other frameworks as well
recipe_list=[
# ...
],
)
```

</TabItem>
</BackendSDKTabs>
1 change: 1 addition & 0 deletions v2/emailpassword/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ module.exports = {
],
},
"advanced-customizations/user-context",
"advanced-customizations/backend-sdk-core-interceptor"
],
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
---
id: backend-sdk-core-interceptor
title: Backend SDK Core Interceptor
hide_title: true
---

import BackendSDKTabs from "/src/components/tabs/BackendSDKTabs"
import TabItem from '@theme/TabItem';

<!-- COPY DOCS -->
<!-- ./emailpassword/advanced-customizations/backend-sdk-core-interceptor.mdx -->

# Backend SDK Core Interceptor

:::important
This feature is only available for SDKs versions:
- NodeJS >= `v16.5.0`
- Python >= `v0.16.8`
- GoLang >= `v0.6.6`
:::

This hook can be used to intercept all outgoing requests from the backend SDK to the core. The request can be captured and modified before it is sent to the core.

Users can modify the HTTP method, query params, headers and body of the request.

## Example Use

<BackendSDKTabs>
<TabItem value="nodejs">

```tsx
import { HttpRequest } from "supertokens-node/types";
import SuperTokens from "supertokens-node";

SuperTokens.init({
supertokens: {
connectionURI: "...",
apiKey: "...",
// highlight-start
networkInterceptor: (request: HttpRequest, userContext: any) => {
console.log("http request to core: ", request)
// this can also be used to return a modified request object.
return request;
},
// highlight-end
},
appInfo: {
apiDomain: "...",
appName: "...",
websiteDomain: "...",
},
recipeList: [
// ...
],
});
```

</TabItem>
<TabItem value="go">

```go
import (
"log"
"net/http"

"github.com/supertokens/supertokens-golang/supertokens"
)

func main() {
supertokens.Init(supertokens.TypeInput{
Supertokens: &supertokens.ConnectionInfo{
ConnectionURI: "...",
APIKey: "...",
// highlight-start
NetworkInterceptor: func(request *http.Request, context supertokens.UserContext) *http.Request {
log.Print("http request to core: %+v", request)
return request
},
// highlight-end
},
AppInfo: supertokens.AppInfo{
AppName: "...",
APIDomain: "...",
WebsiteDomain: "...",
},
RecipeList: []supertokens.Recipe{/*...*/},
})
}
```

</TabItem>
<TabItem value="python">

```python
from typing import Dict, Any, Optional
from supertokens_python import init, InputAppInfo, SupertokensConfig

# highlight-start
def intercept(
url: str,
method: str,
headers: Dict[str, Any],
params: Optional[Dict[str, Any]],
body: Optional[Dict[str, Any]],
user_context: Optional[Dict[str, Any]],
):
print("http request to core: ", url, method, headers, params, body)
return url, method, headers, params, body
# highlight-end

init(
app_info=InputAppInfo(
app_name="...",
api_domain="...",
website_domain="...",
),
supertokens_config=SupertokensConfig(
connection_uri="...",
api_key="...",
# highlight-next-line
network_interceptor=intercept,
),
framework="django", # works with other frameworks as well
recipe_list=[
# ...
],
)
```

</TabItem>
</BackendSDKTabs>
1 change: 1 addition & 0 deletions v2/passwordless/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ module.exports = {
],
},
"advanced-customizations/user-context",
"advanced-customizations/backend-sdk-core-interceptor"
],
},
{
Expand Down
Loading

0 comments on commit dd7cbc7

Please sign in to comment.