Skip to content

Commit

Permalink
Merge pull request #16 from frontegg/FR-13828-fix-social-login-ruls
Browse files Browse the repository at this point in the history
FR-13828 - handle social login urls
  • Loading branch information
frontegg-david authored Oct 24, 2023
2 parents a070870 + 2050d6f commit d88b720
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion FronteggSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'FronteggSwift'
s.version = '1.1.0'
s.version = '1.1.1'
s.summary = 'A swift library for easy integrating iOS application with Frontegg Services'
s.description = 'Frontegg is an end-to-end user management platform for B2B SaaS, powering strategies from PLG to enterprise readiness. Easy migration, no credit card required'
s.homepage = 'https://github.com/frontegg/frontegg-ios-swift'
Expand Down
43 changes: 36 additions & 7 deletions Sources/FronteggSwift/embedded/CustomWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class CustomWebView: WKWebView, WKNavigationDelegate {
logger.info("urlType: \(urlType)")
switch(urlType){

case .SocialLoginRedirectToBrowser: do {

return self.handleSocialLoginRedirectToBrowser(webView, url)
}
// case .SocialLoginRedirectToBrowser: do {
//
// return self.handleSocialLoginRedirectToBrowser(webView, url)
// }
case .HostedLoginCallback: do {
return self.handleHostedLoginCallback(webView, url)
}
Expand Down Expand Up @@ -184,7 +184,7 @@ class CustomWebView: WKWebView, WKNavigationDelegate {

logger.trace("setSocialLoginRedirectUri()")
let queryItems = [
URLQueryItem(name: "redirectUri", value: generateRedirectUri()),
URLQueryItem(name: "redirectUri", value: generateRedirectUri())
]
var urlComps = URLComponents(string: url.absoluteString)!

Expand All @@ -197,7 +197,37 @@ class CustomWebView: WKWebView, WKNavigationDelegate {


logger.trace("added redirectUri to socialLogin auth url \(urlComps.url!)")
_ = webView.load(URLRequest(url: urlComps.url!))
// _ = webView.load(URLRequest(url: urlComps.url!))

let followUrl = urlComps.url!
DispatchQueue.global(qos: .background).async {
var request = URLRequest(url: followUrl)
request.httpMethod = "GET"

let noRedirectDelegate = NoRedirectSessionDelegate()
let noRedirectSession = URLSession(configuration: .default, delegate: noRedirectDelegate, delegateQueue: nil)

let task = noRedirectSession.dataTask(with: request) { (data, response, error) in
// Check for errors
if let error = error {
print("Error: \(error.localizedDescription)")
return
}

// Check for valid HTTP response
if let httpResponse = response as? HTTPURLResponse {

if let location = httpResponse.value(forHTTPHeaderField: "Location"),
let socialLoginUrl = URL(string: location){

self.handleSocialLoginRedirectToBrowser(webView, socialLoginUrl)
}

}
}
task.resume()
}

return .cancel
}

Expand All @@ -212,7 +242,6 @@ class CustomWebView: WKWebView, WKNavigationDelegate {

let url = urlComps.url!

fronteggAuth.webLoading = false
fronteggAuth.webAuthentication.webAuthSession?.cancel()
fronteggAuth.webAuthentication = WebAuthentication()

Expand Down
16 changes: 16 additions & 0 deletions Sources/FronteggSwift/utils/NoRedirectSessionDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// NoRedirectSessionDelegate.swift
//
//
// Created by David Frontegg on 24/10/2023.
//

import Foundation


class NoRedirectSessionDelegate: NSObject, URLSessionTaskDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
// Returning nil will prevent the redirect
completionHandler(nil)
}
}
6 changes: 3 additions & 3 deletions Sources/FronteggSwift/utils/UrlHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ func getOverrideUrlType (url: URL) -> OverrideUrlType {
if(url.absoluteString.starts(with: generateRedirectUri())){
return .HostedLoginCallback
}
if((URLConstants.oauthUrls.first { urlStr.hasPrefix($0)}) != nil) {
return .SocialLoginRedirectToBrowser
}
// if((URLConstants.oauthUrls.first { urlStr.hasPrefix($0)}) != nil) {
// return .SocialLoginRedirectToBrowser
// }

return .Unknown

Expand Down

0 comments on commit d88b720

Please sign in to comment.