Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR-14855 - Support UIKit #35

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,16 @@ your root project directory, this file will store values to be used variables by
return true
}
```
- Create FronteggController class that extends AbstractFronteggController from FronteggSwift
- Create AuthenticationController class that extends AbstractFronteggController from FronteggSwift
```swift
//
// FronteggController.swift
// AuthenticationController.swift
//

import UIKit
import FronteggSwift

class FronteggController: AbstractFronteggController {
class AuthenticationController: AbstractFronteggController {

override func navigateToAuthenticated(){
// This function will be called when the user is authenticated
Expand Down Expand Up @@ -237,29 +237,14 @@ your root project directory, this file will store values to be used variables by
// if the user is not authenticated

let fronteggAuth = FronteggApp.shared.auth
let sub = AnySubscriber<Bool, Never>(
receiveSubscription: {query in
query.request(.unlimited)
}, receiveValue: { showLoader in
self.showLoader = showLoader
self.label.text = fronteggAuth.user?.email ?? "Unknown"

if(!showLoader && !fronteggAuth.isAuthenticated){
// Display your own Authentication View Controller
// to handle after oauth callback
window?.rootViewController = AuthenticationController()
window?.makeKeyAndVisible()
return .none
}
return .unlimited
})

FronteggApp.shared.auth.$showLoader.subscribe(sub)

self.label.text = fronteggAuth.user?.email ?? "Unknown"
}

@IBAction func logoutButton (){
FronteggApp.shared.auth.logout()
FronteggApp.shared.auth.logout() { _ in
window?.rootViewController = AuthenticationController()
window?.makeKeyAndVisible()
}
}

}
Expand Down
9 changes: 7 additions & 2 deletions Sources/FronteggSwift/FronteggAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public class FronteggAuth: ObservableObject {
}
}

public func logout() {
public func logout(_ completion: @escaping (Result<Bool, FronteggError>) -> Void) {
self.isLoading = true

DispatchQueue.global(qos: .userInitiated).async {
Expand All @@ -222,10 +222,15 @@ public class FronteggAuth: ObservableObject {

// isLoading must be at the last bottom
self.isLoading = false
completion(.success(true));
}
}
}

}
public func logout() {
logout { res in
print("logged out")
}
}

public func refreshTokenIfNeeded() async {
Expand Down
5 changes: 4 additions & 1 deletion Sources/FronteggSwift/embedded/CustomWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,14 @@ class CustomWebView: WKWebView, WKNavigationDelegate {
}

let errorMessage = error.localizedDescription;
let url = "\(error.userInfo["NSErrorFailingURLKey"] ?? error.userInfo)"
let url = "\(error.userInfo["NSErrorFailingURLKey"] ?? "")"
logger.error("Failed to load page: \(errorMessage), status: \(statusCode), \(error)")


self.fronteggAuth.webLoading = false
let content = generateErrorPage(message: errorMessage, url: url, status: statusCode);
webView.loadHTMLString(content, baseURL: nil);

}


Expand Down
35 changes: 35 additions & 0 deletions Sources/FronteggSwift/views/AbstractFronteggController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// AbstractFronteggController.swift
//
//
// Created by David Frontegg on 30/12/2022.
//

import Foundation
import UIKit
import SwiftUI


open class AbstractFronteggController: UIViewController {
private var logger = getLogger("AbstractFronteggController")

open func navigateToAuthenticated() {
logger.error("Missing AbstractFronteggController implementation class")
fatalError("Not implemented")
}

public override func viewDidLoad() {
super.viewDidLoad()
logger.trace("viewDidLoad()")
let childView = UIHostingController(rootView: FronteggUIKitWrapper(
navigateToAuthenticated: navigateToAuthenticated,
loaderView: nil
))
addChild(childView)
childView.view.frame = self.view.bounds
self.view.addSubview(childView.view)
childView.didMove(toParent: self)
}


}
38 changes: 38 additions & 0 deletions Sources/FronteggSwift/views/FronteggUIKitWrapper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// FronteggUIKitWrapper.swift
//
// Created by David Frontegg on 14/11/2022.
//

import SwiftUI

struct FronteggUIKitWrapper: View {
var loaderView: AnyView
var navigateToAuthenticated: () -> Void
@StateObject var fronteggAuth = FronteggApp.shared.auth

init(navigateToAuthenticated: @escaping () -> Void, loaderView: AnyView?) {
self.navigateToAuthenticated = navigateToAuthenticated
self.loaderView = loaderView ?? AnyView(DefaultLoader())
}
public var body: some View {
ZStack {
if fronteggAuth.initializing
|| fronteggAuth.showLoader
|| fronteggAuth.appLink {
self.loaderView
}else {
if(fronteggAuth.isAuthenticated) {
self.loaderView.onAppear() {
self.navigateToAuthenticated()
}
}else {
self.loaderView.onAppear() {
fronteggAuth.login()
}
}
}
}
.environmentObject(fronteggAuth)
}
}
Loading
Loading