aboutsummaryrefslogtreecommitdiffstats
path: root/authy.js
blob: 336cc2c54f6b28f0e28c0dd0812ed03d59366ad1 (plain) (blame)
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
import { Client } from 'authy-client'
import config from './config'

const authy = new Client({ key: config.authy.key })

export default async user => {
  let { authyId } = user

  const res = await authy.createApprovalRequest({
    authyId: user.authyId,
    details: {
      visible: {
        User: user.name,
      }
    },
    logos: [{
      res: 'default',
      url: 'https://coinos.io/static/img/coinos.png'
    }],
    message: 'Login to CoinOS',
  }, {
    ttl: 120
  });

  let counter = 0

  return new Promise((resolve, reject) => {
    let poll = setInterval(async () => {
      counter++
      const r = await authy.getApprovalRequest({ id: res.approval_request.uuid });
      if (r.approval_request.status === 'approved') {
        clearInterval(poll)
        resolve(true)
      } 

      if (r.approval_request.status === 'denied')  {
        clearInterval(poll)
        resolve(false)
      } 

      if (counter > 60) { clearInterval(poll); reject(false) }
    }, 1000)
  })
}