Skip to content

Commit 451ae28

Browse files
simplifying code
1 parent b6437f8 commit 451ae28

8 files changed

Lines changed: 47 additions & 133 deletions

File tree

example/app/connect.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export default function Connect() {
4141
onMemberConnected={(payload) => {
4242
console.log(`Member connected with payload: ${JSON.stringify(payload)}`)
4343
}}
44+
onOAuthRequested={(payload) => {
45+
console.log(`OAuth requested with URL: ${payload.url}`)
46+
}}
4447
onStepChange={(payload) => {
4548
console.log(`Moving from ${payload.previous} to ${payload.current}`)
4649
}}

package-lock.json

Lines changed: 14 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"@mxenabled/widget-post-message-definitions": "^1.4.0",
3535
"expo-web-browser": "^13.0.0",
3636
"react-native-base64": "^0.2.1",
37-
"url": "^0.11.0"
37+
"url": "^0.11.4"
3838
},
3939
"peerDependencies": {
4040
"react": "*",

src/components/ConnectWidgets.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {
22
dispatchConnectLocationChangeEvent,
33
ConnectPostMessageCallbackProps,
4+
ConnectOAuthRequestedPayload,
45
} from "@mxenabled/widget-post-message-definitions"
56

67
import { Type, SsoUrlProps, ConnectWidgetConfigurationProps } from "../sso"
7-
import { makeDefaultConnectOnOAuthRequested } from "./oauth"
8+
import * as WebBrowser from "expo-web-browser"
89
import { makeWidgetComponentWithDefaults } from "./make_component"
910
import { useOAuthDeeplink, OAuthProps } from "./oauth"
1011
import { useWidgetRendererWithRef, StylingProps } from "./renderer"
@@ -22,17 +23,29 @@ export const ConnectVerificationWidget = makeWidgetComponentWithDefaults(Connect
2223
})
2324

2425
export function ConnectWidget(props: ConnectWidgetProps) {
26+
const onOAuthRequested = (payload: ConnectOAuthRequestedPayload) => {
27+
const { url } = payload
28+
WebBrowser.openAuthSessionAsync(url)
29+
30+
props.onOAuthRequested?.(payload)
31+
}
32+
2533
props = {
26-
onOAuthRequested: makeDefaultConnectOnOAuthRequested(),
34+
onOAuthRequested,
35+
...props,
36+
}
37+
38+
const modifiedProps = {
2739
...props,
40+
onOAuthRequested,
2841
}
2942

3043
const [ref, elem] = useWidgetRendererWithRef(
31-
{ ...props, widgetType: Type.ConnectWidget },
44+
{ ...modifiedProps, widgetType: Type.ConnectWidget },
3245
dispatchConnectLocationChangeEvent,
3346
)
3447

35-
useOAuthDeeplink(ref, props)
48+
useOAuthDeeplink(ref, modifiedProps)
3649

3750
return elem
3851
}

src/components/loadUrlInBrowser.test.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/components/loadUrlInBrowser.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/components/oauth.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
import { useEffect, useState } from "react"
22
import { parse as parseUrl, UrlWithParsedQuery } from "url"
3-
4-
import { ConnectOAuthRequestedPayload } from "@mxenabled/widget-post-message-definitions"
5-
63
import { WebViewRef } from "./webview"
74
import { onUrlChange } from "../platform/deeplink"
8-
import { loadUrlInBrowser } from "./loadUrlInBrowser"
9-
10-
export function makeDefaultConnectOnOAuthRequested() {
11-
return function ({ url }: ConnectOAuthRequestedPayload) {
12-
loadUrlInBrowser(url)
13-
}
14-
}
155

166
/* Used when handling and parsing an OAuth deeplink. This is what the OAuth
177
* flow uses to communicate state and status back to the app.
Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,22 @@
11
import { WebViewNavigation } from "react-native-webview"
2-
import { parse as parseUrl } from "url"
3-
4-
import { loadUrlInBrowser } from "./loadUrlInBrowser"
5-
6-
export enum Action {
7-
LoadInApp,
8-
LoadInBrowser,
9-
Intercept,
10-
}
2+
import * as WebBrowser from "expo-web-browser"
113

124
type Callbacks = {
135
onIntercept: (url: string) => void
146
}
157

16-
class Interceptor {
17-
constructor(protected widgetUrl: string) {}
18-
19-
action(request: WebViewNavigation): Action {
20-
if (request.url === this.widgetUrl) {
21-
return Action.LoadInApp
22-
}
23-
24-
const { protocol } = parseUrl(request.url)
25-
26-
if (protocol === "mx:") {
27-
return Action.Intercept
28-
}
29-
30-
return Action.LoadInBrowser
31-
}
32-
}
33-
348
export function makeRequestInterceptor(widgetUrl: string, callbacks: Callbacks) {
35-
const interceptor = new Interceptor(widgetUrl)
36-
37-
return function (request: WebViewNavigation) {
38-
const action = interceptor.action(request)
39-
switch (action) {
40-
case Action.LoadInApp:
41-
return true
42-
43-
case Action.Intercept:
44-
callbacks.onIntercept(request.url)
45-
return false
46-
47-
case Action.LoadInBrowser:
48-
loadUrlInBrowser(request.url)
49-
return false
9+
return (request: WebViewNavigation) => {
10+
const { protocol } = new URL(request.url)
11+
12+
if (request.url === widgetUrl) {
13+
return true
14+
} else if (protocol === "mx:") {
15+
callbacks.onIntercept(request.url)
16+
return false
17+
} else {
18+
WebBrowser.openBrowserAsync(request.url)
19+
return false
5020
}
5121
}
5222
}

0 commit comments

Comments
 (0)