Skip to content

Files

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Latest commit

d4025fd · May 8, 2025

History

History
51 lines (46 loc) · 1.7 KB

NativeApplication_Swift.md

File metadata and controls

51 lines (46 loc) · 1.7 KB
name language
swift.swift
swift
import SwiftUI
import Auth0
import WebKit

struct MainView: View {
    var body: some View {
        Button("Open Web Session", action: self.launchWebSSO)
    }

    func launchWebSSO() {
        Auth0
            .authentication()
            .ssoExchange(withRefreshToken: refreshToken)
            .start { result in
                switch result {
                case .success(let ssoCredentials):
                    DispatchQueue.main.async {
                        let cookie = HTTPCookie(properties: [
                            .domain: "${account.namespace}",
                            .path: "/",
                            .name: "auth0_session_transfer_token",
                            .value: ssoCredentials.sessionTransferToken,
                            .expires: ssoCredentials.expiresIn,
                            .secure: true
                        ])!

                        let webView = WKWebView()
                        let store = webView.configuration.websiteDataStore.httpCookieStore
                        store.setCookie(cookie) {
                            let url = URL(string: "https://yourWebApplicationLoginURI")!
                            let request = URLRequest(url: url)
                            webView.load(request)

                            let vc = UIViewController()
                            vc.view = webView
                            UIApplication.shared.windows.first?.rootViewController?.present(vc, animated: true)
                        }
                    }
                case .failure(let error):
                    print("Failed to get SSO token: \(error)")
                }
            }
    }
}