@@ -3,27 +3,23 @@ import { prompt } from 'enquirer'
3
3
import { logger } from '@poppinss/cliui'
4
4
import { errorAndExit } from '../../utils/log'
5
5
import { config , isSameUser } from '../../utils/config'
6
+ import type { JikeClient } from 'jike-sdk/node'
6
7
import type { ConfigUser } from '../../utils/config'
7
8
8
9
export const login = async ( ) => {
9
- interface Answers {
10
+ const apiConfig = await prompt < {
10
11
endpointId : string
11
12
endpointUrl : string
12
13
bundleId : string
13
14
appVersion : string
14
15
buildNo : string
15
16
userAgent : string
16
- areaCode : string
17
- mobile : string
18
- password : string
19
- alias : string
20
- }
21
- const answers = await prompt < Answers > ( [
17
+ } > ( [
22
18
{
23
19
type : 'input' ,
24
20
name : 'endpointId' ,
25
21
message : 'What is the endpoint id?' ,
26
- initial : process . env . API_ENDPOINT_ID ,
22
+ initial : process . env . API_ENDPOINT_ID || 'jike' ,
27
23
} ,
28
24
{
29
25
type : 'input' ,
@@ -55,69 +51,59 @@ export const login = async () => {
55
51
message : 'What is the user agent?' ,
56
52
initial : process . env . API_USER_AGENT ,
57
53
} ,
58
- {
59
- type : 'input' ,
60
- name : 'areaCode' ,
61
- message : 'What is your mobile phone area code?' ,
62
- initial : '86' ,
63
- validate : ( value : string ) => / ^ \d + $ / . test ( value ) ,
64
- } ,
65
- {
66
- type : 'input' ,
67
- name : 'mobile' ,
68
- message : 'What is your mobile phone?' ,
69
- validate : ( value : string ) => / ^ \d + $ / . test ( value ) ,
70
- initial : process . env . API_MOBILE ,
71
- } ,
72
- {
73
- type : 'password' ,
74
- name : 'password' ,
75
- message : 'What is your password?' ,
76
- initial : process . env . API_PASSWORD ,
77
- } ,
78
- {
79
- type : 'input' ,
80
- name : 'alias' ,
81
- message : 'Do you want to set an alias for this user? (not required)' ,
82
- required : false ,
83
- } ,
84
54
] )
85
55
56
+ const { JikeClient } = await import ( 'jike-sdk/node' )
57
+
86
58
const deviceId = randomUUID ( )
87
59
const idfv = randomUUID ( )
88
60
89
- const { JikeClient } = await import ( 'jike-sdk/node' )
90
61
const client = new JikeClient ( {
91
- endpointId : answers . endpointId ,
92
- endpointUrl : answers . endpointUrl ,
93
- bundleId : answers . bundleId ,
94
- appVersion : answers . appVersion ,
95
- buildNo : answers . buildNo ,
96
- userAgent : answers . userAgent ,
62
+ ...apiConfig ,
97
63
deviceId,
98
64
idfv,
99
65
} )
100
66
101
- await client
102
- . loginWithPassword ( answers . areaCode , answers . mobile , answers . password )
103
- . catch ( ( err ) => errorAndExit ( new Error ( err ) ) )
67
+ type LoginMethod = 'mobile-password' | 'mobile-sms'
68
+ const { loginMethod } = await prompt < { loginMethod : LoginMethod } > ( {
69
+ name : 'loginMethod' ,
70
+ message : 'What is your preferred login method?' ,
71
+ type : 'select' ,
72
+ choices : [
73
+ { name : 'mobile-password' , message : 'Mobile phone and password' } ,
74
+ { name : 'mobile-sms' , message : 'Mobile phone and SMS code' } ,
75
+ ] ,
76
+ } )
77
+
78
+ switch ( loginMethod ) {
79
+ case 'mobile-password' :
80
+ await loginWithPassword ( client )
81
+ break
82
+ case 'mobile-sms' :
83
+ await loginWithSms ( client )
84
+ break
85
+ default :
86
+ errorAndExit ( new Error ( 'Unknown login method' ) )
87
+ }
88
+
89
+ const { alias } = await prompt < { alias : string } > ( {
90
+ type : 'input' ,
91
+ name : 'alias' ,
92
+ message : 'Do you want to set an alias for this user? (not required)' ,
93
+ required : false ,
94
+ } )
104
95
105
96
// Save Auth
106
97
const profile = await client . getSelf ( ) . queryProfile ( )
107
98
const user : ConfigUser = {
108
- endpointId : answers . endpointId ,
109
- endpointUrl : answers . endpointUrl ,
110
- bundleId : answers . bundleId ,
111
- appVersion : answers . appVersion ,
112
- buildNo : answers . buildNo ,
113
- userAgent : answers . userAgent ,
99
+ ...apiConfig ,
114
100
accessToken : client . accessToken ,
115
101
refreshToken : client . refreshToken ,
116
102
deviceId,
117
103
idfv,
118
104
userId : profile . user . id ,
119
105
screenName : profile . user . screenName ,
120
- alias : answers . alias . trim ( ) ,
106
+ alias : alias . trim ( ) ,
121
107
}
122
108
const index = config . value . users . findIndex ( ( _auth ) => isSameUser ( user , _auth ) )
123
109
if ( index > - 1 ) {
@@ -128,3 +114,67 @@ export const login = async () => {
128
114
129
115
logger . success ( 'Login success!' )
130
116
}
117
+
118
+ const questions = {
119
+ areaCode : {
120
+ type : 'input' ,
121
+ name : 'areaCode' ,
122
+ message : 'What is your mobile phone area code?' ,
123
+ initial : '86' ,
124
+ validate : ( value : string ) => / ^ \d + $ / . test ( value ) ,
125
+ } ,
126
+ mobile : {
127
+ type : 'input' ,
128
+ name : 'mobile' ,
129
+ message : 'What is your mobile phone?' ,
130
+ validate : ( value : string ) => / ^ \d + $ / . test ( value ) ,
131
+ initial : process . env . API_MOBILE ,
132
+ } ,
133
+ }
134
+
135
+ const loginWithPassword = async ( client : JikeClient ) => {
136
+ interface Auth {
137
+ areaCode : string
138
+ mobile : string
139
+ password : string
140
+ }
141
+
142
+ const { areaCode, mobile, password } = await prompt < Auth > ( [
143
+ questions . areaCode ,
144
+ questions . mobile ,
145
+ {
146
+ type : 'password' ,
147
+ name : 'password' ,
148
+ message : 'What is your password?' ,
149
+ initial : process . env . API_PASSWORD ,
150
+ } ,
151
+ ] )
152
+
153
+ await client
154
+ . loginWithPassword ( areaCode , mobile , password )
155
+ . catch ( ( err ) => errorAndExit ( new Error ( err ) ) )
156
+ }
157
+
158
+ const loginWithSms = async ( client : JikeClient ) => {
159
+ const { areaCode, mobile } = await prompt < {
160
+ areaCode : string
161
+ mobile : string
162
+ } > ( [ questions . areaCode , questions . mobile ] )
163
+
164
+ await client
165
+ . sendSmsCode ( areaCode , mobile )
166
+ . catch ( ( err ) => errorAndExit ( new Error ( err ) ) )
167
+
168
+ logger . success ( 'SMS code sent!' )
169
+
170
+ const { smsCode } = await prompt < { smsCode : string } > ( {
171
+ type : 'input' ,
172
+ name : 'smsCode' ,
173
+ message : 'What is the SMS code?' ,
174
+ validate : ( value : string ) => / ^ \d { 6 } $ / . test ( value ) ,
175
+ } )
176
+
177
+ await client
178
+ . loginWithSmsCode ( areaCode , mobile , smsCode )
179
+ . catch ( ( err ) => errorAndExit ( new Error ( err ) ) )
180
+ }
0 commit comments