react-native-okhi 1.2.20 → 1.2.21-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/android/build.gradle +2 -2
  2. package/android/local.properties +8 -0
  3. package/android/src/main/java/com/reactnativeokhi/OkhiModule.java +13 -18
  4. package/ios/Okhi.m +9 -4
  5. package/ios/Okhi.swift +34 -17
  6. package/lib/commonjs/OkCollect/OkHiLocationManager.js +37 -59
  7. package/lib/commonjs/OkCollect/OkHiLocationManager.js.map +1 -1
  8. package/lib/commonjs/OkCollect/Util.js +14 -7
  9. package/lib/commonjs/OkCollect/Util.js.map +1 -1
  10. package/lib/commonjs/OkCollect/app.json +1 -1
  11. package/lib/commonjs/OkCollect/types.js.map +1 -1
  12. package/lib/commonjs/OkCore/index.js +1 -1
  13. package/lib/commonjs/OkCore/index.js.map +1 -1
  14. package/lib/commonjs/OkCore/types.js.map +1 -1
  15. package/lib/commonjs/OkHiNativeModule/index.js.map +1 -1
  16. package/lib/commonjs/OkVerify/index.js +27 -27
  17. package/lib/commonjs/OkVerify/index.js.map +1 -1
  18. package/lib/module/OkCollect/OkHiLocationManager.js +37 -59
  19. package/lib/module/OkCollect/OkHiLocationManager.js.map +1 -1
  20. package/lib/module/OkCollect/Util.js +14 -7
  21. package/lib/module/OkCollect/Util.js.map +1 -1
  22. package/lib/module/OkCollect/app.json +1 -1
  23. package/lib/module/OkCollect/types.js.map +1 -1
  24. package/lib/module/OkCore/index.js +1 -1
  25. package/lib/module/OkCore/index.js.map +1 -1
  26. package/lib/module/OkCore/types.js.map +1 -1
  27. package/lib/module/OkHiNativeModule/index.js.map +1 -1
  28. package/lib/module/OkVerify/index.js +27 -27
  29. package/lib/module/OkVerify/index.js.map +1 -1
  30. package/lib/typescript/OkCollect/Util.d.ts +1 -1
  31. package/lib/typescript/OkCollect/types.d.ts +6 -4
  32. package/lib/typescript/OkCore/types.d.ts +11 -3
  33. package/lib/typescript/OkHiNativeModule/index.d.ts +1 -1
  34. package/lib/typescript/OkVerify/index.d.ts +4 -4
  35. package/package.json +1 -1
  36. package/react-native-okhi.podspec +1 -1
  37. package/src/OkCollect/OkHiLocationManager.tsx +52 -83
  38. package/src/OkCollect/Util.ts +13 -8
  39. package/src/OkCollect/app.json +1 -1
  40. package/src/OkCollect/types.ts +7 -4
  41. package/src/OkCore/index.ts +1 -1
  42. package/src/OkCore/types.ts +15 -3
  43. package/src/OkHiNativeModule/index.ts +3 -1
  44. package/src/OkVerify/index.ts +46 -54
  45. package/lib/commonjs/OkCore/OkHiAuth.js +0 -109
  46. package/lib/commonjs/OkCore/OkHiAuth.js.map +0 -1
  47. package/lib/module/OkCore/OkHiAuth.js +0 -102
  48. package/lib/module/OkCore/OkHiAuth.js.map +0 -1
  49. package/lib/typescript/OkCore/OkHiAuth.d.ts +0 -16
  50. package/src/OkCore/OkHiAuth.ts +0 -114
@@ -14,7 +14,6 @@ import {
14
14
  parseOkHiLocation,
15
15
  } from './Util';
16
16
  import { OkHiException } from '../OkCore/OkHiException';
17
- import { OkHiAuth } from '../OkCore/OkHiAuth';
18
17
  import type { AuthApplicationConfig } from '../OkCore/_types';
19
18
  import { start as sv } from '../OkVerify';
