-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): automatically load session from deep link
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 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
56 changes: 56 additions & 0 deletions
56
Sources/Supabase/SupabaseClient+UIApplicationDelegate.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,56 @@ | ||
// | ||
// SupabaseClient+UIApplicationDelegate.swift | ||
// | ||
// | ||
// Created by Guilherme Souza on 15/05/24. | ||
// | ||
|
||
import UIKit | ||
|
||
extension SupabaseClient { | ||
@discardableResult | ||
public func application( | ||
_: UIApplication, | ||
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? | ||
) -> Bool { | ||
if let url = launchOptions?[.url] as? URL { | ||
handleDeepLink(url) | ||
} | ||
|
||
return true | ||
} | ||
|
||
public func application( | ||
_: UIApplication, | ||
open url: URL, | ||
options _: [UIApplication.OpenURLOptionsKey: Any] = [:] | ||
) -> Bool { | ||
handleDeepLink(url) | ||
return true | ||
} | ||
|
||
@MainActor | ||
public func scene(_: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { | ||
guard let url = URLContexts.first?.url else { return } | ||
|
||
handleDeepLink(url) | ||
} | ||
|
||
private func handleDeepLink(_ url: URL) { | ||
let logger = options.global.logger | ||
|
||
Task { | ||
do { | ||
try await auth.session(from: url) | ||
} catch { | ||
logger?.error( | ||
""" | ||
Failure loading session. | ||
URL: \(url.absoluteString) | ||
Error: \(error) | ||
""" | ||
) | ||
} | ||
} | ||
} | ||
} |