Skip to content

Commit 3443209

Browse files
the connect widget is now fetching its own url
1 parent 20f112b commit 3443209

2 files changed

Lines changed: 101 additions & 36 deletions

File tree

example/src/app/connect.tsx

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import React from "react"
2-
import { StyleSheet, Platform } from "react-native"
1+
import React, { useEffect, useState } from "react"
2+
import { StyleSheet } from "react-native"
33
import { SafeAreaView } from "react-native-safe-area-context"
44
import * as Linking from "expo-linking"
55

66
import { ConnectWidget } from "@mxenabled/react-native-widget-sdk"
7-
8-
const baseUrl = Platform.OS === "android" ? "http://10.0.2.2:8089" : "http://localhost:8089"
9-
const proxy = `${baseUrl}/user/widget_urls`
7+
import { fetchWidgetUrl } from "../shared/api"
108

119
const styles = StyleSheet.create({
1210
page: {
@@ -18,39 +16,48 @@ const styles = StyleSheet.create({
1816
export default function Connect() {
1917
const clientRedirectUrl = Linking.createURL("connect")
2018

19+
const [url, setUrl] = useState<string | null>(null)
20+
21+
useEffect(() => {
22+
fetchWidgetUrl({ clientRedirectUrl, widgetType: "connect_widget" }).then((url) => {
23+
setUrl(url)
24+
})
25+
}, [clientRedirectUrl])
26+
2127
return (
2228
<SafeAreaView style={styles.page}>
23-
<ConnectWidget
24-
url="junk"
25-
clientRedirectUrl={clientRedirectUrl}
26-
onSsoUrlLoadError={(error) => {
27-
console.error(`SSO URL load error: ${error}`)
28-
}}
29-
onMessage={(url) => {
30-
console.log(`Got a message: ${url}`)
31-
}}
32-
onInvalidMessageError={(url, _error) => {
33-
console.log(`Got an unknown message: ${url}`)
34-
}}
35-
onLoad={(_payload) => {
36-
console.log("Widget is loading")
37-
}}
38-
onLoaded={(_payload) => {
39-
console.log("Widget has loaded")
40-
}}
41-
onMemberConnected={(payload) => {
42-
console.log(`Member connected with payload: ${JSON.stringify(payload)}`)
43-
}}
44-
onOAuthRequested={(payload) => {
45-
console.log(`OAuth requested with URL: ${payload.url}`)
46-
}}
47-
onStepChange={(payload) => {
48-
console.log(`Moving from ${payload.previous} to ${payload.current}`)
49-
}}
50-
onSelectedInstitution={(payload) => {
51-
console.log(`Selecting ${payload.name}`)
52-
}}
53-
/>
29+
{url && (
30+
<ConnectWidget
31+
url={url}
32+
onSsoUrlLoadError={(error) => {
33+
console.error(`SSO URL load error: ${error}`)
34+
}}
35+
onMessage={(url) => {
36+
console.log(`Got a message: ${url}`)
37+
}}
38+
onInvalidMessageError={(url, _error) => {
39+
console.log(`Got an unknown message: ${url}`)
40+
}}
41+
onLoad={(_payload) => {
42+
console.log("Widget is loading")
43+
}}
44+
onLoaded={(_payload) => {
45+
console.log("Widget has loaded")
46+
}}
47+
onMemberConnected={(payload) => {
48+
console.log(`Member connected with payload: ${JSON.stringify(payload)}`)
49+
}}
50+
onOAuthRequested={(payload) => {
51+
console.log(`OAuth requested with URL: ${payload.url}`)
52+
}}
53+
onStepChange={(payload) => {
54+
console.log(`Moving from ${payload.previous} to ${payload.current}`)
55+
}}
56+
onSelectedInstitution={(payload) => {
57+
console.log(`Selecting ${payload.name}`)
58+
}}
59+
/>
60+
)}
5461
</SafeAreaView>
5562
)
5663
}

example/src/shared/api.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Platform } from "react-native"
2+
3+
const baseUrl = Platform.OS === "android" ? "http://10.0.2.2:8089" : "http://localhost:8089"
4+
const proxyUrl = `${baseUrl}/user/widget_urls`
5+
6+
interface FetchWidgetUrlProps {
7+
clientRedirectUrl: string
8+
widgetType: string
9+
}
10+
11+
export function buildWidgetConfiguration({ clientRedirectUrl, widgetType }: FetchWidgetUrlProps) {
12+
return {
13+
...(widgetType === "connect_widget"
14+
? { data_request: { products: ["identity_verification"] } }
15+
: {}),
16+
client_redirect_url: clientRedirectUrl,
17+
is_mobile_webview: true,
18+
ui_message_version: 4,
19+
widget_type: widgetType,
20+
}
21+
}
22+
23+
interface WidgetUrlResponse {
24+
widget_url: {
25+
url: string
26+
}
27+
}
28+
29+
export const fetchWidgetUrl = async (props: FetchWidgetUrlProps) => {
30+
const headers: Record<string, string> = {
31+
"Accept-Version": "v20250224",
32+
"Content-Type": "application/json",
33+
}
34+
35+
const method = "POST"
36+
const body = JSON.stringify({
37+
widget_url: buildWidgetConfiguration(props),
38+
})
39+
40+
try {
41+
const response = await fetch(proxyUrl, {
42+
body,
43+
headers,
44+
method,
45+
})
46+
47+
if (!response.ok) {
48+
throw new Error(`Failed to fetch widget URL: ${response.status} ${response.statusText}`)
49+
}
50+
51+
const data: WidgetUrlResponse = await response.json()
52+
53+
return data.widget_url.url
54+
} catch (error) {
55+
console.error("Error fetching widget URL:", error)
56+
throw error
57+
}
58+
}

0 commit comments

Comments
 (0)