-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
40 lines (34 loc) · 871 Bytes
/
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
const axios = require('axios')
const API_URL = 'https://tti.tiwiconnect.com/api'
const identity = {}
/**
* Log into Ryobi GDO account using username / password. After successful login,
* thr API can be used and commands can be sent to GDO
* @param {String} username
* @param {String} password
* @returns apiKey on success
*/
const login = async (username, password) => {
try {
const loginResponse = await axios.post(
`${API_URL}/login`,
{
username,
password,
},
{ withCredentials: true },
)
identity.apiKey = loginResponse.data.result.auth.apiKey
identity.username = username
identity.password = password
return identity
} catch (err) {
console.error('Failed login', err)
return undefined
}
}
module.exports = {
login,
identity,
isAuthenticated: () => !!identity.apiKey,
}