-
-
Notifications
You must be signed in to change notification settings - Fork 154
/
refresh-token.ts
executable file
·46 lines (39 loc) · 1.41 KB
/
refresh-token.ts
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
/* eslint-disable no-console */
import { RingRestClient } from './rest-client'
import { requestInput } from './util'
import { AuthTokenResponse } from './ring-types'
export async function acquireRefreshToken() {
const email = await requestInput('Email: '),
password = await requestInput('Password: '),
restClient = new RingRestClient({ email, password }),
getAuthWith2fa = async (): Promise<any> => {
const code = await requestInput('2fa Code: ')
try {
return await restClient.getAuth(code)
} catch (_) {
console.log('Incorrect 2fa code. Please try again.')
return getAuthWith2fa()
}
},
auth: AuthTokenResponse = await restClient.getCurrentAuth().catch((e) => {
if (restClient.promptFor2fa) {
console.log(restClient.promptFor2fa)
return getAuthWith2fa()
}
console.error(e)
process.exit(1)
})
return auth.refresh_token
}
export async function logRefreshToken() {
console.log(
'This CLI will provide you with a refresh token which you can use to configure ring-client-api and homebridge-ring.'
)
const refreshToken = await acquireRefreshToken()
console.log(
'\nSuccessfully logged in to Ring. Please add the following to your config:\n'
)
console.log(`"refreshToken": "${refreshToken}"`)
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
process.on('unhandledRejection', () => {})