@@ -5,7 +5,7 @@ import Foundation
5
5
import FoundationNetworking
6
6
#endif
7
7
8
- public actor GoTrueClient {
8
+ public actor AuthClient {
9
9
/// FetchHandler is a type alias for asynchronous network request handling.
10
10
public typealias FetchHandler =
11
11
@Sendable ( _ request: URLRequest ) async throws -> ( Data , URLResponse )
@@ -15,45 +15,36 @@ public actor GoTrueClient {
15
15
public let url : URL
16
16
public var headers : [ String : String ]
17
17
public let flowType : AuthFlowType
18
- public let localStorage : GoTrueLocalStorage
18
+ public let localStorage : AuthLocalStorage
19
19
public let encoder : JSONEncoder
20
20
public let decoder : JSONDecoder
21
21
public let fetch : FetchHandler
22
22
23
- /// Initializes a GoTrueClient Configuration with optional parameters.
23
+ /// Initializes a AuthClient Configuration with optional parameters.
24
24
///
25
25
/// - Parameters:
26
- /// - url: The base URL of the GoTrue server.
27
- /// - headers: (Optional) Custom headers to be included in requests.
28
- /// - flowType: (Optional) The authentication flow type. Default is `.implicit`.
29
- /// - localStorage: (Optional) The storage mechanism for local data. Default is a
30
- /// KeychainLocalStorage.
31
- /// - encoder: (Optional) The JSON encoder to use for encoding requests.
32
- /// - decoder: (Optional) The JSON decoder to use for decoding responses.
33
- /// - fetch: (Optional) The asynchronous fetch handler for network requests.
26
+ /// - url: The base URL of the Auth server.
27
+ /// - headers: Custom headers to be included in requests.
28
+ /// - flowType: The authentication flow type.
29
+ /// - localStorage: The storage mechanism for local data.
30
+ /// - encoder: The JSON encoder to use for encoding requests.
31
+ /// - decoder: The JSON decoder to use for decoding responses.
32
+ /// - fetch: The asynchronous fetch handler for network requests.
34
33
public init (
35
34
url: URL ,
36
35
headers: [ String : String ] = [ : ] ,
37
- flowType: AuthFlowType = . implicit ,
38
- localStorage: GoTrueLocalStorage ? = nil ,
39
- encoder: JSONEncoder = . goTrue ,
40
- decoder: JSONDecoder = . goTrue ,
36
+ flowType: AuthFlowType = Configuration . defaultFlowType ,
37
+ localStorage: AuthLocalStorage = Configuration . defaultLocalStorage ,
38
+ encoder: JSONEncoder = AuthClient . Configuration . jsonEncoder ,
39
+ decoder: JSONDecoder = AuthClient . Configuration . jsonDecoder ,
41
40
fetch: @escaping FetchHandler = { try await URLSession . shared. data ( for: $0) }
42
41
) {
43
- var headers = headers
44
- if headers [ " X-Client-Info " ] == nil {
45
- headers [ " X-Client-Info " ] = " gotrue-swift/ \( version) "
46
- }
42
+ let headers = headers. merging ( Configuration . defaultHeaders) { l, _ in l }
47
43
48
44
self . url = url
49
45
self . headers = headers
50
46
self . flowType = flowType
51
- self . localStorage =
52
- localStorage
53
- ?? KeychainLocalStorage (
54
- service: " supabase.gotrue.swift " ,
55
- accessGroup: nil
56
- )
47
+ self . localStorage = localStorage
57
48
self . encoder = encoder
58
49
self . decoder = decoder
59
50
self . fetch = fetch
@@ -82,34 +73,33 @@ public actor GoTrueClient {
82
73
83
74
/// Returns the session, refreshing it if necessary.
84
75
///
85
- /// If no session can be found, a ``GoTrueError /sessionNotFound`` error is thrown.
76
+ /// If no session can be found, a ``AuthError /sessionNotFound`` error is thrown.
86
77
public var session : Session {
87
78
get async throws {
88
79
try await sessionManager. session ( )
89
80
}
90
81
}
91
82
92
83
/// Namespace for accessing multi-factor authentication API.
93
- public let mfa : GoTrueMFA
84
+ public let mfa : AuthMFA
94
85
95
- /// Initializes a GoTrueClient with optional parameters.
86
+ /// Initializes a AuthClient with optional parameters.
96
87
///
97
88
/// - Parameters:
98
- /// - url: The base URL of the GoTrue server.
99
- /// - headers: (Optional) Custom headers to be included in requests.
100
- /// - flowType: (Optional) The authentication flow type. Default is `.implicit`.
101
- /// - localStorage: (Optional) The storage mechanism for local data. Default is a
102
- /// KeychainLocalStorage.
103
- /// - encoder: (Optional) The JSON encoder to use for encoding requests.
104
- /// - decoder: (Optional) The JSON decoder to use for decoding responses.
105
- /// - fetch: (Optional) The asynchronous fetch handler for network requests.
89
+ /// - url: The base URL of the Auth server.
90
+ /// - headers: Custom headers to be included in requests.
91
+ /// - flowType: The authentication flow type..
92
+ /// - localStorage: The storage mechanism for local data..
93
+ /// - encoder: The JSON encoder to use for encoding requests.
94
+ /// - decoder: The JSON decoder to use for decoding responses.
95
+ /// - fetch: The asynchronous fetch handler for network requests.
106
96
public init (
107
97
url: URL ,
108
98
headers: [ String : String ] = [ : ] ,
109
- flowType: AuthFlowType = . implicit ,
110
- localStorage: GoTrueLocalStorage ? = nil ,
111
- encoder: JSONEncoder = . goTrue ,
112
- decoder: JSONDecoder = . goTrue ,
99
+ flowType: AuthFlowType = AuthClient . Configuration . defaultFlowType ,
100
+ localStorage: AuthLocalStorage = AuthClient . Configuration . defaultLocalStorage ,
101
+ encoder: JSONEncoder = AuthClient . Configuration . jsonEncoder ,
102
+ decoder: JSONDecoder = AuthClient . Configuration . jsonDecoder ,
113
103
fetch: @escaping FetchHandler = { try await URLSession . shared. data ( for: $0) }
114
104
) {
115
105
self . init (
@@ -125,7 +115,7 @@ public actor GoTrueClient {
125
115
)
126
116
}
127
117
128
- /// Initializes a GoTrueClient with a specific configuration.
118
+ /// Initializes a AuthClient with a specific configuration.
129
119
///
130
120
/// - Parameters:
131
121
/// - configuration: The client configuration.
@@ -151,7 +141,7 @@ public actor GoTrueClient {
151
141
eventEmitter: EventEmitter ,
152
142
sessionStorage: SessionStorage
153
143
) {
154
- mfa = GoTrueMFA ( )
144
+ mfa = AuthMFA ( )
155
145
156
146
Dependencies . current. setValue (
157
147
Dependencies (
@@ -213,7 +203,7 @@ public actor GoTrueClient {
213
203
email: email,
214
204
password: password,
215
205
data: data,
216
- gotrueMetaSecurity: captchaToken. map ( GoTrueMetaSecurity . init ( captchaToken: ) ) ,
206
+ gotrueMetaSecurity: captchaToken. map ( AuthMetaSecurity . init ( captchaToken: ) ) ,
217
207
codeChallenge: codeChallenge,
218
208
codeChallengeMethod: codeChallengeMethod
219
209
)
@@ -243,7 +233,7 @@ public actor GoTrueClient {
243
233
password: password,
244
234
phone: phone,
245
235
data: data,
246
- gotrueMetaSecurity: captchaToken. map ( GoTrueMetaSecurity . init ( captchaToken: ) )
236
+ gotrueMetaSecurity: captchaToken. map ( AuthMetaSecurity . init ( captchaToken: ) )
247
237
)
248
238
)
249
239
)
@@ -359,7 +349,7 @@ public actor GoTrueClient {
359
349
email: email,
360
350
createUser: shouldCreateUser,
361
351
data: data,
362
- gotrueMetaSecurity: captchaToken. map ( GoTrueMetaSecurity . init ( captchaToken: ) ) ,
352
+ gotrueMetaSecurity: captchaToken. map ( AuthMetaSecurity . init ( captchaToken: ) ) ,
363
353
codeChallenge: codeChallenge,
364
354
codeChallengeMethod: codeChallengeMethod
365
355
)
@@ -391,7 +381,7 @@ public actor GoTrueClient {
391
381
phone: phone,
392
382
createUser: shouldCreateUser,
393
383
data: data,
394
- gotrueMetaSecurity: captchaToken. map ( GoTrueMetaSecurity . init ( captchaToken: ) )
384
+ gotrueMetaSecurity: captchaToken. map ( AuthMetaSecurity . init ( captchaToken: ) )
395
385
)
396
386
)
397
387
)
@@ -401,7 +391,7 @@ public actor GoTrueClient {
401
391
/// Log in an existing user by exchanging an Auth Code issued during the PKCE flow.
402
392
public func exchangeCodeForSession( authCode: String ) async throws -> Session {
403
393
guard let codeVerifier = try codeVerifierStorage. getCodeVerifier ( ) else {
404
- throw GoTrueError . pkce ( . codeVerifierNotFound)
394
+ throw AuthError . pkce ( . codeVerifierNotFound)
405
395
}
406
396
do {
407
397
let session : Session = try await api. execute (
@@ -482,26 +472,26 @@ public actor GoTrueClient {
482
472
@discardableResult
483
473
public func session( from url: URL ) async throws -> Session {
484
474
if configuration. flowType == . implicit, !isImplicitGrantFlow( url: url) {
485
- throw GoTrueError . invalidImplicitGrantFlowURL
475
+ throw AuthError . invalidImplicitGrantFlowURL
486
476
}
487
477
488
478
if configuration. flowType == . pkce, !isPKCEFlow( url: url) {
489
- throw GoTrueError . pkce ( . invalidPKCEFlowURL)
479
+ throw AuthError . pkce ( . invalidPKCEFlowURL)
490
480
}
491
481
492
482
let params = extractParams ( from: url)
493
483
494
484
if isPKCEFlow ( url: url) {
495
485
guard let code = params. first ( where: { $0. name == " code " } ) ? . value else {
496
- throw GoTrueError . pkce ( . codeVerifierNotFound)
486
+ throw AuthError . pkce ( . codeVerifierNotFound)
497
487
}
498
488
499
489
let session = try await exchangeCodeForSession ( authCode: code)
500
490
return session
501
491
}
502
492
503
493
if let errorDescription = params. first ( where: { $0. name == " error_description " } ) ? . value {
504
- throw GoTrueError . api ( . init( errorDescription: errorDescription) )
494
+ throw AuthError . api ( . init( errorDescription: errorDescription) )
505
495
}
506
496
507
497
guard
@@ -565,7 +555,7 @@ public actor GoTrueClient {
565
555
expiresAt = Date ( timeIntervalSince1970: exp)
566
556
hasExpired = expiresAt <= now
567
557
} else {
568
- throw GoTrueError . missingExpClaim
558
+ throw AuthError . missingExpClaim
569
559
}
570
560
571
561
if hasExpired {
@@ -607,7 +597,7 @@ public actor GoTrueClient {
607
597
// ignore 401s since an invalid or expired JWT should sign out the current session
608
598
let ignoredCodes = Set ( [ 404 , 401 ] )
609
599
610
- if case let GoTrueError . api( apiError) = error, let code = apiError. code,
600
+ if case let AuthError . api( apiError) = error, let code = apiError. code,
611
601
!ignoredCodes. contains ( code)
612
602
{
613
603
throw error
@@ -642,7 +632,7 @@ public actor GoTrueClient {
642
632
email: email,
643
633
token: token,
644
634
type: type,
645
- gotrueMetaSecurity: captchaToken. map ( GoTrueMetaSecurity . init ( captchaToken: ) )
635
+ gotrueMetaSecurity: captchaToken. map ( AuthMetaSecurity . init ( captchaToken: ) )
646
636
)
647
637
)
648
638
)
@@ -669,7 +659,7 @@ public actor GoTrueClient {
669
659
phone: phone,
670
660
token: token,
671
661
type: type,
672
- gotrueMetaSecurity: captchaToken. map ( GoTrueMetaSecurity . init ( captchaToken: ) )
662
+ gotrueMetaSecurity: captchaToken. map ( AuthMetaSecurity . init ( captchaToken: ) )
673
663
)
674
664
)
675
665
)
@@ -755,7 +745,7 @@ public actor GoTrueClient {
755
745
body: configuration. encoder. encode (
756
746
RecoverParams (
757
747
email: email,
758
- gotrueMetaSecurity: captchaToken. map ( GoTrueMetaSecurity . init ( captchaToken: ) ) ,
748
+ gotrueMetaSecurity: captchaToken. map ( AuthMetaSecurity . init ( captchaToken: ) ) ,
759
749
codeChallenge: codeChallenge,
760
750
codeChallengeMethod: codeChallengeMethod
761
751
)
@@ -841,17 +831,17 @@ public actor GoTrueClient {
841
831
}
842
832
}
843
833
844
- extension GoTrueClient {
834
+ extension AuthClient {
845
835
/// Notification posted when an auth state event is triggered.
846
836
public static let didChangeAuthStateNotification = Notification . Name (
847
- " GoTrueClient .didChangeAuthStateNotification"
837
+ " AuthClient .didChangeAuthStateNotification"
848
838
)
849
839
850
840
/// A user info key to retrieve the ``AuthChangeEvent`` value for a
851
- /// ``GoTrueClient /didChangeAuthStateNotification`` notification.
852
- public static let authChangeEventInfoKey = " GoTrueClient .authChangeEvent"
841
+ /// ``AuthClient /didChangeAuthStateNotification`` notification.
842
+ public static let authChangeEventInfoKey = " AuthClient .authChangeEvent"
853
843
854
844
/// A user info key to retrieve the ``Session`` value for a
855
- /// ``GoTrueClient /didChangeAuthStateNotification`` notification.
856
- public static let authChangeSessionInfoKey = " GoTrueClient .authChangeSession"
845
+ /// ``AuthClient /didChangeAuthStateNotification`` notification.
846
+ public static let authChangeSessionInfoKey = " AuthClient .authChangeSession"
857
847
}
0 commit comments