Skip to content

Commit a6827b6

Browse files
committed
feat(auth): automatically load session from deep link
1 parent 7222c4b commit a6827b6

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

Examples/Examples/ExamplesApp.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,35 @@ import GoogleSignIn
99
import Supabase
1010
import SwiftUI
1111

12+
class AppDelegate: UIResponder, UIApplicationDelegate {
13+
func application(
14+
_ application: UIApplication,
15+
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
16+
) -> Bool {
17+
supabase.application(application, didFinishLaunchingWithOptions: launchOptions)
18+
}
19+
20+
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
21+
supabase.application(app, open: url, options: options)
22+
}
23+
24+
func application(_: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options _: UIScene.ConnectionOptions) -> UISceneConfiguration {
25+
let configuration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
26+
configuration.delegateClass = SceneDelegate.self
27+
return configuration
28+
}
29+
}
30+
31+
class SceneDelegate: UIResponder, UISceneDelegate {
32+
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
33+
supabase.scene(scene, openURLContexts: URLContexts)
34+
}
35+
}
36+
1237
@main
1338
struct ExamplesApp: App {
39+
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
40+
1441
var body: some Scene {
1542
WindowGroup {
1643
RootView()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// SupabaseClient+UIApplicationDelegate.swift
3+
//
4+
//
5+
// Created by Guilherme Souza on 15/05/24.
6+
//
7+
8+
import UIKit
9+
10+
extension SupabaseClient {
11+
@discardableResult
12+
public func application(
13+
_: UIApplication,
14+
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
15+
) -> Bool {
16+
if let url = launchOptions?[.url] as? URL {
17+
handleDeepLink(url)
18+
}
19+
20+
return true
21+
}
22+
23+
public func application(
24+
_: UIApplication,
25+
open url: URL,
26+
options _: [UIApplication.OpenURLOptionsKey: Any] = [:]
27+
) -> Bool {
28+
handleDeepLink(url)
29+
return true
30+
}
31+
32+
@MainActor
33+
public func scene(_: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
34+
guard let url = URLContexts.first?.url else { return }
35+
36+
handleDeepLink(url)
37+
}
38+
39+
private func handleDeepLink(_ url: URL) {
40+
let logger = options.global.logger
41+
42+
Task {
43+
do {
44+
try await auth.session(from: url)
45+
} catch {
46+
logger?.error(
47+
"""
48+
Failure loading session.
49+
URL: \(url.absoluteString)
50+
Error: \(error)
51+
"""
52+
)
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)