20
19
  import {
@@ -25,7 +24,7 @@ import {
25
24
  openProtectedAppsSettings,
26
25
  requestBackgroundLocationPermission,
27
26
  requestLocationPermission,
28
- VerificationType,
27
+ UsageType,
29
28
  } from '../OkCore';
30
29
  import { OkHiNativeModule } from '../OkHiNativeModule';
31
30
 
@@ -33,7 +32,6 @@ import { OkHiNativeModule } from '../OkHiNativeModule';
33
32
  * The OkHiLocationManager React Component is used to display an in app modal, enabling the user to quickly create an accurate OkHi address.
34
33
  */
35
34
  export const OkHiLocationManager = (props: OkHiLocationManagerProps) => {
36
- const [token, setToken] = useState<string | null>(null);
37
35
  const [applicationConfiguration, setApplicationConfiguration] =
38
36
  useState<AuthApplicationConfig | null>(null);
39
37
  const [startPayload, setStartPaylaod] =
@@ -43,88 +41,49 @@ export const OkHiLocationManager = (props: OkHiLocationManagerProps) => {
43
41
  ? { ...props.style, ...defaultStyle }
44
42
  : defaultStyle;
45
43
 
46
- const { user, onSuccess, onCloseRequest, onError, loader, launch, config } =
47
- props;
44
+ const { user, onSuccess, onCloseRequest, onError, loader, launch } = props;
48
45
  const webViewRef = useRef<WebView | null>(null);
49
46
  const startMessage =
50
47
  props.mode === 'create' ? 'start_app' : 'select_location';
51
48
  const [ready, setReady] = useState(false);
52
49
 
53
50
  useEffect(() => {
54
- if (applicationConfiguration == null && token == null && user.phone) {
55
- getApplicationConfiguration()
56
- .then((config) => {
57
- if (!config && launch) {
58
- onError(
59
- new OkHiException({
60
- code: OkHiException.UNAUTHORIZED_CODE,
61
- message: OkHiException.UNAUTHORIZED_MESSAGE,
62
- })
63
- );
64
- } else if (config) {
65
- setApplicationConfiguration(config);
66
- const auth = new OkHiAuth();
67
- auth
68
- .anonymousSignInWithPhoneNumber(user.phone, ['verify'], config)
69
- .then(setToken)
70
- .catch(onError);
71
- }
72
- })
73
- .catch((error) => {
74
- if (launch) {
75
- onError(error);
76
- }
77
- });
51
+ if (launch) {
52
+ setReady(launch);
78
53
  }
79
- }, [onError, user.phone, launch, applicationConfiguration, token]);
54
+ }, [launch]);
80
55
 
81
56
  useEffect(() => {
82
- if (token !== null && applicationConfiguration !== null) {
83
- // TODO: handle faliure
84
- generateStartDataPayload(props, token, applicationConfiguration)
85
- .then((startPayload) => {
86
- setStartPaylaod(startPayload);
87
- if (Platform.OS === 'android' && Platform.Version > 25) {
88
- OkHiNativeModule.setItem(
89
- 'okcollect-launch-payload',
90
- JSON.stringify({
91
- message: startMessage,
92
- payload: startPayload,
93
- url: getFrameUrl(applicationConfiguration),
94
- })
95
- ).catch(console.error);
96
- }
97
- })
98
- .catch(console.error);
57
+ async function startOkHiLocationManager() {
58
+ const appConfiguration = await getApplicationConfiguration();
59
+ if (!appConfiguration) {
60
+ throw new OkHiException({
61
+ code: OkHiException.UNAUTHORIZED_CODE,
62
+ message: OkHiException.UNAUTHORIZED_MESSAGE,
63
+ });
64
+ }
65
+ const startPayload = await generateStartDataPayload(
66
+ props,
67
+ appConfiguration
68
+ );
69
+ setApplicationConfiguration(appConfiguration);
70
+ setStartPaylaod(startPayload);
71
+ if (Platform.OS === 'android' && Platform.Version > 25) {
72
+ OkHiNativeModule.setItem(
73
+ 'okcollect-launch-payload',
74
+ JSON.stringify({
75
+ message: startMessage,
76
+ payload: startPayload,
77
+ url: getFrameUrl(appConfiguration),
78
+ })
79
+ ).catch(console.error);
80
+ }
99
81
  }
100
- }, [applicationConfiguration, props, token]);
101
82
 
102
- useEffect(() => {
103
83
  if (launch) {
104
- if (
105
- typeof config?.permissionsOnboarding === 'boolean' &&
106
- !config.permissionsOnboarding
107
- ) {
108
- isBackgroundLocationPermissionGranted().then((result) => {
109
- if (!result) {
110
- onError(
111
- new OkHiException({
112
- code: OkHiException.PERMISSION_DENIED_CODE,
113
- message:
114
- 'Always location permission must be granted to launch OkCollect',
115
- })
116
- );
117
- } else {
118
- setReady(true);
119
- }
120
- });
121
- } else {
122
- setReady(true);
123
- }
124
- } else {
125
- setReady(false);
84
+ startOkHiLocationManager();
126
85
  }
127
- }, [launch, config?.permissionsOnboarding]);
86
+ }, [launch, props, onError]);
128
87
 
129
88
  const runWebViewCallback = (value: string) => {
130
89
  if (webViewRef.current) {
@@ -241,18 +200,32 @@ export const OkHiLocationManager = (props: OkHiLocationManagerProps) => {
241
200
  message: 'Missing location id from response',
242
201
  })
243
202
  );
203
+ } else if (!createdUser.id) {
204
+ reject(
205
+ new OkHiException({
206
+ code: OkHiException.BAD_REQUEST_CODE,
207
+ message: 'Missing user id from response',
208
+ })
209
+ );
210
+ } else if (!createdUser.token) {
211
+ reject(
212
+ new OkHiException({
213
+ code: OkHiException.UNAUTHORIZED_CODE,
214
+ message: OkHiException.UNAUTHORIZED_MESSAGE,
215
+ })
216
+ );
244
217
  } else {
245
- const verificationTypes: VerificationType = Array.isArray(
246
- location.verificationTypes
247
- )
248
- ? location.verificationTypes
249
- : ['digital'];
218
+ const usageTypes: UsageType = Array.isArray(location.usageTypes)
219
+ ? location.usageTypes
220
+ : ['digital_verification'];
250
221
  sv(
222
+ createdUser.token,
251
223
  createdUser.phone,
224
+ createdUser.id,
252
225
  location.id,
253
226
  location.lat,
254
227
  location.lon,
255
- verificationTypes
228
+ usageTypes
256
229
  )
257
230
  .then(resolve)
258
231
  .catch(reject);
@@ -293,11 +266,7 @@ export const OkHiLocationManager = (props: OkHiLocationManagerProps) => {
293
266
  };
294
267
 
295
268
  const renderContent = () => {
296
- if (token === null || applicationConfiguration == null) {
297
- return loader || <Spinner />;
298
- }
299
-
300
- if (startPayload === null) {
269
+ if (startPayload === null || applicationConfiguration == null) {
301
270
  return loader || <Spinner />;
302
271
  }
303
272
 
@@ -30,7 +30,6 @@ const fetchCurrentLocation = async (): Promise<null | {
30
30
  */
31
31
  export const generateStartDataPayload = async (
32
32
  props: OkHiLocationManagerProps,
33
- authToken: string,
34
33
  applicationConfiguration: AuthApplicationConfig
35
34
  ): Promise<OkHiLocationManagerStartDataPayload> => {
36
35
  const payload: any = {};
@@ -54,9 +53,11 @@ export const generateStartDataPayload = async (
54
53
  firstName: props.user.firstName,
55
54
  lastName: props.user.lastName,
56
55
  email: props.user.email,
56
+ appUserId: props.user.appUserId,
57
57
  };
58
58
  payload.auth = {
59
- authToken,
59
+ branchId: applicationConfiguration.credentials.branchId,
60
+ clientKey: applicationConfiguration.credentials.clientKey,
60
61
  };
61
62
  payload.context = {
62
63
  container: {
@@ -101,11 +102,8 @@ export const generateStartDataPayload = async (
101
102
  },
102
103
  protectedApps:
103
104
  Platform.OS === 'android' && (await canOpenProtectedAppsSettings()),
104
- permissionsOnboarding:
105
- typeof props.config?.permissionsOnboarding === 'boolean'
106
- ? props.config.permissionsOnboarding
107
- : true,
108
- verificationTypes: props.config?.verificationTypes || ['digital'],
105
+ permissionsOnboarding: true,
106
+ usageTypes: props.config?.usageTypes || ['digital_verification'],
109
107
  };
110
108
 
111
109
  if (Platform.OS === 'ios') {
@@ -171,6 +169,13 @@ export const generateStartDataPayload = async (
171
169
  message: OkHiException.UNAUTHORIZED_MESSAGE,
172
170
  });
173
171
  }
172
+
173
+ if (props.location?.id) {
174
+ payload.location = {
175
+ id: props.location.id,
176
+ };
177
+ }
178
+
174
179
  return payload;
175
180
  };
176
181
 
@@ -256,6 +261,6 @@ export const parseOkHiLocation = (location: any): OkHiLocation => {
256
261
  state: location?.state,
257
262
  city: location?.city,
258
263
  countryCode: location?.country_code,
259
- verificationTypes: location?.verification_types,
264
+ usageTypes: location?.usage_types,
260
265
  };
261
266
  };
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "name": "okhiWebReactNative",
3
- "version": "1.2.20"
3
+ "version": "1.2.21-beta.2"
4
4
  }
@@ -1,6 +1,6 @@
1
1
  import type { ViewStyle } from 'react-native';
2
2
  import type { OkHiException } from '../OkCore/OkHiException';
3
- import type { OkHiUser, OkHiLocation, VerificationType } from '../OkCore/types';
3
+ import type { OkHiUser, OkHiLocation, UsageType } from '../OkCore/types';
4
4
 
5
5
  /**
6
6
  * The OkCollect Success Response object contains information about the newly created user and location once an address has been successfully created.
@@ -78,14 +78,17 @@ export interface OkHiLocationManagerProps {
78
78
  home?: boolean;
79
79
  work?: boolean;
80
80
  };
81
- permissionsOnboarding?: boolean;
82
- verificationTypes?: VerificationType;
81
+ usageTypes?: UsageType;
83
82
  };
84
83
 
85
84
  /**
86
85
  * **Optional:** Enable a user to either select an existing address, or force to create a new one
87
86
  */
88
87
  mode?: 'create' | 'select';
88
+
89
+ location?: {
90
+ id: string;
91
+ };
89
92
  }
90
93
 
91
94
  /**
@@ -124,7 +127,7 @@ export interface OkHiLocationManagerStartDataPayload {
124
127
  color?: string;
125
128
  visible?: boolean;
126
129
  };
127
- verificationTypes: VerificationType;
130
+ usageTypes: UsageType;
128
131
  };
129
132
  user: OkHiUser;
130
133
  }
@@ -42,6 +42,7 @@ export function initialize(
42
42
  message: 'Invalid OkHi configuration provided.',
43
43
  });
44
44
  }
45
+ okhiApplicationConfiguration = configuration;
45
46
  if (Platform.OS === 'ios') {
46
47
  await OkHiNativeModule.initializeIOS(
47
48
  configuration.credentials.branchId,
@@ -54,7 +55,6 @@ export function initialize(
54
55
  } else {
55
56
  await OkHiNativeModule.initialize(JSON.stringify(configuration));
56
57
  }
57
- okhiApplicationConfiguration = configuration;
58
58
  });
59
59
  }
60
60
 
@@ -29,6 +29,16 @@ export interface OkHiUser {
29
29
  * The user's device firebase push notification token.
30
30
  */
31
31
  fcmPushNotificationToken?: string;
32
+
33
+ /**
34
+ * The user's jwt token provided by OkHi after address creation.
35
+ */
36
+ token?: string;
37
+
38
+ /**
39
+ * Your assigned user id
40
+ */
41
+ appUserId?: string;
32
42
  }
