@@ -449,8 +449,10 @@ public final class AuthClient: Sendable {
449
449
450
450
/// Log in an existing user by exchanging an Auth Code issued during the PKCE flow.
451
451
public func exchangeCodeForSession( authCode: String ) async throws -> Session {
452
- guard let codeVerifier = codeVerifierStorage. get ( ) else {
453
- throw AuthError . pkce ( . codeVerifierNotFound)
452
+ let codeVerifier = codeVerifierStorage. get ( )
453
+
454
+ if codeVerifier == nil {
455
+ logger? . error ( " code verifier not found, a code verifier should exist when calling this method. " )
454
456
}
455
457
456
458
let session : Session = try await api. execute (
@@ -519,14 +521,10 @@ public final class AuthClient: Sendable {
519
521
queryParams: [ ( name: String , value: String ? ) ] = [ ] ,
520
522
launchFlow: @MainActor @Sendable ( _ url: URL ) async throws -> URL
521
523
) async throws -> Session {
522
- guard let redirectTo = ( redirectTo ?? configuration. redirectToURL) else {
523
- throw AuthError . invalidRedirectScheme
524
- }
525
-
526
524
let url = try getOAuthSignInURL (
527
525
provider: provider,
528
526
scopes: scopes,
529
- redirectTo: redirectTo,
527
+ redirectTo: redirectTo ?? configuration . redirectToURL ,
530
528
queryParams: queryParams
531
529
)
532
530
@@ -566,8 +564,9 @@ public final class AuthClient: Sendable {
566
564
) { @MainActor url in
567
565
try await withCheckedThrowingContinuation { continuation in
568
566
guard let callbackScheme = ( configuration. redirectToURL ?? redirectTo) ? . scheme else {
569
- continuation. resume ( throwing: AuthError . invalidRedirectScheme)
570
- return
567
+ preconditionFailure (
568
+ " Please, provide a valid redirect URL, either thorugh `redirectTo` param, or globally thorugh `AuthClient.Configuration.redirectToURL`. "
569
+ )
571
570
}
572
571
573
572
#if !os(tvOS) && !os(watchOS)
@@ -583,7 +582,7 @@ public final class AuthClient: Sendable {
583
582
} else if let url {
584
583
continuation. resume ( returning: url)
585
584
} else {
586
- continuation . resume ( throwing : AuthError . missingURL )
585
+ fatalError ( " Expected url or error, but got none. " )
587
586
}
588
587
589
588
#if !os(tvOS) && !os(watchOS)
@@ -674,24 +673,28 @@ public final class AuthClient: Sendable {
674
673
let params = extractParams ( from: url)
675
674
676
675
if configuration. flowType == . implicit, !isImplicitGrantFlow( params: params) {
677
- throw AuthError . invalidImplicitGrantFlowURL
676
+ throw AuthError . implicitGrantRedirect ( message : " Not a valid implicit grant flow url: \( url ) " )
678
677
}
679
678
680
679
if configuration. flowType == . pkce, !isPKCEFlow( params: params) {
681
- throw AuthError . pkce ( . invalidPKCEFlowURL )
680
+ throw AuthError . pkceGrantCodeExchange ( message : " Not a valid PKCE flow url: \( url ) " )
682
681
}
683
682
684
683
if isPKCEFlow ( params: params) {
685
684
guard let code = params [ " code " ] else {
686
- throw AuthError . pkce ( . codeVerifierNotFound )
685
+ throw AuthError . pkceGrantCodeExchange ( message : " No code detected. " )
687
686
}
688
687
689
688
let session = try await exchangeCodeForSession ( authCode: code)
690
689
return session
691
690
}
692
691
693
- if let errorDescription = params [ " error_description " ] {
694
- throw AuthError . api ( . init( errorDescription: errorDescription) )
692
+ if params [ " error " ] != nil || params [ " error_description " ] != nil || params [ " error_code " ] != nil {
693
+ throw AuthError . pkceGrantCodeExchange (
694
+ message: params [ " error_description " ] ?? " Error in URL with unspecified error_description. " ,
695
+ error: params [ " error " ] ?? " unspecified_error " ,
696
+ code: params [ " error_code " ] ?? " unspecified_code "
697
+ )
695
698
}
696
699
697
700
guard
@@ -700,7 +703,7 @@ public final class AuthClient: Sendable {
700
703
let refreshToken = params [ " refresh_token " ] ,
701
704
let tokenType = params [ " token_type " ]
702
705
else {
703
- throw URLError ( . badURL )
706
+ throw AuthError . implicitGrantRedirect ( message : " No session defined in URL " )
704
707
}
705
708
706
709
let expiresAt = params [ " expires_at " ] . flatMap ( TimeInterval . init)
@@ -753,11 +756,9 @@ public final class AuthClient: Sendable {
753
756
var session : Session
754
757
755
758
let jwt = try decode ( jwt: accessToken)
756
- if let exp = jwt [ " exp " ] as? TimeInterval {
759
+ if let exp = jwt ? [ " exp " ] as? TimeInterval {
757
760
expiresAt = Date ( timeIntervalSince1970: exp)
758
761
hasExpired = expiresAt <= now
759
- } else {
760
- throw AuthError . missingExpClaim
761
762
}
762
763
763
764
if hasExpired {
@@ -803,16 +804,9 @@ public final class AuthClient: Sendable {
803
804
headers: [ . init( name: " Authorization " , value: " Bearer \( accessToken) " ) ]
804
805
)
805
806
)
806
- } catch {
807
+ } catch let AuthError . api ( _ , _ , _ , response ) where [ 404 , 403 , 401 ] . contains ( response . statusCode ) {
807
808
// ignore 404s since user might not exist anymore
808
809
// ignore 401s, and 403s since an invalid or expired JWT should sign out the current session.
809
- let ignoredCodes = Set ( [ 404 , 403 , 401 ] )
810
-
811
- if case let AuthError . api( apiError) = error, let code = apiError. code,
812
- !ignoredCodes. contains ( code)
813
- {
814
- throw error
815
- }
816
810
}
817
811
}
818
812
@@ -1169,7 +1163,7 @@ public final class AuthClient: Sendable {
1169
1163
@discardableResult
1170
1164
public func refreshSession( refreshToken: String ? = nil ) async throws -> Session {
1171
1165
guard let refreshToken = refreshToken ?? currentSession? . refreshToken else {
1172
- throw AuthError . sessionNotFound
1166
+ throw AuthError . sessionMissing
1173
1167
}
1174
1168
1175
1169
return try await sessionManager. refreshSession ( refreshToken)
0 commit comments