-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
105 lines (76 loc) · 2.44 KB
/
app.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const logger = require('morgan');
const testUsers = {}
const app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.get('/ping', (req, res, next) => {
res.send('pong')
});
app.post('/user/verify', (req, res, next) => {
const token = req.body.token
console.log("POST /user/verify. token: ", token);
const userData = verifyUser(token)
console.log("userData", userData);
if (!userData) {
return res.status(422).json({error: "User token is invalid"})
}
return res.status(200).json(userData)
});
app.get('/user/verify', (req, res, next) => {
const token = req.query.token
console.log("GET /user/verify. token: ", token);
const userData = verifyUser(token)
console.log("userData", userData);
if (!userData) {
return res.status(422).json({error: "User token is invalid"})
}
return res.status(200).json(userData)
});
function verifyUser(token) {
// TODO:
// here basically we need to verify a provided data (e.g. token)) against DB
// and if the provided token is valid -> return a data with specified format back to ConnectCube server,
// as described here https://developers.connectycube.com/guides/custom-identity-provider
const isValid = isTokenValid(token);
if (!isValid) {
return null
}
let userData = testUsers[token]
if (!userData) {
const userID = Math.floor(Math.random() * 1000000);
const uLogin = "login_" + userID;
const uEmail = userID + "@test.com";
userData = {
uid: userID,
login: uLogin,
email: uEmail,
user: {id: userID, login: uLogin, email: uEmail},
users: [{uid: userID, login: uLogin, email: uEmail}]
};
testUsers[token] = userData
}
return userData;
}
function isTokenValid(token) {
// TODO:
// here basically we need to verify a provided data (e.g. token)) against DB
// For a test purpose we will be returning true all the time
return true
}
// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
});
// error handler
app.use((err, req, res, next) => {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.sendStatus(err.status || 500);
});
module.exports = app;