-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
236 lines (166 loc) · 5.36 KB
/
app.js
File metadata and controls
236 lines (166 loc) · 5.36 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
require('dotenv').config();
const express = require('express');
const path = require('path');
const router = require('./Routes/Routes');
const dashboardRouter = require('./Routes/dashboardRouter');
const mongoose = require('mongoose');
const session = require('express-session');
const passport = require('passport');
const bodyParser = require('body-parser');
const MongoStore = require('connect-mongo');
const User = require('./models/user');
const flash = require('connect-flash');
// server setup
const app = express();
//database connnectivitys
mongoose.connect(process.env.DB_URL);
// set static content of the project
app.use(express.static(path.join(__dirname, 'public')));
// body-parser middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.set('view engine', 'ejs'); // set view engine
// flash massages setup
app.use(flash());
//session setup
app.use(session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: false,
cookie: { maxAge: 1 * 60 * 60 * 1000 },
store: MongoStore.create({
mongoUrl: process.env.DB_URL,
collection: "sessions"
})
}));
// authentication setup
app.use(passport.initialize());
app.use(passport.session());
app.use(express.urlencoded({ extended: false }))
const port = 3000
app.use('/dashboard', dashboardRouter);
app.use('/exam',router);
app.get('/', (req, res) => {
res.render('index')
});
app.get('/Login', (req, res) => {
// res.sendFile(path.join(__dirname,'public/pages/login.html'));
res.render('login', { unothorized: req.flash('unothorized'), invalidCredentials: req.flash('invalidCredentials'), logout:req.flash('logout') });
});
app.get('/Signup', (req, res) => {
res.render('create-account');
})
app.get('/create-account', (req, res) => {
res.render('create-account',{usernameError: req.flash('usernameError') , emailError: req.flash('emailError'), serverError: req.flash('serverError')});
})
app.get('/forgot-password', (req, res) => {
res.sendFile(path.join(__dirname, 'public/pages/forgot-password.html'));
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
});
passport.serializeUser((user, cb) => {
cb(null, user);
})
passport.deserializeUser(function (obj, cb) {
cb(null, obj);
});
var userProfile;
// Google login
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
const GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID;
const GOOGLE_CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET;
passport.use(new GoogleStrategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/google/callback"
},
// function to save or create new user from google
(accessToken, refreshToken, profile, cb)=> {
User.findOne({ googleId: profile.id }, function (err, user) {
if(user) {return cb(err, user);}
else{
var user =new User({
username:profile.displayName,
password:"abcdef",
email:profile._json.email,
googleId:profile.id,
});
user.save();
cb(null,user);
}
});
}
));
// Local-statergy login using username pssword
var LocalStrategy = require('passport-local');
const { Router } = require('express');
passport.use(new LocalStrategy({
usernameField: "email",
passwordField: 'password'
},
function (email, password, done) {
// console.log('email = ' + email);
// console.log('password' + password);
User.findOne({ email: email, password: password }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
// if (!user.verifyPassword(password)) { return done(null, false); }
return done(null, user);
});
}
));
app.get('/auth/google',
passport.authenticate('google', { scope: ['profile', 'email'] }));
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login-fail-google' }),
function (req, res) {
// Successful authentication, redirect success.
// console.log(req.user);
res.redirect('/Dashboard');
});
app.post('/login',
passport.authenticate('local', { failureRedirect: '/login-fail'}),
(req, res) => {
console.log(req.user);
res.redirect('/dashboard');
});
app.get('/login-fail', (req, res) => {
req.flash('invalidCredentials', "Wrong Email or Password. Try again !");
res.redirect('/login');
});
app.get('/login-fail-google', (req, res) => {
req.flash('invalidCredentials', "User Not registered. Try again !");
res.redirect('/login');
})
app.post('/create-account', async (req, res) => {
try {
let user = await User.exists({username : req.body.username});
if (user){
req.flash("usernameError","Username already registered");
res.redirect('/create-account');
}
user = await User.exists({email : req.body.email});
if (user){
req.flash("emailError","Email already registered");
res.redirect('/create-account');
}
await User.create({
username: req.body.username,
email: req.body.email,
password: req.body.password
});
res.redirect('/login');
}
catch (error) {
console.log(error);
req.flash("serverError","Some Error occupied");
res.redirect('/create-account');
}
});
app.get('/logout', (req, res) => {
req.logOut();
req.session.destroy();
// req.flash('logout',"Logout Successful!");
res.redirect('/login');
})