33
43
 
34
44
  /**
@@ -164,9 +174,9 @@ export interface OkHiLocation {
164
174
  countryCode?: string;
165
175
 
166
176
  /**
167
- * An array of verification types that specifies the mode of verification. Can include "physical" and/or "digital" as valid values.
177
+ * An array of usage types that specifies the mode of verification. Can include "physical_verification" and/or "digital_verification" as valid values.
168
178
  */
169
- verificationTypes?: VerificationType;
179
+ usageTypes?: UsageType;
170
180
  }
171
181
 
172
182
  /**
@@ -219,4 +229,6 @@ export type LocationPermissionCallback = (
219
229
  error: OkHiException | null
220
230
  ) => any;
221
231
 
222
- export type VerificationType = Array<'physical' | 'digital'>;
232
+ export type UsageType = Array<
233
+ 'physical_verification' | 'digital_verification' | 'address_book'
234
+ >;
@@ -19,11 +19,13 @@ type OkHiNativeModuleType = {
19
19
  getAuthToken(branchId: string, clientKey: string): Promise<string>;
20
20
  initialize(configuration: string): Promise<void>;
21
21
  startAddressVerification(
22
+ token: string,
22
23
  phoneNumber: string,
24
+ userId: string,
23
25
  locationId: string,
24
26
  lat: Number,
25
27
  lon: Number,
26
- verificationTypes: string[]
28
+ usageTypes: string[]
27
29
  ): Promise<string>;
28
30
  stopAddressVerification(
29
31
  phoneNumber: string,
@@ -13,45 +13,39 @@ import { errorHandler, isValidPlatform } from '../OkCore/_helpers';
13
13
  import { OkHiNativeModule } from '../OkHiNativeModule';
14
14
  import type { OkCollectSuccessResponse } from '../OkCollect/types';
15
15
  import { OkHiException } from '../OkCore/OkHiException';
16
- import type { VerificationType } from '../OkCore';
16
+ import type { UsageType } from '../OkCore';
17
17
  /**
18
18
  * Starts verification for a particular address
19
19
  * @param {string} phoneNumber A users phone number
20
20
  * @param {string} locationId An OkHi location identifier obtained after successfull creation of addresses.
21
21
  * @param {number} lat The latitude of the created address
22
22
  * @param {number} lon The longitude of the created address
23
- * @param verificationTypes - Optional. An array of verification types that specifies the mode of verification.
23
+ * @param usageTypes - Optional. An array of verification types that specifies the mode of verification.
24
24
  * Can include "physical" and/or "digital" as valid values.
25
25
  * @returns {Promise<string>} A promise that resolves to a string value of the location identifier
26
26
  */
