-
Notifications
You must be signed in to change notification settings - Fork 7
/
AuthController.js
67 lines (61 loc) · 1.59 KB
/
AuthController.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
import Auth from '../services/Auth'
import { knex } from '../database'
export default class AuthController {
async oAuthCallback(req, res) {
try {
await Auth.authorizeVehicle(req)
res.redirect(
`http://${req.hostname}${
req.hostname === 'localhost' ? ':3000' : ''
}/dashboard`
)
} catch (err) {
console.log(err)
res.redirect(
`http://${req.hostname}${
req.hostname === 'localhost' ? ':3000' : ''
}/connect?error=${err.message}`
)
}
}
async fleetAuth(req, res) {
try {
const { vin } = req.body
if (!vin) {
return res.status(400).send({ error: 'VIN is required' })
}
await Auth.authorizeFleetVehicle(vin)
res.send({ message: 'Vehicle added' })
} catch (err) {
console.log(err)
res.status(500).json({
error:
(err.response &&
err.response.data &&
err.response.data.error_description) ||
err.message,
})
}
}
async getFleetVehicles(req, res) {
try {
const appConfig = await knex('app_config').first()
const vehicles = await knex('vehicles').select()
const authorizedVehicles = await Auth.getFleetVehicles(appConfig)
res.send(
authorizedVehicles.filter(
(authorizedVehicle) =>
!vehicles.some(
(connectedVehicle) =>
connectedVehicle.vin === authorizedVehicle.vin
)
)
)
} catch (err) {
console.log(err)
res.status(500).json({
error: err.message,
})
}
}
}