@jetthai/cuora 1.0.43 → 1.0.44

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/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.0.44](https://github.com/jetthai/cuora-module/compare/v1.0.43...v1.0.44) (2021-11-22)
6
+
5
7
  ### [1.0.43](https://github.com/jetthai/cuora-module/compare/v1.0.42...v1.0.43) (2021-11-22)
6
8
 
7
9
  ### [1.0.42](https://github.com/jetthai/cuora-module/compare/v1.0.41...v1.0.42) (2021-11-16)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetthai/cuora",
3
- "version": "1.0.43",
3
+ "version": "1.0.44",
4
4
  "description": "",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
package/src/api/auth.js CHANGED
@@ -5,10 +5,10 @@ export default {
5
5
  params: { username, password, provider, countryCode, phone, email },
6
6
  isPublic: true,
7
7
  }),
8
- register: (data) => ({
9
- url: 'register?channel=official&subchannel=pwa',
8
+ register: ({ channel = 'official', subchannel = 'pwa', ...rest }) => ({
9
+ url: `register?channel=${channel}&subchannel=${subchannel}`,
10
10
  method: 'post',
11
- params: data,
11
+ params: rest,
12
12
  isPublic: true,
13
13
  }),
14
14
  logout: () => ({ url: 'logout', method: 'post' }),
@@ -19,6 +19,14 @@ export default function (request) {
19
19
  token: null,
20
20
  tick: 2000,
21
21
  isPlaying: false,
22
+ loginData: {
23
+ provider: '',
24
+ countryCode: null,
25
+ phone: null,
26
+ email: null,
27
+ username: null,
28
+ password: null,
29
+ },
22
30
  userInfo: {},
23
31
  redirectURL: null,
24
32
  },
@@ -28,13 +36,16 @@ export default function (request) {
28
36
  always: [{ target: 'login', cond: 'hasToken' }],
29
37
  on: {
30
38
  LOGIN_PHONE: {
31
- target: 'authentication.phone',
39
+ target: 'authentication',
40
+ actions: 'setLoginPhone',
32
41
  },
33
42
  LOGIN_EMAIL: {
34
- target: 'authentication.email',
43
+ target: 'authentication',
44
+ actions: 'setLoginEmail',
35
45
  },
36
46
  LOGIN_LOCAL: {
37
- target: 'authentication.local',
47
+ target: 'authentication',
48
+ actions: 'setLoginLocal',
38
49
  },
39
50
  LOGIN_3PARTY: {
40
51
  actions: 'redirectTo3Party',
@@ -45,45 +56,15 @@ export default function (request) {
45
56
  },
46
57
  },
47
58
  authentication: {
48
- states: {
49
- phone: {
50
- invoke: {
51
- src: 'authenticateByPhone',
52
- onDone: {
53
- target: '#auth.login',
54
- actions: 'setToken',
55
- },
56
- onError: {
57
- target: '#auth.idle',
58
- actions: 'removeToken',
59
- },
60
- },
61
- },
62
- local: {
63
- invoke: {
64
- src: 'authenticateByUsername',
65
- onDone: {
66
- target: '#auth.login',
67
- actions: 'setToken',
68
- },
69
- onError: {
70
- target: '#auth.idle',
71
- actions: 'removeToken',
72
- },
73
- },
59
+ invoke: {
60
+ src: 'authenticate',
61
+ onDone: {
62
+ target: 'login',
63
+ actions: 'setToken',
74
64
  },
75
- email: {
76
- invoke: {
77
- src: 'authenticateByEmail',
78
- onDone: {
79
- target: '#auth.login',
80
- actions: 'setToken',
81
- },
82
- onError: {
83
- target: '#auth.idle',
84
- actions: 'removeToken',
85
- },
86
- },
65
+ onError: {
66
+ target: 'idle',
67
+ actions: 'removeToken',
87
68
  },
88
69
  },
89
70
  },
@@ -163,6 +144,41 @@ export default function (request) {
163
144
  },
164
145
  },
165
146
  actions: {
147
+ setLoginPhone: assign((context, event) => {
148
+ const { countryCode, phone, password } = event.data
149
+ return {
150
+ loginData: {
151
+ provider: 'phone',
152
+ countryCode,
153
+ phone,
154
+ password,
155
+ },
156
+ }
157
+ }),
158
+ setLoginEmail: assign((context, event) => {
159
+ const { email, password } = event.data
160
+ return {
161
+ loginData: {
162
+ provider: 'email',
163
+ email,
164
+ password,
165
+ },
166
+ }
167
+ }),
168
+ setLoginLocal: assign((context, event) => {
169
+ const { username, password } = event.data
170
+ cookies.set(userKey, username, {
171
+ path: '/',
172
+ maxAge: 60 * 60 * 24,
173
+ })
174
+ return {
175
+ loginData: {
176
+ provider: 'local',
177
+ username,
178
+ password,
179
+ },
180
+ }
181
+ }),
166
182
  fetchToken: assign((context, event) => {
167
183
  const token = cookies.get(tokenKey)
168
184
  return {
@@ -213,23 +229,8 @@ export default function (request) {
213
229
  }),
214
230
  },
215
231
  services: {
216
- authenticateByUsername: async (context, event) => {
217
- const { username, password } = event.data
218
- cookies.set(userKey, username, {
219
- path: '/',
220
- maxAge: 60 * 60 * 24,
221
- })
222
- const { token } = await request.send(auth.login({ provider: 'local', username, password }))
223
- return token
224
- },
225
- authenticateByPhone: async (context, event) => {
226
- const { countryCode, phone, password } = event.data
227
- const { token } = await request.send(auth.login({ provider: 'phone', countryCode, phone, password }))
228
- return token
229
- },
230
- authenticateByEmail: async (context, event) => {
231
- const { email, password } = event.data
232
- const { token } = await request.send(auth.login({ provider: 'email', email, password }))
232
+ authenticate: async (context, event) => {
233
+ const { token } = await request.send(auth.login(context.loginData))
233
234
  return token
234
235
  },
235
236
  logout: async (context, event) => {
@@ -16,6 +16,7 @@ import setting from './setting'
16
16
  import side from './side'
17
17
  import withdraw from './withdraw'
18
18
  import user_info from './user_info'
19
+ import sign_up from './sign_up'
19
20
 
20
21
  export default {
21
22
  ...activity,
@@ -36,4 +37,5 @@ export default {
36
37
  side,
37
38
  withdraw,
38
39
  user_info,
40
+ sign_up,
39
41
  }
@@ -1,10 +1,11 @@
1
1
  import { createMachine, assign } from 'xstate'
2
+ import auth from '../api/auth'
2
3
 
3
4
  export default function (request) {
4
5
  return createMachine(
5
6
  {
6
7
  id: 'sign_up',
7
- initial: '',
8
+ initial: 'idle',
8
9
  context: {
9
10
  email: '',
10
11
  password: '',
@@ -12,27 +13,41 @@ export default function (request) {
12
13
  states: {
13
14
  idle: {
14
15
  on: {
15
- submit: {
16
+ SIGN_UP: {
16
17
  target: 'validating',
17
- actions: 'setAccount',
18
+ actions: 'setForm',
18
19
  },
19
20
  },
20
21
  },
21
22
  validating: {
22
- always: [],
23
+ always: [
24
+ {
25
+ target: 'invalidFormat',
26
+ cond: 'isInvalidFormat',
27
+ },
28
+ {
29
+ target: 'submitting',
30
+ },
31
+ ],
32
+ },
33
+ invalidFormat: {},
34
+ submitting: {
35
+ invoke: {
36
+ src: 'signUp',
37
+ onDone: {
38
+ target: 'success',
39
+ },
40
+ onError: {
41
+ target: 'idle',
42
+ },
43
+ },
23
44
  },
24
- submittin: {},
25
45
  success: {},
26
46
  },
27
47
  },
28
48
  {
29
- guards: {
30
- valid: (context, event) => {
31
- return /^[^+-]*@/.test(context.password)
32
- },
33
- },
34
49
  actions: {
35
- setAccount: assign((context, event) => {
50
+ setForm: assign((context, event) => {
36
51
  const { email, password } = event.data
37
52
  return {
38
53
  email,
@@ -40,6 +55,17 @@ export default function (request) {
40
55
  }
41
56
  }),
42
57
  },
58
+ guards: {
59
+ isInvalidFormat: (context, event) => {
60
+ return /[+-]/.test(context.email)
61
+ },
62
+ },
63
+ services: {
64
+ signUp: async (context, event) => {
65
+ const { email, password } = context
66
+ return await request.send(auth.register({ provider: 'email', email, password }))
67
+ },
68
+ },
43
69
  }
44
70
  )
45
71
  }