27
27
  export const start = (
28
+ token: string,
28
29
  phoneNumber: string,
30
+ okhiUserId: string,
29
31
  locationId: string,
30
32
  lat: number,
31
33
  lon: number,
32
- verificationTypes?: VerificationType
34
+ usageTypes?: UsageType
33
35
  ) => {
34
36
  return isValidPlatform(() => {
35
- const vtypes: VerificationType = Array.isArray(verificationTypes)
36
- ? verificationTypes
37
- : ['physical'];
38
- if (Platform.OS === 'android') {
39
- return OkHiNativeModule.startAddressVerification(
40
- phoneNumber,
41
- locationId,
42
- lat,
43
- lon,
44
- vtypes
45
- );
46
- } else {
47
- return OkHiNativeModule.startAddressVerification(
48
- phoneNumber,
49
- locationId,
50
- lat,
51
- lon,
52
- vtypes
53
- );
54
- }
37
+ const utypes: UsageType = Array.isArray(usageTypes)
38
+ ? usageTypes
39
+ : ['digital_verification'];
40
+ return OkHiNativeModule.startAddressVerification(
41
+ token,
42
+ phoneNumber,
43
+ okhiUserId,
44
+ locationId,
45
+ lat,
46
+ lon,
47
+ utypes
48
+ );
55
49
  });
56
50
  };
