-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
89 lines (73 loc) · 2.35 KB
/
Copy pathserver.js
File metadata and controls
89 lines (73 loc) · 2.35 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Load Dependencies
var express = require('express');
var exphbs = require('express-handlebars');
var bodyParser = require('body-parser');
// Load configuration from .env file
require('dotenv').config();
// Load and initialize MesageBird SDK
var messagebird = require('messagebird')(process.env.MESSAGEBIRD_API_KEY);
// Set up and configure the Express framework
var app = express();
// Create `ExpressHandlebars` instance with a default layout.
var hbs = exphbs.create({
defaultLayout: 'main',
// Uses multiple partials dirs, templates in "shared/templates/" are shared
// with the client-side of the app (see below).
// partialsDir: [
// 'shared/templates/',
// 'views/partials/'
// ]
});
// app.engine('handlebars', exphbs.create({defaultLayout: 'main'}));
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.use(bodyParser.urlencoded({ extended : true }));
// Display page to ask the user for their phone number
app.get('/', function(req, res) {
res.render('step1');
});
app.post("/step2",function(req,res){
var number = req.body.number;
messagebird.verify.create(number , {
originator : 'Code',
template : 'Your verification code is %token.'
},function(err,response){
if (err) {
// Request has failed
console.log(err);
res.render('step1', {
error : err.errors[0].description
});
} else {
// Request was successful
console.log(response);
res.render('step2', {
id : response.id
});
}
})
});
// Verify whether the token is correct
app.post('/step3', function(req, res) {
var id = req.body.id;
var token = req.body.token;
// Make request to Verify API
messagebird.verify.verify(id, token, function(err, response) {
if (err) {
// Verification has failed
console.log(err);
res.render('step2', {
error: err.errors[0].description,
id : id
});
} else {
// Verification was successful
console.log(response);
res.render('step3');
}
})
});
const port = process.env.PORT || 4000;
app.listen(port,()=>{
console.log(`server is listening on port : ${port}`);
})