-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.js
73 lines (62 loc) · 1.7 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const { inputField } = require('./input')
const config = require('./config')
const APP_TYPE_CODE = 'auth.sentCodeTypeApp'
const _sendCode = async (telegram, retry = 0) => {
console.log('Sent code to: ', config.phone_number)
const phone = config.phone_number
let sendCode = await telegram('auth.sendCode', {
phone_number: phone,
sms_type: 0,
api_id: config.api_id,
api_hash: config.api_hash
})
return sendCode
// const { phone_code_hash } = sendCode
// console.log('sendCode type', sendCode.type._)
// if (sendCode.type._ === APP_TYPE_CODE && retry < 2) {
// console.log('Retrying')
// return _sendCode(telegram, rety + 1)
// }
// console.log('APP Code', sendCode)
// return sendCode
}
const logout = async (telegram) => {
const result = await telegram('auth.logOut')
return result
}
const login = async (telegram) => {
// const phone = await inputField('phone')
const { phone_code_hash } = await _sendCode(telegram)
const code = await inputField('code')
const res = await telegram('auth.signIn', {
phone_number: config.phone_number,
phone_code_hash,
phone_code: code
})
const { user } = res
return user
}
const register = async (telegram) => {
const phone = config.phone_number
const { phone_code_hash } = await telegram('auth.sendCode', {
phone_number: phone,
current_number: false,
api_id: config.api_id,
api_hash: config.api_hash
})
const code = await inputField('code')
const res = await telegram('auth.signUp', {
phone_number: phone,
phone_code_hash,
phone_code: code,
first_name: 'First',
last_name: 'Last'
})
console.log('signUp', res)
return res
}
module.exports = {
login,
register,
logout
}