Skip to content

Commit

Permalink
[Latte] Auto back to auth flow after reset password completed
Browse files Browse the repository at this point in the history
  • Loading branch information
louischan-oursky committed Oct 19, 2023
2 parents be848db + 455e4f6 commit 9146e6d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
44 changes: 40 additions & 4 deletions Sources/Latte+Flows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,22 @@ public extension Latte {
let handle = LatteHandle<UserInfo>(task: Task { try await run1() })
@Sendable @MainActor
func run1() async throws -> UserInfo {
let result: LatteWebViewResult = try await withCheckedThrowingContinuation { next in
let result: LatteWebViewResult = try await withCheckedThrowingContinuation { [
weak self
] next in
guard let nc = self?.eventNotificationCenter else { return }
let observer = nc.addObserver(
forName: LatteInternalEvent.resetPasswordCompleted.notificationName,
object: nil,
queue: nil,
using: { _ in
Task {
await latteVC.dispatchWebViewSignal(signal: .resetPasswordCompleted)
}
}
)
latteVC.webView.completion = { (_, result) in
nc.removeObserver(observer)
next.resume(with: result)
}
}
Expand Down Expand Up @@ -183,17 +197,29 @@ public extension Latte {

@Sendable @MainActor
func run1() async throws -> Bool {
let result: Bool = try await withCheckedThrowingContinuation { next in
let result: Bool = try await withCheckedThrowingContinuation { [weak self] next in
guard let nc = self?.eventNotificationCenter else { return }
var isResumed = false
func resume(_ result: Result<Bool, Error>) {
guard isResumed == false else { return }
isResumed = true
next.resume(with: result)
}
let observer = nc.addObserver(
forName: LatteInternalEvent.resetPasswordCompleted.notificationName,
object: nil,
queue: nil,
using: { _ in
Task {
await latteVC.dispatchWebViewSignal(signal: .resetPasswordCompleted)
}
}
)
latteVC.webView.completion = { (_, result) in
do {
nc.removeObserver(observer)
let finishURL = try result.get().unwrap()
self.authgear.experimental.finishAuthentication(finishURL: finishURL, request: request) { r in
self?.authgear.experimental.finishAuthentication(finishURL: finishURL, request: request) { r in
resume(r.flatMap { _ in
.success(true)
})
Expand Down Expand Up @@ -387,7 +413,13 @@ public extension Latte {
let handle = LatteHandle<Void>(task: Task { try await run1() })
@Sendable @MainActor
func run1() async throws {
let result: LatteWebViewResult = try await withCheckedThrowingContinuation { next in
let result: LatteWebViewResult = try await withCheckedThrowingContinuation { [weak self] next in
latteVC.webView.onResetPasswordCompleted = { _ in
self?.eventNotificationCenter.post(
name: LatteInternalEvent.resetPasswordCompleted.notificationName,
object: nil
)
}
latteVC.webView.completion = { (_, result) in
next.resume(with: result)
}
Expand Down Expand Up @@ -572,4 +604,8 @@ class LatteViewController: UIViewController {
}
}
}

func dispatchWebViewSignal(signal: LatteBuiltInSignals) {
self.webView.dispatchSignal(signal: signal)
}
}
18 changes: 18 additions & 0 deletions Sources/Latte+WKWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ enum LatteBuiltInEvents: String {
case tracking
case ready
case reauthWithBiometric
case resetPasswordCompleted
}

enum LatteBuiltInSignals: String {
case resetPasswordCompleted
}

let initScript = """
Expand All @@ -28,6 +33,7 @@ class LatteWKWebView: WKWebView, WKNavigationDelegate {
let request: LatteWebViewRequest
var onReady: ((_ webview: LatteWKWebView) -> Void)?
var onReauthWithBiometric: ((_ webview: LatteWKWebView) -> Void)?
var onResetPasswordCompleted: ((_ webview: LatteWKWebView) -> Void)?
var completion: ((_ webview: LatteWKWebView, _ result: Result<LatteWebViewResult, Error>) -> Void)?
weak var viewController: UIViewController?
weak var delegate: LatteWebViewDelegate?
Expand Down Expand Up @@ -78,6 +84,16 @@ class LatteWKWebView: WKWebView, WKNavigationDelegate {
self.initialNavigation = self.load(URLRequest(url: self.request.url))
}

func dispatchSignal(signal: LatteBuiltInSignals) {
self.evaluateJavaScript("""
document.dispatchEvent(
new CustomEvent("latte:signal", {
detail: { type: "\(signal.rawValue)" },
})
);
""")
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
if navigation == self.initialNavigation {
self.completion?(self, .failure(error))
Expand Down Expand Up @@ -151,6 +167,8 @@ class LatteWKWebView: WKWebView, WKNavigationDelegate {
parent.onReady = nil
case LatteBuiltInEvents.reauthWithBiometric.rawValue:
parent.onReauthWithBiometric?(parent)
case LatteBuiltInEvents.resetPasswordCompleted.rawValue:
parent.onResetPasswordCompleted?(parent)
default:
return
}
Expand Down
10 changes: 10 additions & 0 deletions Sources/Latte.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class Latte: LatteWebViewDelegate {
let webViewLoadTimeoutMillis: Int
public weak var delegate: LatteDelegate?

let eventNotificationCenter = NotificationCenter()

public init(
authgear: Authgear,
customUIEndpoint: String,
Expand Down Expand Up @@ -133,6 +135,14 @@ enum LatteWebViewEvent {
case trackingEvent(event: LatteTrackingEvent)
}

enum LatteInternalEvent: String {
case resetPasswordCompleted

var notificationName: Notification.Name {
Notification.Name(self.rawValue)
}
}

public struct LatteBiometricOptions {
let localizedReason: String
let laPolicy: LAPolicy
Expand Down

0 comments on commit 9146e6d

Please sign in to comment.