-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
82 lines (70 loc) · 2.01 KB
/
server.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
const axios = require('axios');
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const moment = require('moment');
const app = express();
const PORT = process.env.PORT || 3000;
const API = `https://api-staging.socialidnow.com/v1/marketing`;
const API_ID = 465;
const API_SECRET = 'f6001a6cfd5f6a23d9546c4b2b5668d334f2320a584f7f7121eb7c85851b1a20';
const headers = {
Accept: 'application/json',
'Content-Type': 'application/json'
};
const getLoginInfo = (token) =>
axios({
url: `${API}/login/info`,
params: {
token,
api_secret: API_SECRET,
fields: 'birthday,gender,name,about_me,display_name,picture_urls,providers,emails,educations,verified_email,current_location'
},
headers
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(session({secret: 'coffeebean', saveUninitialized: true, resave: true, cookie: { secure: false }}));
app.use(express.static('public'));
app.set('view engine', 'ejs');
app.use(function(req, res, next) {
res.locals.user = req.session.user;
res.locals.moment = moment;
next();
});
app.get('/', function (req, res) {
const { user } = req.session;
if(user){ res.redirect('/account'); }else{
res.render('index');
}
});
app.get('/account', function (req, res) {
const { user } = req.session;
if(!user) {
res.redirect('/');
}else{
res.render('account');
}
});
app.post('/account', function (req, res) {
const {token} = req.body;
if(!token){
res.redirect('/');
return;
}
getLoginInfo(token).then(result => {
req.session.user = result.data;
req.session.save();
if(!res.locals.user) res.locals.user = req.session.user;
res.render('account')
})
});
app.get('/logout', function (req, res) {
req.session.destroy();
res.redirect('/')
});
app.listen(PORT, function () {
console.log(`Express server running at port ${PORT}!`);
});