-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.js
68 lines (61 loc) · 1.94 KB
/
routes.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
'use strict'
var express = require('express')
var router = express.Router()
const axios = require('axios')
const validateSubscribeInput = require('./utilities/subscribe')
const User = require('./models/user')
router.post('/idcheck', handleIdentityCheck)
function handleIdentityCheck (req, res) {
axios
.post(`https://lab.selified.com/api/request/facematch-id`,
{
type: 'sms',
mobile: req.body.phone
},
{
headers: {
Authorization: 'Bearer 1hfeZ9IsV345H6SdrksAle',
'Content-Type': 'application/json'
}
}
).then(result => {
console.log('success: ', result.data)
res.json(result.data)
}).catch(err => {
console.log('SELIFIED ERROR', err)
res.status(400).json({message: 'ERROR: ' + err.message, err})
})
}
router.get('/test', handleApiTest)
function handleApiTest (req, res) {
// res.status(400).json({message: 'Missing Data', err: {status_code: 400, code: 'BAD_REQUEST'}})
res.json({success: true})
}
router.post('/users/subscribe', handleNewUser)
async function handleNewUser (req, res) {
var data = req.body
res.contentType('application/json')
var errorMessage = validateSubscribeInput(data)
if (errorMessage) {
return res.status(400).json({message: errorMessage, err: {status_code: 400, code: 'BAD_REQUEST'}})
}
try {
const user = await User.findOne({ address: data.address })
if (user) {
res.status(400).json({message: 'Ethereum Address already exists.', err: {status_code: 400, code: 'ACCOUNT_EXISTS'}})
} else {
console.log('here comes the phone number', data.phone)
const newUser = new User({
name: data.name,
email: data.email,
phone: data.phone,
address: data.address
})
const result = await newUser.save()
res.json(result)
}
} catch (e) {
res.status(400).json({message: 'Something went wrong. ' + e.message, err: e})
}
}
module.exports = router