-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
188 lines (159 loc) · 5.13 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const jsonParser = bodyParser.json();
const urlParser = bodyParser.urlencoded({extended: true});
app.use(urlParser);
app.use(jsonParser);
app.use(express.static('.'));
var secret_key = 'n93874nc32jknksjav34o8743fjasdkvjoidfshjk';
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var mongodbUri = 'mongodb://mylipdb:[email protected]:60649/mylipdb';
mongoose.connect(mongodbUri);
var conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection error:'));
conn.once('open', function() {
});
var colors = ['red','orange','pink','purple','dark','nude'];
var lipstick = {type: String, enum: colors};
var Schema = mongoose.Schema, ObjectId = Schema.ObjectId;
var ProfileInfo = new Schema ({
username:String,
password:String,
email: String,
lipsOwned:[lipstick]
});
var ProfModel = mongoose.model('ProfileModel', ProfileInfo);
module.exports = ProfModel;
app.use(session({
store: new MongoStore({
url: mongodbUri
}),
secret: secret_key,
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(function(username,password,done) {
ProfModel.findOne({username:username},function(err,user) {
if (err) {
console.log(err);
return done(err);
}
if (!user) {
console.log(password);
return done(null,false);
}
if (password != user.password) {
console.log(password);
console.log(user.password);
return done(null,false);
}
return done(null,user);
});
}));
passport.serializeUser(function(user, cb) {
console.log(user);
cb(null, user.id);
});
passport.deserializeUser(function(id, cb) {
ProfModel.findOne({username:id}, function (err, user) {
if (err) { return cb(err); }
cb(null, user);
});
});
app.get('/',function(req,res) {
res.sendFile("html/index.html",{"root":__dirname});
});
app.get('/logout',function(req,res) {
//delete req.session.user_id;
req.logout();
res.redirect('/');//("html/index.html",{"root":__dirname});
});
app.get('/login',function(req,res) {
res.sendFile("html/login.html",{"root":__dirname});
});
//app.get('/user_profile', checkAuth, function(req,res) {
app.get('/signup',function(req,res) {
res.sendFile("html/signup.html",{"root":__dirname});
});
app.get('/home',/*require('connect-ensure-login').ensureLoggedIn(),/*passport.authenticate('local',{failureRedirect:'/login'}),*/function(req,res) {
res.sendFile("html/home.html",{"root":__dirname});
/*if ( req.session && req.session.user) {
ProfModel.findOne({username:req.session.user.username},function(err,user) {
if (!user) {
req.session.reset();
res.redirect('/login');
} else {
res.locals.user = user;
res.sendFile("html/home.html",{"root":__dirname});
}
});
} else {
res.redirect('/login');
}*/
});
app.get('/home-nudes',function(req,res) {
res.sendFile("html/home-nudes.html",{"root":__dirname});
});
app.get('/home-pinks',function(req,res) {
res.sendFile("html/home-pinks.html",{"root":__dirname});
});
app.get('/home-mirror',function(req,res) {
res.sendFile("html/home-mirror.html",{"root":__dirname});
});
app.get('/home-camera',function(req,res) {
res.sendFile("html/home-camera.html",{"root":__dirname});
});
app.post('/login',passport.authenticate('local',{failureRedirect:'/login'}),function(req,res) {
/*var post = req.body;
console.log(post.username);
console.log(post.password);
var check_user = ProfModel.findOne({username:post.username}, function(err,user) {
if (err) {console.log('Login error');
} else if (!user) {
res.send('Incorrect username or password');
} else {
if (post.password == user.password) {
req.session.user = user;
res.redirect('/home');
} else {
res.send('Incorrect username or password');
}
}
});*/
console.log('was authenticated');
res.redirect('/home');
});
app.post('/signup',jsonParser,function(req,res) {
var data = req.body;
var newUser = new ProfModel({
username:data.username,
password:data.password,
email:data.email
});
//ProfModel.insert(newUser);
//console.log(res);
newUser.save(function (err) { if(err) console.log('Save error');});
//res.end(JSON.stringify(res));
/*myModel.create({username: data.username, password: data.password},function(err,instance) {
if(err) return console.log(err);
});*/
//res.redirect('http://127.0.0.1:8081'+req.url);
console.log(newUser.username);
console.log(newUser.password);
console.log(newUser.email);
res.redirect('/login');
//res.sendFile("html/home.html",{"root":__dirname});
});
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
});