Skip to content

Commit

Permalink
[REST API] Fix magic link handling on cold start (#14502)
Browse files Browse the repository at this point in the history
  • Loading branch information
hichamboushaba authored Nov 25, 2024
2 parents 275aedd + a425c2f commit d5d40ba
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
3 changes: 3 additions & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
*** PLEASE FOLLOW THIS FORMAT: [<priority indicator, more stars = higher priority>] <description> [<PR URL>]
*** Use [*****] to indicate smoke tests of all critical flows should be run on the final IPA before release (e.g. major library or OS update).
21.3
-----
- [*] Jetpack Setup: Fixed an issue with magic link handling when the app is cold started. [https://github.com/woocommerce/woocommerce-ios/pull/14502]

21.3
-----
Expand Down
52 changes: 45 additions & 7 deletions WooCommerce/Classes/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import UIKit
import Combine
import Storage
import class Networking.UserAgent
import Experiments
import class WidgetKit.WidgetCenter
import protocol WooFoundation.Analytics
import protocol Yosemite.StoresManager
import struct Yosemite.Site

import CocoaLumberjack
import KeychainAccess
Expand Down Expand Up @@ -61,6 +63,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
///
private let appRefreshHandler = BackgroundTaskRefreshDispatcher()

private var subscriptions: Set<AnyCancellable> = []

// MARK: - AppDelegate Methods

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
Expand Down Expand Up @@ -135,17 +139,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
fatalError()
}

if ServiceLocator.stores.isAuthenticatedWithoutWPCom,
let site = ServiceLocator.stores.sessionManager.defaultSite {
let coordinator = JetpackSetupCoordinator(site: site, rootViewController: rootViewController)
jetpackSetupCoordinator = coordinator
return coordinator.handleAuthenticationUrl(url)
}
if let universalLinkRouter, universalLinkRouter.canHandle(url: url) {
universalLinkRouter.handle(url: url)
return true
}
return ServiceLocator.authenticationManager.handleAuthenticationUrl(url, options: options, rootViewController: rootViewController)

return handleAuthenticationUrl(url, options: options, rootViewController: rootViewController)
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Expand Down Expand Up @@ -578,6 +577,45 @@ private extension AppDelegate {
}
}

// MARK: - Magic link
private extension AppDelegate {
func handleAuthenticationUrl(_ url: URL, options: [UIApplication.OpenURLOptionsKey: Any], rootViewController: UIViewController) -> Bool {
return if ServiceLocator.stores.isAuthenticatedWithoutWPCom {
handleAuthenticationUrlForJetpackSetup(url, rootViewController: rootViewController)
} else {
ServiceLocator.authenticationManager.handleAuthenticationUrl(url, options: options, rootViewController: rootViewController)
}
}

func handleAuthenticationUrlForJetpackSetup(_ url: URL, rootViewController: UIViewController) -> Bool {
if let site = ServiceLocator.stores.sessionManager.defaultSite {
return handleAuthenticationUrlForJetpackSetup(with: site, url: url, rootViewController: rootViewController)
} else {
// Wait for the site to be available before handling the magic link
ServiceLocator.stores.sessionManager.defaultSitePublisher
.timeout(.seconds(30), scheduler: DispatchQueue.main)
.first(where: { $0 != nil })
.sink { [weak self] site in
guard let site else {
// This should never happen as we filter out nil values
return
}
_ = self?.handleAuthenticationUrlForJetpackSetup(with: site, url: url, rootViewController: rootViewController)
}
.store(in: &subscriptions)

// Assume that we will handle the URL
return true
}
}

func handleAuthenticationUrlForJetpackSetup(with site: Site, url: URL, rootViewController: UIViewController) -> Bool {
let coordinator = JetpackSetupCoordinator(site: site, rootViewController: rootViewController)
jetpackSetupCoordinator = coordinator
return coordinator.handleAuthenticationUrl(url)
}
}

// MARK: - Home Screen Quick Actions

enum QuickAction: String {
Expand Down

0 comments on commit d5d40ba

Please sign in to comment.