@@ -3,6 +3,7 @@ import 'dart:convert';
3
3
import 'dart:io' show File, HttpHeaders, HttpStatus;
4
4
5
5
import 'package:clerk_auth/src/clerk_api/token_cache.dart' ;
6
+ import 'package:clerk_auth/src/clerk_auth/auth_config.dart' ;
6
7
import 'package:clerk_auth/src/clerk_auth/http_service.dart' ;
7
8
import 'package:clerk_auth/src/clerk_auth/persistor.dart' ;
8
9
import 'package:clerk_auth/src/clerk_constants.dart' ;
@@ -15,65 +16,36 @@ import 'package:http/http.dart' as http;
15
16
16
17
export 'package:clerk_auth/src/models/enums.dart' show SessionTokenPollMode;
17
18
18
- /// Used by [Api] to locate the current user locale preference.
19
- typedef ClerkLocalesLookup = List <String > Function ();
20
-
21
19
/// [Api] manages communication with the Clerk frontend API
22
20
///
23
21
class Api with Logging {
24
- Api ._(
25
- this ._tokenCache,
26
- this ._domain,
27
- this ._httpService,
28
- this ._localesLookup,
29
- this ._pollMode,
30
- );
31
-
32
- /// Create an [Api] object for a given Publishable Key, or return the existing one
33
- /// if such already exists for that key. Requires a [publishableKey]
34
- /// found in the Clerk dashboard for you account. Additional arguments:
35
- ///
36
- /// [persistor] : an optional instance of a [Persistor] which will keep track of
37
- /// tokens and expiry between app activations
38
- ///
39
- /// [client] : an optional instance of [HttpService] to manage low-level communications
40
- /// with the back end. Injected for e.g. test mocking
41
- ///
42
- /// [pollMode] : session token poll mode, default [SessionTokenPollMode.lazy] ,
43
- /// manages how to refresh the [sessionToken] .
22
+ /// Create an [Api] object
44
23
///
45
- factory Api ({
46
- required String publishableKey ,
24
+ Api ({
25
+ required AuthConfig config ,
47
26
required Persistor persistor,
48
27
required HttpService httpService,
49
- required ClerkLocalesLookup localesLookup,
50
- SessionTokenPollMode pollMode = SessionTokenPollMode .lazy,
51
- }) =>
52
- Api ._(
53
- TokenCache (
28
+ }) : _config = config,
29
+ _tokenCache = TokenCache (
54
30
persistor: persistor,
55
- cacheId : publishableKey.hashCode ,
31
+ publishableKey : config.publishableKey ,
56
32
),
57
- _deriveDomainFrom (publishableKey),
58
- httpService,
59
- localesLookup,
60
- pollMode,
61
- );
33
+ _httpService = httpService,
34
+ _domain = _deriveDomainFrom (config.publishableKey),
35
+ _testMode = config.isTestMode;
62
36
37
+ final AuthConfig _config;
63
38
final TokenCache _tokenCache;
64
- final String _domain;
65
39
final HttpService _httpService;
66
- final ClerkLocalesLookup _localesLookup ;
67
- final SessionTokenPollMode _pollMode;
68
- late final String _nativeDeviceId ;
40
+ final String _domain ;
41
+
42
+ bool _testMode ;
69
43
Timer ? _pollTimer;
70
44
bool _multiSessionMode = true ;
71
- bool _testMode = ClerkConstants .isTestMode;
72
45
73
46
static const _kClerkAPIVersion = 'clerk-api-version' ;
74
47
static const _kClerkClientId = 'x-clerk-client-id' ;
75
48
static const _kClerkJsVersion = '_clerk_js_version' ;
76
- static const _kClerkNativeDeviceId = 'x-native-device-id' ;
77
49
static const _kClerkSessionId = '_clerk_session_id' ;
78
50
static const _kClientKey = 'client' ;
79
51
static const _kErrorsKey = 'errors' ;
@@ -90,8 +62,7 @@ class Api with Logging {
90
62
/// Initialise the API
91
63
Future <void > initialize () async {
92
64
await _tokenCache.initialize ();
93
- _nativeDeviceId = 'PENDING' ; // TODO get this value
94
- if (_pollMode == SessionTokenPollMode .hungry) {
65
+ if (_config.sessionTokenPollMode == SessionTokenPollMode .hungry) {
95
66
await _pollForSessionToken ();
96
67
}
97
68
}
@@ -115,7 +86,7 @@ class Api with Logging {
115
86
final body = json.decode (resp.body) as Map <String , dynamic >;
116
87
final env = Environment .fromJson (body);
117
88
118
- _testMode = env.config.testMode || ClerkConstants .isTestMode;
89
+ _testMode = env.config.testMode && _config .isTestMode;
119
90
_multiSessionMode = env.config.singleSessionMode == false ;
120
91
121
92
return env;
@@ -492,7 +463,7 @@ class Api with Logging {
492
463
493
464
/// Update details pertaining to the current [User]
494
465
///
495
- Future <ApiResponse > updateUser (User user, AuthConfig config) async {
466
+ Future <ApiResponse > updateUser (User user, Config config) async {
496
467
return await _fetchApiResponse (
497
468
'/me' ,
498
469
method: HttpMethod .patch,
@@ -902,7 +873,7 @@ class Api with Logging {
902
873
}) {
903
874
return {
904
875
HttpHeaders .acceptHeader: 'application/json' ,
905
- HttpHeaders .acceptLanguageHeader: _localesLookup ().join (', ' ),
876
+ HttpHeaders .acceptLanguageHeader: _config. localesLookup ().join (', ' ),
906
877
HttpHeaders .contentTypeHeader: method.isGet
907
878
? 'application/json'
908
879
: 'application/x-www-form-urlencoded' ,
@@ -912,7 +883,6 @@ class Api with Logging {
912
883
_kXFlutterSDKVersion: ClerkConstants .flutterSdkVersion,
913
884
if (_testMode) //
914
885
_kClerkClientId: _tokenCache.clientId,
915
- _kClerkNativeDeviceId: _nativeDeviceId,
916
886
_kXMobile: '1' ,
917
887
...? headers,
918
888
};
0 commit comments