@jetthai/cuora 1.0.180 → 1.0.182

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetthai/cuora",
3
- "version": "1.0.180",
3
+ "version": "1.0.182",
4
4
  "description": "",
5
5
  "main": "./src/index.js",
6
6
  "type": "module",
package/src/api/auth.js CHANGED
@@ -11,6 +11,21 @@ export default {
11
11
  isPublic: true,
12
12
  headers: { 'x-access-captcha': reCaptchaToken },
13
13
  }),
14
+ loginApp: ({ id, provider, displayName, pictureUrl, accessToken }) => ({
15
+ url: 'appLogin',
16
+ method: 'post',
17
+ params: {
18
+ provider,
19
+ data: {
20
+ id,
21
+ provider,
22
+ displayName,
23
+ pictureUrl,
24
+ accessToken,
25
+ },
26
+ },
27
+ isPublic: true,
28
+ }),
14
29
  register: ({ channel = 'official', subchannel = 'pwa', reCaptchaToken, dt, ...rest }) => ({
15
30
  url: `register?channel=${channel}&subchannel=${subchannel}`,
16
31
  method: 'post',
package/src/index.js CHANGED
@@ -2,6 +2,6 @@ import api from './api/index.js'
2
2
  import machine from './machine/index.js'
3
3
  import Request from './request/request.js'
4
4
  import Config from './request/config.js'
5
- import util, { constant } from './util/index.js'
5
+ import util, { constant, storage } from './util/index.js'
6
6
 
7
- export { api, machine, Request, Config, util, constant }
7
+ export { api, machine, Request, Config, util, constant, storage }
@@ -1,8 +1,8 @@
1
1
  import urljoin from 'url-join'
2
- import Cookies from 'universal-cookie'
3
2
  import { assign, createMachine } from 'xstate'
4
3
  import auth from '../api/auth.js'
5
4
  import user from '../api/user.js'
5
+ import storage from '../util/storage.js'
6
6
  import { getDeviceToken } from '../util/deviceToken.js'
7
7
 
8
8
  // const urljoin = require('url-join')
@@ -14,7 +14,6 @@ export default function (request) {
14
14
  const phoneCountryCode = 'phone_countrycode'
15
15
  const accountPhoneKey = 'account_phone'
16
16
  const accountEmailKey = 'account_email'
17
- const cookies = new Cookies()
18
17
 
19
18
  return createMachine(
20
19
  {
@@ -68,6 +67,19 @@ export default function (request) {
68
67
  },
69
68
  },
70
69
  },
70
+ authenticationApp: {
71
+ invoke: {
72
+ src: 'authenticateApp',
73
+ onDone: {
74
+ target: 'authenticated',
75
+ actions: ['setToken'],
76
+ },
77
+ onError: {
78
+ target: 'idle',
79
+ actions: 'removeToken',
80
+ },
81
+ },
82
+ },
71
83
  authenticated: {
72
84
  invoke: {
73
85
  src: 'fetchUserInfo',
@@ -180,6 +192,9 @@ export default function (request) {
180
192
  LOGIN_3PARTY: {
181
193
  actions: 'redirectTo3Party',
182
194
  },
195
+ LOGIN_APP: {
196
+ actions: '.authenticationApp'
197
+ },
183
198
  SET_PLAY: {
184
199
  actions: 'setPlay',
185
200
  },
@@ -234,7 +249,7 @@ export default function (request) {
234
249
  }),
235
250
  setLoginLocal: assign((context, event) => {
236
251
  const { username, password } = event.data
237
- cookies.set(userKey, username, {
252
+ storage.set(userKey, username, {
238
253
  path: '/',
239
254
  maxAge: 60 * 60 * 24,
240
255
  })
@@ -247,13 +262,13 @@ export default function (request) {
247
262
  }
248
263
  }),
249
264
  fetchToken: assign((context, event) => {
250
- const token = cookies.get(tokenKey)
265
+ const token = storage.get(tokenKey)
251
266
  return {
252
267
  token,
253
268
  }
254
269
  }),
255
270
  setToken: assign((context, event) => {
256
- cookies.set(tokenKey, event.data, {
271
+ storage.set(tokenKey, event.data, {
257
272
  path: '/',
258
273
  maxAge: 300,
259
274
  })
@@ -263,34 +278,34 @@ export default function (request) {
263
278
  }),
264
279
  setAccount: assign((context, event) => {
265
280
  if (context.loginData.phone) {
266
- cookies.remove(accountEmailKey)
267
- cookies.set(accountPhoneKey, `${context.loginData.phone}`, {
281
+ storage.remove(accountEmailKey)
282
+ storage.set(accountPhoneKey, `${context.loginData.phone}`, {
268
283
  path: '/',
269
284
  maxAge: 60 * 60 * 24 * 7,
270
285
  })
271
- cookies.set(phoneCountryCode, `${context.countryAbbr}_${context.loginData.countryCode}`, {
286
+ storage.set(phoneCountryCode, `${context.countryAbbr}_${context.loginData.countryCode}`, {
272
287
  path: '/',
273
288
  maxAge: 60 * 60 * 24 * 7,
274
289
  })
275
290
  } else if (context.loginData.email) {
276
- cookies.remove(accountPhoneKey)
277
- cookies.remove(phoneCountryCode)
278
- cookies.set(accountEmailKey, context.loginData.email, {
291
+ storage.remove(accountPhoneKey)
292
+ storage.remove(phoneCountryCode)
293
+ storage.set(accountEmailKey, context.loginData.email, {
279
294
  path: '/',
280
295
  maxAge: 60 * 60 * 24 * 7,
281
296
  })
282
297
  }
283
298
  }),
284
299
  removeToken: assign((context, event) => {
285
- cookies.remove(tokenKey, {
300
+ storage.remove(tokenKey, {
286
301
  path: '/',
287
302
  maxAge: 300,
288
303
  })
289
304
  return { token: null }
290
305
  }),
291
306
  extendToken: assign((context, event) => {
292
- const token = cookies.get(tokenKey)
293
- cookies.set(tokenKey, token, {
307
+ const token = storage.get(tokenKey)
308
+ storage.set(tokenKey, token, {
294
309
  path: '/',
295
310
  maxAge: 300,
296
311
  })
@@ -304,7 +319,7 @@ export default function (request) {
304
319
  const messages = {
305
320
  url: window.location.href,
306
321
  list: [
307
- { channel: cookies.get('channel'), subchannel: cookies.get('subchannel') },
322
+ { channel: storage.get('channel'), subchannel: storage.get('subchannel') },
308
323
  { channel: defaultChannel, subchannel: defaultSubchannel },
309
324
  { channel: channel, subchannel: subchannel },
310
325
  ],
@@ -317,8 +332,8 @@ export default function (request) {
317
332
  new URLSearchParams({
318
333
  provider,
319
334
  redirectURI: `${window.location.protocol}//${window.location.host}`,
320
- channel: cookies.get('channel') || channel || defaultChannel || 'official',
321
- subchannel: cookies.get('subchannel') || subchannel || defaultSubchannel || 'pwa',
335
+ channel: storage.get('channel') || channel || defaultChannel || 'official',
336
+ subchannel: storage.get('subchannel') || subchannel || defaultSubchannel || 'pwa',
322
337
  // dt,
323
338
  platform: 'pwa',
324
339
  }).toString()
@@ -338,6 +353,10 @@ export default function (request) {
338
353
  }),
339
354
  },
340
355
  services: {
356
+ authenticateApp: async (context, event) => {
357
+ const { token } = await request.send(auth.loginApp(event.data))
358
+ return token
359
+ },
341
360
  authenticate: async (context, event) => {
342
361
  const data = { ...context.loginData, reCaptchaToken: context.reCaptchaToken }
343
362
  const { token } = await request.send(auth.login(data))
@@ -1,12 +1,11 @@
1
1
  import UAParser from 'ua-parser-js'
2
- import Cookies from 'universal-cookie'
3
2
  import { assign, createMachine } from 'xstate'
4
3
  import pickBy from 'lodash/pickBy.js'
5
4
  import api from '../api/common.js'
5
+ import storage from '../util/storage.js'
6
6
 
7
7
  // const pickBy = require('lodash/pickBy')
8
8
  // const UAParser = require('ua-parser-js')
9
- const cookies = new Cookies()
10
9
 
11
10
  export default function (request) {
12
11
  const { appSecret, defaultChannel, defaultSubchannel } = request.config
@@ -155,14 +154,14 @@ export default function (request) {
155
154
  return
156
155
  } else {
157
156
  let urlParams = new URLSearchParams(window.location.search)
158
- const channel = urlParams.get('channel') || cookies.get('channel') || defaultChannel || 'official'
159
- const subchannel = urlParams.get('subchannel') || cookies.get('subchannel') || defaultSubchannel || 'pwa'
157
+ const channel = urlParams.get('channel') || storage.get('channel') || defaultChannel || 'official'
158
+ const subchannel = urlParams.get('subchannel') || storage.get('subchannel') || defaultSubchannel || 'pwa'
160
159
 
161
- cookies.set('channel', channel, {
160
+ storage.set('channel', channel, {
162
161
  path: '/',
163
162
  maxAge: 60 * 60 * 24 * 7,
164
163
  })
165
- cookies.set('subchannel', subchannel, {
164
+ storage.set('subchannel', subchannel, {
166
165
  path: '/',
167
166
  maxAge: 60 * 60 * 24 * 7,
168
167
  })
@@ -1,9 +1,8 @@
1
1
  import { assign, createMachine } from 'xstate'
2
- import Cookies from 'universal-cookie'
3
2
  import auth from '../api/auth.js'
4
3
  import { getDeviceToken } from '../util/deviceToken.js'
4
+ import storage from '../util/storage.js'
5
5
 
6
- const cookies = new Cookies()
7
6
 
8
7
  export default function (request) {
9
8
  const { fpToken, sentry, defaultChannel, defaultSubchannel } = request.config
@@ -108,7 +107,7 @@ export default function (request) {
108
107
  const messages = {
109
108
  url: window.location.href,
110
109
  list: [
111
- { channel: cookies.get('channel'), subchannel: cookies.get('subchannel') },
110
+ { channel: storage.get('channel'), subchannel: storage.get('subchannel') },
112
111
  { channel: defaultChannel, subchannel: defaultSubchannel },
113
112
  { channel: channel, subchannel: subchannel },
114
113
  ],
@@ -118,8 +117,8 @@ export default function (request) {
118
117
  return await request.send(
119
118
  auth.register({
120
119
  ...signUpData,
121
- channel: cookies.get('channel') || defaultChannel || channel,
122
- subchannel: cookies.get('subchannel') || defaultSubchannel || subchannel,
120
+ channel: storage.get('channel') || defaultChannel || channel,
121
+ subchannel: storage.get('subchannel') || defaultSubchannel || subchannel,
123
122
  dt,
124
123
  reCaptchaToken: context.reCaptchaToken,
125
124
  })
@@ -1,9 +1,7 @@
1
1
  import { assign, createMachine } from 'xstate'
2
- import Cookies from 'universal-cookie'
3
2
  import payment from '../api/payment.js'
4
3
  import user from '../api/user.js'
5
-
6
- const cookies = new Cookies()
4
+ import storage from '../util/storage.js'
7
5
 
8
6
  export default function (request) {
9
7
  return createMachine(
@@ -71,7 +69,7 @@ export default function (request) {
71
69
  const { bank_account, bank_setting_status, topup, verification_status, uid } = context.userInfo || {}
72
70
  // const hasBankAccount = bank_account || bank_setting_status === 1 // sabai
73
71
  const isComplete = topup === 1 || verification_status === 1
74
- const isSent = cookies.get(`welcome_${uid}`)
72
+ const isSent = storage.get(`welcome_${uid}`)
75
73
  return !(isSent || isComplete)
76
74
  },
77
75
  isFilled: (context, event) => {
@@ -86,7 +84,7 @@ export default function (request) {
86
84
  }
87
85
  }),
88
86
  setInfo: (context, event) => {
89
- cookies.set(`welcome_${context.userInfo.uid}`, 'done', {
87
+ storage.set(`welcome_${context.userInfo.uid}`, 'done', {
90
88
  path: '/',
91
89
  maxAge: 60 * 60 * 24 * 365,
92
90
  })
@@ -1,5 +1,5 @@
1
1
  import pickBy from 'lodash/pickBy.js'
2
- import Cookie from './cookie.js'
2
+ import storage from '../util/storage.js'
3
3
 
4
4
  // const pickBy = require('lodash/pickBy')
5
5
 
@@ -12,7 +12,7 @@ export default class Request {
12
12
  this.axios = axios
13
13
  this.source = axios.CancelToken.source()
14
14
  this.config = config
15
- this.cookie = new Cookie(`cuora_${config.clientId}`)
15
+ this.key = `cuora_${config.clientId}`
16
16
  this.errorHandler = null
17
17
  this.initialize()
18
18
  }
@@ -21,8 +21,8 @@ export default class Request {
21
21
  this.axios.interceptors.request.use((axiosConfig) => {
22
22
  axiosConfig.headers['X-TH1GAMES-VERSION'] = this.config.version
23
23
  axiosConfig.headers['X-TH1GAMES-PLATFORM'] = this.config.platform
24
- if (this.cookie.getToken()) {
25
- axiosConfig.headers.Authorization = this.cookie.getToken()
24
+ if (storage.get(this.key)) {
25
+ axiosConfig.headers.Authorization = storage.get(this.key)
26
26
  }
27
27
  return axiosConfig
28
28
  })
@@ -50,7 +50,7 @@ export default class Request {
50
50
  async send(req, customToken) {
51
51
  try {
52
52
  const cancelToken = customToken || this.source.token
53
- this._checkToken(req, this.cookie.getToken())
53
+ this._checkToken(req, storage.get(this.key))
54
54
  const requestConfig = this._createConfig(req, cancelToken)
55
55
  const { data } = await this.axios.request(requestConfig)
56
56
  return data
package/src/util/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import { getDeviceToken } from './deviceToken.js'
2
- export * as constant from './constant.js'
2
+ import storage from './storage.js'
3
+ import * as constant from './constant.js'
3
4
 
4
5
  export default { getDeviceToken }
6
+ export { constant, storage }
@@ -0,0 +1,37 @@
1
+ // import Cookies from 'universal-cookie'
2
+
3
+ // const cookies = new Cookies()
4
+
5
+ export default {
6
+ get(name, options) {
7
+ //return cookies.get(name, options)
8
+
9
+ const itemStr = localStorage.getItem(name)
10
+
11
+ if (!itemStr) {
12
+ return null
13
+ }
14
+
15
+ const item = JSON.parse(itemStr)
16
+ if (new Date().getTime() > item.expiry) {
17
+ localStorage.removeItem(name)
18
+ return null
19
+ }
20
+
21
+ return item.value
22
+ },
23
+ set(name, value, options) {
24
+ // cookies.set(name, value, options)
25
+
26
+ const item = {
27
+ value,
28
+ expiry: new Date().getTime() + (options.maxAge || 0)*1000
29
+ }
30
+ localStorage.setItem(name, JSON.stringify(item))
31
+ },
32
+ remove(name, options) {
33
+ // cookies.remove(name, options)
34
+
35
+ localStorage.removeItem(name)
36
+ }
37
+ }
@@ -1,52 +0,0 @@
1
- import Cookies from 'universal-cookie'
2
-
3
- export default class Cookie {
4
- constructor(tokenKey) {
5
- this.cookies = new Cookies()
6
- this.tokenKey = tokenKey
7
- }
8
-
9
- setToken(token) {
10
- this.cookies.set(this.tokenKey, token, {
11
- path: '/',
12
- maxAge: 300,
13
- })
14
- }
15
-
16
- prolongToken() {
17
- const token = this.getToken()
18
- if (token) {
19
- this.setToken(token)
20
- }
21
- }
22
-
23
- getToken() {
24
- return this.cookies.get(this.tokenKey)
25
- }
26
-
27
- removeToken() {
28
- this.cookies.remove(this.tokenKey, {
29
- path: '/',
30
- maxAge: 300,
31
- })
32
- }
33
-
34
- setUser(user) {
35
- this.cookies.set('cuora_tempuser', user, {
36
- path: '/',
37
- maxAge: 60 * 60 * 24,
38
- })
39
- }
40
-
41
- getUser() {
42
- return this.cookies.get('cuora_tempuser')
43
- }
44
-
45
- set(name, value, options) {
46
- this.cookies.set(name, value, options)
47
- }
48
-
49
- get(name, options) {
50
- return this.cookies.get(name, options)
51
- }
52
- }