Skip to content

Commit 318a2cf

Browse files
committed
feat: support sms code login
1 parent 1e5a0bd commit 318a2cf

File tree

1 file changed

+101
-51
lines changed

1 file changed

+101
-51
lines changed

src/modules/user/login.ts

Lines changed: 101 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,23 @@ import { prompt } from 'enquirer'
33
import { logger } from '@poppinss/cliui'
44
import { errorAndExit } from '../../utils/log'
55
import { config, isSameUser } from '../../utils/config'
6+
import type { JikeClient } from 'jike-sdk/node'
67
import type { ConfigUser } from '../../utils/config'
78

89
export const login = async () => {
9-
interface Answers {
10+
const apiConfig = await prompt<{
1011
endpointId: string
1112
endpointUrl: string
1213
bundleId: string
1314
appVersion: string
1415
buildNo: string
1516
userAgent: string
16-
areaCode: string
17-
mobile: string
18-
password: string
19-
alias: string
20-
}
21-
const answers = await prompt<Answers>([
17+
}>([
2218
{
2319
type: 'input',
2420
name: 'endpointId',
2521
message: 'What is the endpoint id?',
26-
initial: process.env.API_ENDPOINT_ID,
22+
initial: process.env.API_ENDPOINT_ID || 'jike',
2723
},
2824
{
2925
type: 'input',
@@ -55,69 +51,59 @@ export const login = async () => {
5551
message: 'What is the user agent?',
5652
initial: process.env.API_USER_AGENT,
5753
},
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-
},
8454
])
8555

56+
const { JikeClient } = await import('jike-sdk/node')
57+
8658
const deviceId = randomUUID()
8759
const idfv = randomUUID()
8860

89-
const { JikeClient } = await import('jike-sdk/node')
9061
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,
9763
deviceId,
9864
idfv,
9965
})
10066

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+
})
10495

10596
// Save Auth
10697
const profile = await client.getSelf().queryProfile()
10798
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,
114100
accessToken: client.accessToken,
115101
refreshToken: client.refreshToken,
116102
deviceId,
117103
idfv,
118104
userId: profile.user.id,
119105
screenName: profile.user.screenName,
120-
alias: answers.alias.trim(),
106+
alias: alias.trim(),
121107
}
122108
const index = config.value.users.findIndex((_auth) => isSameUser(user, _auth))
123109
if (index > -1) {
@@ -128,3 +114,67 @@ export const login = async () => {
128114

129115
logger.success('Login success!')
130116
}
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

Comments
 (0)