-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: start implementing ASWebAuthenticationSession
- Loading branch information
Showing
8 changed files
with
118 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import Foundation | ||
import AuthenticationServices | ||
|
||
class HybridASWebAuthenticationSession: HybridASWebAuthenticationSessionSpec { | ||
var hybridContext = margelo.nitro.HybridContext() | ||
|
||
var memorySize: Int { | ||
return getSizeOf(self) | ||
} | ||
|
||
private var authSession: ASWebAuthenticationSession? | ||
|
||
var prefersEphemeralWebBrowserSession: Bool = false | ||
|
||
func start(params: ASWebAuthenticationSessionStartParams) throws -> Void { | ||
NSLog("HybridASWebAuthenticationSession.start(url:%@) is being called", params.url) | ||
|
||
guard let nativeUrl = URL(string: params.url) else { | ||
throw NSError(domain: "HybridASWebAuthenticationSession", code: 0, userInfo: [NSLocalizedDescriptionKey: "Invalid URL"]) | ||
} | ||
|
||
authSession = ASWebAuthenticationSession(url: nativeUrl, callbackURLScheme: nil) { session, error in | ||
if let error = error { | ||
NSLog("ASWebAuthenticationSession failed with error: %@", error.localizedDescription) | ||
// TODO: Implement error handling and callback to JavaScript | ||
} else if let callbackURL = session { | ||
NSLog("ASWebAuthenticationSession succeeded with URL: %@", callbackURL.absoluteString) | ||
// TODO: Implement success callback to JavaScript with the callbackURL | ||
} else { | ||
NSLog("ASWebAuthenticationSession completed without error or callback URL") | ||
// TODO: Implement callback to JavaScript for completion without result | ||
} | ||
|
||
self.authSession = nil | ||
} | ||
|
||
if !(authSession?.start() ?? false) { | ||
throw NSError(domain: "HybridASWebAuthenticationSession", code: 1, userInfo: [NSLocalizedDescriptionKey: "ASWebAuthenticationSession failed to start"]) | ||
} | ||
} | ||
|
||
func cancel() throws -> Void { | ||
guard let authSession = authSession else { | ||
throw NSError(domain: "HybridASWebAuthenticationSession", code: 2, userInfo: [NSLocalizedDescriptionKey: "ASWebAuthenticationSession is not initialized"]) | ||
} | ||
|
||
authSession.cancel() | ||
self.authSession = nil | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Platform } from 'react-native' | ||
import { NitroModules } from 'react-native-nitro-modules' | ||
import type { ASWebAuthenticationSession as ASWebAuthenticationSessionType } from './specs/ASWebAuthenticationSession.nitro' | ||
|
||
export * from './specs/ASWebAuthenticationSession.nitro' | ||
|
||
let ASWebAuthenticationSession: ASWebAuthenticationSessionType | ||
|
||
const ASWebAuthenticationSessionStub: ASWebAuthenticationSessionType = { | ||
name: 'ASWebAuthenticationSession', | ||
equals: () => false, | ||
dispose: () => {}, | ||
prefersEphemeralWebBrowserSession: false, | ||
start: () => { | ||
console.warn('ASWebAuthenticationSession.start is only supported on iOS.') | ||
}, | ||
cancel: () => { | ||
console.warn('ASWebAuthenticationSession.cancel is only supported on iOS.') | ||
}, | ||
} | ||
|
||
if (Platform.OS === 'ios') { | ||
ASWebAuthenticationSession = | ||
NitroModules.createHybridObject<ASWebAuthenticationSessionType>( | ||
'ASWebAuthenticationSession' | ||
) | ||
} else { | ||
ASWebAuthenticationSession = ASWebAuthenticationSessionStub | ||
} | ||
|
||
export { ASWebAuthenticationSession } |
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