@@ -307,25 +307,40 @@ public class DescopeFlowCoordinator {
307307 }
308308
309309 // Authentication
310+
311+ private func handleData( _ data: Data ? ) async {
312+ let jwtResponse = await parseJWTResponse ( data)
313+ if let jwtResponse {
314+ do {
315+ let authResponse = try jwtResponse. convert ( )
316+ logger. debug ( " Finishing flow with an authentication response " , data)
317+ return handleSuccess ( authResponse)
318+ } catch {
319+ logger. debug ( " Finishing flow with a partial authentication response " )
320+ }
321+ }
310322
311- private func handleAuthentication ( _ data : Data ) {
312- logger . info ( " Finishing flow authentication " )
313- Task {
314- guard let authResponse = await parseAuthentication ( data ) else { return }
315- handleSuccess ( authResponse )
323+ if let session = flow ? . providedSession {
324+ if jwtResponse == nil {
325+ logger . info ( " Finishing flow authentication without an authentication response " )
326+ }
327+ return handleSuccess ( AuthenticationResponse ( sessionToken : session . sessionToken , refreshToken : session . refreshToken , user : session . user , isFirstAuthentication : false , flowOutput : jwtResponse ? . flowOutput ?? [ : ] ) )
316328 }
329+
330+ logger. error ( " Couldn't find session to finish flow " , flow? . sessionProvider == nil ? " nil provider " : " custom provider " )
331+ handleError ( DescopeError . flowFailed. with ( message: " No valid authentication tokens found " ) )
317332 }
318333
319- private func parseAuthentication( _ data: Data ) async -> AuthenticationResponse ? {
334+ private func parseJWTResponse( _ data: Data ? ) async -> DescopeClient . JWTResponse ? {
335+ guard let data, let webView else { return nil }
320336 do {
321- guard let webView else { return nil }
322337 var jwtResponse = try JSONDecoder ( ) . decode ( DescopeClient . JWTResponse. self, from: data)
323338 let cookies = await webView. configuration. websiteDataStore. httpCookieStore. cookies ( for: jwtResponse. cookieDomain, at: webView. url)
324339 try jwtResponse. setValues ( from: data, cookies: cookies, refreshCookieName: bridge. attributes. refreshCookieName)
325- return try jwtResponse. convert ( )
340+ return jwtResponse
326341 } catch {
342+ // should never happen because all JWTResponse fields are optional
327343 logger. error ( " Unexpected error parsing authentication response " , error, String ( bytes: data, encoding: . utf8) )
328- handleError ( DescopeError . flowFailed. with ( message: " No valid authentication response found " ) )
329344 return nil
330345 }
331346 }
@@ -401,13 +416,8 @@ extension DescopeFlowCoordinator: FlowBridgeDelegate {
401416 }
402417
403418 func bridgeDidFinish( _ bridge: FlowBridge , data: Data ? ) {
404- if let data {
405- handleAuthentication ( data)
406- } else if let session = flow? . providedSession {
407- handleSuccess ( AuthenticationResponse ( sessionToken: session. sessionToken, refreshToken: session. refreshToken, user: session. user, isFirstAuthentication: false ) )
408- } else {
409- logger. error ( " Couldn't find session to finish flow " , flow? . sessionProvider == nil ? " nil provider " : " custom provider " )
410- handleError ( DescopeError . flowFailed. with ( message: " No valid authentication tokens found " ) )
419+ Task { @MainActor in
420+ await handleData ( data)
411421 }
412422 }
413423}
0 commit comments