-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[menu-bar] Migrate WebAuthenticationSession module to expo-modules an…
…d add electron support (#177) * [menu-bar][electron] Add support for WebAuthenticationSession module * [menu-bar] Migrate WebAuthenticationSession module to expo-modules * Fix deeplinks on windows * Remove objective-c code * Add changelog entry * Apply suggestions from code review Co-authored-by: Alan Hughes <30924086+alanjhughes@users.noreply.github.com> * No longer extend ObservableObject --------- Co-authored-by: Alan Hughes <30924086+alanjhughes@users.noreply.github.com>
- Loading branch information
1 parent
d0a2d20
commit 56aae30
Showing
17 changed files
with
168 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 0 additions & 4 deletions
4
apps/menu-bar/macos/ExpoMenuBar-macOS/WebAuthenticationSession.h
This file was deleted.
Oops, something went wrong.
53 changes: 0 additions & 53 deletions
53
apps/menu-bar/macos/ExpoMenuBar-macOS/WebAuthenticationSession.m
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
apps/menu-bar/modules/web-authentication-session/electron/main.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { BrowserWindow } from 'electron'; | ||
|
||
import { | ||
WebBrowserResult, | ||
WebBrowserResultType, | ||
WebAuthenticationSessionModuleType, | ||
} from '../src/WebAuthenticationSession.types'; | ||
|
||
async function openAuthSessionAsync(urlString: string): Promise<WebBrowserResult> { | ||
const url = new URL(urlString); | ||
const window = new BrowserWindow({ | ||
width: 860, | ||
height: 740, | ||
}); | ||
window.menuBarVisible = false; | ||
window.loadURL(urlString); | ||
|
||
return new Promise((resolve, reject) => { | ||
function handleRedirect( | ||
event: Electron.Event< | ||
Electron.WebContentsWillRedirectEventParams | Electron.WebContentsWillNavigateEventParams | ||
> | ||
) { | ||
if (event.isSameDocument || url.origin === new URL(event.url).origin) { | ||
return; | ||
} | ||
|
||
event.preventDefault(); | ||
window.close(); | ||
|
||
resolve({ type: WebBrowserResultType.SUCCESS, url: event.url }); | ||
} | ||
|
||
window.webContents.on('will-redirect', handleRedirect); | ||
window.webContents.on('will-navigate', handleRedirect); | ||
|
||
window.on('closed', () => { | ||
resolve({ type: WebBrowserResultType.CANCEL }); | ||
}); | ||
|
||
window.webContents.on('render-process-gone', (event, details) => { | ||
reject(new Error(details.reason)); | ||
}); | ||
}); | ||
} | ||
|
||
const WebAuthenticationSession: WebAuthenticationSessionModuleType & { name: string } = { | ||
name: 'WebAuthenticationSession', | ||
openAuthSessionAsync, | ||
}; | ||
|
||
export default WebAuthenticationSession; |
6 changes: 6 additions & 0 deletions
6
apps/menu-bar/modules/web-authentication-session/expo-module.config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"platforms": ["apple", "web"], | ||
"ios": { | ||
"modules": ["WebAuthenticationSessionModule"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { WebBrowserResultType } from './src/WebAuthenticationSession.types'; | ||
import WebAuthenticationSessionModule from './src/WebAuthenticationSessionModule'; | ||
|
||
export async function openAuthSessionAsync(url: string) { | ||
return await WebAuthenticationSessionModule.openAuthSessionAsync(url); | ||
} | ||
|
||
export { WebBrowserResultType }; |
13 changes: 13 additions & 0 deletions
13
apps/menu-bar/modules/web-authentication-session/ios/WebAuthContextProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import AuthenticationServices | ||
|
||
public class WebAuthContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding { | ||
public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor { | ||
var anchor: ASPresentationAnchor? | ||
|
||
DispatchQueue.main.sync { | ||
anchor = NSApp.mainWindow | ||
} | ||
|
||
return anchor ?? ASPresentationAnchor() | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
apps/menu-bar/modules/web-authentication-session/ios/WebAuthenticationSession.podspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Pod::Spec.new do |s| | ||
s.name = 'WebAuthenticationSession' | ||
s.version = '1.0.0' | ||
s.summary = 'A sample project summary' | ||
s.description = 'A sample project description' | ||
s.author = '' | ||
s.homepage = 'https://docs.expo.dev/modules/' | ||
s.platform = :osx, '10.15' | ||
s.source = { git: '' } | ||
s.static_framework = true | ||
|
||
s.dependency 'ExpoModulesCore' | ||
|
||
# Swift/Objective-C compatibility | ||
s.pod_target_xcconfig = { | ||
'DEFINES_MODULE' => 'YES', | ||
'SWIFT_COMPILATION_MODE' => 'wholemodule' | ||
} | ||
|
||
s.source_files = "**/*.{h,m,mm,swift,hpp,cpp}" | ||
end |
35 changes: 35 additions & 0 deletions
35
apps/menu-bar/modules/web-authentication-session/ios/WebAuthenticationSessionModule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import ExpoModulesCore | ||
import AuthenticationServices | ||
|
||
public class WebAuthenticationSessionModule: Module { | ||
var authContextProvider = WebAuthContextProvider() | ||
var authSession: ASWebAuthenticationSession? | ||
|
||
public func definition() -> ModuleDefinition { | ||
Name("WebAuthenticationSession") | ||
|
||
AsyncFunction("openAuthSessionAsync") { (urlString: String, promise: Promise) in | ||
guard let url = URL(string: urlString) else { | ||
promise.reject("INVALID_URL", "Invalid URL provided") | ||
return | ||
} | ||
|
||
authSession = ASWebAuthenticationSession(url: url, callbackURLScheme: "expo-orbit") { callbackURL, error in | ||
if let error { | ||
promise.reject("AUTH_SESSION_ERROR", error.localizedDescription) | ||
} else { | ||
if let callbackURL { | ||
promise.resolve(["type": "success", "url": callbackURL.absoluteString]) | ||
} else { | ||
promise.resolve(["type": "cancel"]) | ||
} | ||
} | ||
} | ||
|
||
if #available(macOS 10.15, *) { | ||
authSession?.presentationContextProvider = authContextProvider | ||
} | ||
authSession?.start() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
apps/menu-bar/modules/web-authentication-session/src/WebAuthenticationSessionModule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { requireNativeModule } from 'expo-modules-core'; | ||
|
||
import { WebAuthenticationSessionModuleType } from './WebAuthenticationSession.types'; | ||
|
||
export default requireNativeModule<WebAuthenticationSessionModuleType>('WebAuthenticationSession'); |
7 changes: 7 additions & 0 deletions
7
apps/menu-bar/modules/web-authentication-session/src/WebAuthenticationSessionModule.web.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { requireElectronModule } from 'react-native-electron-modules/build/requireElectronModule'; | ||
|
||
import { WebAuthenticationSessionModuleType } from './WebAuthenticationSession.types'; | ||
|
||
export default requireElectronModule<WebAuthenticationSessionModuleType>( | ||
'WebAuthenticationSession' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters