Skip to content

Commit

Permalink
feat(auth): automatically load session from deep link
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed May 21, 2024
1 parent 7222c4b commit a6827b6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Examples/Examples/ExamplesApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,35 @@ import GoogleSignIn
import Supabase
import SwiftUI

class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
supabase.application(application, didFinishLaunchingWithOptions: launchOptions)
}

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
supabase.application(app, open: url, options: options)
}

func application(_: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options _: UIScene.ConnectionOptions) -> UISceneConfiguration {
let configuration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
configuration.delegateClass = SceneDelegate.self
return configuration
}
}

class SceneDelegate: UIResponder, UISceneDelegate {
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
supabase.scene(scene, openURLContexts: URLContexts)
}
}

@main
struct ExamplesApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

var body: some Scene {
WindowGroup {
RootView()
Expand Down
56 changes: 56 additions & 0 deletions Sources/Supabase/SupabaseClient+UIApplicationDelegate.swift
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)
"""
)
}
}
}
}

0 comments on commit a6827b6

Please sign in to comment.