57
51
 
@@ -60,41 +54,39 @@ export const start = (
60
54
  * @param {Object} response Response returned by OkCollect
61
55
  * @returns {Promise<string>} A promise that resolves to a string value of the location identifier
62
56
  */
63
- export const startVerification = async (response: OkCollectSuccessResponse) => {
64
- return new Promise((resolve, reject) => {
65
- const { location, user } = response;
66
- const verificationTypes: VerificationType = Array.isArray(
67
- response.location.verificationTypes
68
- )
69
- ? response.location.verificationTypes
70
- : ['digital'];
71
- if (location.id) {
72
- if (Platform.OS === 'android') {
73
- const result = OkHiNativeModule.startAddressVerification(
74
- user.phone,
75
- location.id,
76
- location.lat,
77
- location.lon,
78
- verificationTypes
79
- );
80
- resolve(result);
81
- } else {
82
- const result = OkHiNativeModule.startAddressVerification(
83
- user.phone,
84
- location.id,
85
- location.lat,
86
- location.lon,
87
- verificationTypes
88
- );
89
- resolve(result);
57
+ export const startVerification = (
58
+ response: OkCollectSuccessResponse
59
+ ): Promise<string> => {
60
+ return new Promise(async (resolve, reject) => {
61
+ try {
62
+ const { location, user } = response;
63
+ if (!user.token || !user.id) {
64
+ throw new OkHiException({
65
+ code: OkHiException.UNAUTHORIZED_CODE,
66
+ message: OkHiException.UNAUTHORIZED_MESSAGE,
67
+ });
90
68
  }
91
- } else {
92
- reject(
93
- new OkHiException({
69
+ if (!location.id) {
70
+ throw new OkHiException({
94
71
  code: OkHiException.BAD_REQUEST_CODE,
95
72
  message: 'Missing location id from response',
96
- })
73
+ });
74
+ }
75
+ const usageTypes: UsageType = Array.isArray(response.location.usageTypes)
76
+ ? response.location.usageTypes
77
+ : ['digital_verification'];
78
+ const result = await OkHiNativeModule.startAddressVerification(
79
+ user.token,
80
+ user.phone,
81
+ user.id,
82
+ location.id,
83
+ location.lat,
84
+ location.lon,
85
+ usageTypes
97
86
  );
87
+ resolve(result);
88
+ } catch (error) {
89
+ reject(error);
98
90
  }
99
91
  });
100
92
  };
@@ -1,109 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.OkHiAuth = void 0;
7
- var _axios = _interopRequireDefault(require("axios"));
8
- var _OkHiMode = require("./OkHiMode");
9
- var _OkHiException = require("./OkHiException");
10
- var _ = require("./");
11
- var _OkHiNativeModule = require("../OkHiNativeModule");
12
- var _reactNative = require("react-native");
13
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
14
- function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
15
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
16
- function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
17
- /**
18
- * @ignore
19
- */
20
- class OkHiAuth {
21
- constructor() {
22
- _defineProperty(this, "API_VERSION", 'v5');
23
- _defineProperty(this, "ANONYMOUS_SIGN_IN_ENDPOINT", '/auth/anonymous-signin');
24
- _defineProperty(this, "DEV_BASE_URL", `https://dev-api.okhi.io/${this.API_VERSION}` + this.ANONYMOUS_SIGN_IN_ENDPOINT);
25
- _defineProperty(this, "SANDBOX_BASE_URL", `https://sandbox-api.okhi.io/${this.API_VERSION}` + this.ANONYMOUS_SIGN_IN_ENDPOINT);
26
- _defineProperty(this, "PROD_BASE_URL", `https://api.okhi.io/${this.API_VERSION}` + this.ANONYMOUS_SIGN_IN_ENDPOINT);
27
- _defineProperty(this, "config", null);
28
- }
29
- anonymousSignInWithPhoneNumber(phone, scopes, config) {
30
- this.config = config;
31
- return this.anonymousSignIn({
32
- scopes,
33
- phone
34
- });
35
- }
36
- anonymousSignInWithUserId(userId, scopes) {
37
- return this.anonymousSignIn({
38
- scopes,
39
- user_id: userId
40
- });
41
- }
42
- async anonymousSignIn(payload) {
43
- return new Promise(async (resolve, reject) => {
44
- try {
45
- const config = this.config || (await (0, _.getApplicationConfiguration)());
46
- if (config === null || !config.auth || !config.auth.token) {
47
- reject(new _OkHiException.OkHiException({
48
- code: _OkHiException.OkHiException.UNAUTHORIZED_CODE,
49
- message: _OkHiException.OkHiException.UNAUTHORIZED_MESSAGE
50
- }));
51
- } else {
52
- const {
53
- auth,
54
- context
55
- } = config;
56
- let url = this.SANDBOX_BASE_URL;
57
- if ((context === null || context === void 0 ? void 0 : context.mode) === 'dev') {
58
- url = this.DEV_BASE_URL;
59
- } else if ((context === null || context === void 0 ? void 0 : context.mode) === _OkHiMode.OkHiMode.PROD) {
60
- url = this.PROD_BASE_URL;
61
- } else {
62
- url = this.SANDBOX_BASE_URL;
63
- }
64
- const headers = {
65
- Authorization: auth.token
66
- };
67
- const {
68
- data
69
- } = await _axios.default.post(url, payload, {
70
- headers
71
- });
72
- if (_reactNative.Platform.OS === 'android') {
73
- await _OkHiNativeModule.OkHiNativeModule.setItem('okhi:recent:token', data.authorization_token); //TODO: move all anonymousSignIn to native code
74
- }
75
- resolve(data.authorization_token);
76
- }
77
- } catch (error) {
78
- reject(this.parseRequestError(error));
79
- }
80
- });
81
- }
82
- parseRequestError(error) {
83
- if (!error.response) {
84
- return new _OkHiException.OkHiException({
85
- code: _OkHiException.OkHiException.NETWORK_ERROR_CODE,
86
- message: _OkHiException.OkHiException.NETWORK_ERROR_MESSAGE
87
- });
88
- }
89
- switch (error.response.status) {
90
- case 400:
91
- return new _OkHiException.OkHiException({
92
- code: _OkHiException.OkHiException.INVALID_PHONE_CODE,
93
- message: _OkHiException.OkHiException.INVALID_PHONE_MESSAGE
94
- });
95
- case 401:
96
- return new _OkHiException.OkHiException({
97
- code: _OkHiException.OkHiException.UNAUTHORIZED_CODE,
98
- message: _OkHiException.OkHiException.UNAUTHORIZED_MESSAGE
99
- });
100
- default:
101
- return new _OkHiException.OkHiException({
102
- code: _OkHiException.OkHiException.UNKNOWN_ERROR_CODE,
103
- message: error.message || _OkHiException.OkHiException.UNKNOWN_ERROR_MESSAGE
104
- });
105
- }
106
- }
107
- }
108
- exports.OkHiAuth = OkHiAuth;
109
- //# sourceMappingURL=OkHiAuth.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["_axios","_interopRequireDefault","require","_OkHiMode","_OkHiException","_","_OkHiNativeModule","_reactNative","e","__esModule","default","_defineProperty","r","t","_toPropertyKey","Object","defineProperty","value","enumerable","configurable","writable","i","_toPrimitive","Symbol","toPrimitive","call","TypeError","String","Number","OkHiAuth","constructor","API_VERSION","ANONYMOUS_SIGN_IN_ENDPOINT","anonymousSignInWithPhoneNumber","phone","scopes","config","anonymousSignIn","anonymousSignInWithUserId","userId","user_id","payload","Promise","resolve","reject","getApplicationConfiguration","auth","token","OkHiException","code","UNAUTHORIZED_CODE","message","UNAUTHORIZED_MESSAGE","context","url","SANDBOX_BASE_URL","mode","DEV_BASE_URL","OkHiMode","PROD","PROD_BASE_URL","headers","Authorization","data","axios","post","Platform","OS","OkHiNativeModule","setItem","authorization_token","error","parseRequestError","response","NETWORK_ERROR_CODE","NETWORK_ERROR_MESSAGE","status","INVALID_PHONE_CODE","INVALID_PHONE_MESSAGE","UNKNOWN_ERROR_CODE","UNKNOWN_ERROR_MESSAGE","exports"],"sources":["OkHiAuth.ts"],"sourcesContent":["import axios from 'axios';\nimport { OkHiMode } from './OkHiMode';\nimport { OkHiException } from './OkHiException';\nimport type { AuthApplicationConfig, OkHiAccessScope } from './_types';\nimport { getApplicationConfiguration } from './';\nimport { OkHiNativeModule } from '../OkHiNativeModule';\nimport { Platform } from 'react-native';\n\n/**\n * @ignore\n */\nexport class OkHiAuth {\n private readonly API_VERSION = 'v5';\n private readonly ANONYMOUS_SIGN_IN_ENDPOINT = '/auth/anonymous-signin';\n private readonly DEV_BASE_URL =\n `https://dev-api.okhi.io/${this.API_VERSION}` +\n this.ANONYMOUS_SIGN_IN_ENDPOINT;\n private readonly SANDBOX_BASE_URL =\n `https://sandbox-api.okhi.io/${this.API_VERSION}` +\n this.ANONYMOUS_SIGN_IN_ENDPOINT;\n private readonly PROD_BASE_URL =\n `https://api.okhi.io/${this.API_VERSION}` + this.ANONYMOUS_SIGN_IN_ENDPOINT;\n private config: AuthApplicationConfig | null = null;\n\n anonymousSignInWithPhoneNumber(\n phone: string,\n scopes: Array<OkHiAccessScope>,\n config: AuthApplicationConfig\n ) {\n this.config = config;\n return this.anonymousSignIn({\n scopes,\n phone,\n });\n }\n\n protected anonymousSignInWithUserId(\n userId: string,\n scopes: Array<OkHiAccessScope>\n ) {\n return this.anonymousSignIn({\n scopes,\n user_id: userId,\n });\n }\n\n private async anonymousSignIn(payload: {\n scopes: Array<OkHiAccessScope>;\n [key: string]: any;\n }): Promise<string> {\n return new Promise(async (resolve, reject) => {\n try {\n const config = this.config || (await getApplicationConfiguration());\n if (config === null || !config.auth || !config.auth.token) {\n reject(\n new OkHiException({\n code: OkHiException.UNAUTHORIZED_CODE,\n message: OkHiException.UNAUTHORIZED_MESSAGE,\n })\n );\n } else {\n const { auth, context } = config;\n let url = this.SANDBOX_BASE_URL;\n if (context?.mode === ('dev' as any)) {\n url = this.DEV_BASE_URL;\n } else if (context?.mode === OkHiMode.PROD) {\n url = this.PROD_BASE_URL;\n } else {\n url = this.SANDBOX_BASE_URL;\n }\n const headers = { Authorization: auth.token };\n const { data } = await axios.post(url, payload, {\n headers,\n });\n if (Platform.OS === 'android') {\n await OkHiNativeModule.setItem(\n 'okhi:recent:token',\n data.authorization_token\n ); //TODO: move all anonymousSignIn to native code\n }\n resolve(data.authorization_token);\n }\n } catch (error) {\n reject(this.parseRequestError(error));\n }\n });\n }\n\n private parseRequestError(error: any) {\n if (!error.response) {\n return new OkHiException({\n code: OkHiException.NETWORK_ERROR_CODE,\n message: OkHiException.NETWORK_ERROR_MESSAGE,\n });\n }\n switch (error.response.status) {\n case 400:\n return new OkHiException({\n code: OkHiException.INVALID_PHONE_CODE,\n message: OkHiException.INVALID_PHONE_MESSAGE,\n });\n case 401:\n return new OkHiException({\n code: OkHiException.UNAUTHORIZED_CODE,\n message: OkH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