-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
130 lines (91 loc) · 3.46 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var passport = require('passport');
var Auth0Strategy = require('passport-auth0');
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/twity_app');
var routes = require('./routes/index');
var user = require('./routes/user');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
require('./controllers/socket').start(io);
var port = 5454;
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
//res.setHeader('Access-Control-Allow-Origin', 'http://0.0.0.0:4200');
res.setHeader('Access-Control-Allow-Origin', 'http://46.101.211.25:4200');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
// Configure Passport to use Auth0
var strategy = new Auth0Strategy({
domain: "omearthling.auth0.com",
clientID: "kAvxHwFKWTSMceNlaxhi24nKygxlY7z2",
clientSecret: "Ox7C7gRofzTDI5Fk_cSuVpuOp9ExAe3LGHYNGO4oZP1q6T4lGNtu08AWYEuY7e01",
callbackURL: 'http://localhost:5454/callback'
}, function(accessToken, refreshToken, extraParams, profile, done) {
// accessToken is the token to call Auth0 API (not needed in the most cases)
// extraParams.id_token has the JSON Web Token
// profile has all the information from the user
if (profile != null && profile != undefined) {
var User = {};
User.profile = profile;
User.accessToken = accessToken;
User.refreshToken = refreshToken;
User.extraParams = extraParams;
//var model = new UserModel();
//
//model.username = profile._json.screen_name;
//model.access_token = accessToken;
//
//model.save(function (err) {
// console.log("saved model ", model, err);
//});
}
return done(null, profile);
});
passport.use(strategy);
// This can be used to keep a smaller payload
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
app.set('views', __dirname + '/views');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
secret: 'shhhhhhhhh',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use('/static', express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/user', user);
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.header('Access-Control-Allow-Origin', '*');
next();
});
server.listen(port);
console.log("Server listening on port " + port);
module.exports = app;