-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
180 lines (144 loc) · 4.32 KB
/
Copy pathserver.js
File metadata and controls
180 lines (144 loc) · 4.32 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
if (process.env.NODE_ENV !== 'production') require('dotenv').config()
const express = require('express')
const mongoose = require('mongoose')
const bcrypt = require("bcrypt")
const bodyParser = require("body-parser")
const passport = require('passport')
const flash = require('express-flash')
const session = require('express-session')
const findOrCreate = require("mongoose-findorcreate")
//DB config:
mongoose.connect(process.env.DB_URL)
// s1TO1n7bDOE9a58S
const test_userSchema = new mongoose.Schema({
username: {type: String, unique: true},
name: String,
googleId: String,
password: String
})
test_userSchema.plugin(findOrCreate)
const Test_user = mongoose.model('test_user', test_userSchema)
// const user = new Test_user({
// username: "boubkeraouabe@gmail.com",
// password: "bobaob123"
// })
// user.save();
// const users = []
const initializePassport = require('./passport-config')
initializePassport(
passport,
email => Test_user.findOne({username: email}),
id => Test_user.findById(id),
Test_user)
// const initializePassport = require('./passport-config')
// initializePassport(
// passport,
// email => users.find(user => user.email === email),
// id => users.find(user => user.id === id)
// )
const app = express()
app.set('view engine', 'ejs')
app.use(express.static("public"))
app.use(bodyParser.urlencoded({extended: true}))
app.use(flash())
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false, //if nothing has changed don't resave the session variables
saveUninitialized: false //don't save empty values
}))
app.use(passport.initialize())
app.use(passport.session())
// app.get('/', (req, res)=>{
// res.render('index.ejs', {name: "Boubker AOUABE"});
// })
// app.get('/register', (req, res)=>{
// res.render('register.ejs')
// })
// app.get('/login', (req, res)=>{
// res.render('login.ejs')
// })
// app.post('/login', passport.authenticate('local', {
// successRedirect: '/',
// failureRedirect: '/login',
// failureFlash: true
// }))
// app.post('/register', async (req, res)=>{
// try {
// const hashedPassword = await bcrypt.hash(req.body.password, 10)
// const user = new Test_user({
// username: req.body.email,
// password: hashedPassword
// })
// user.save().then(res.redirect('/login'));
// } catch (error) {
// res.redirect('/register')
// }
// })
let user;
app.get('/', checkAuthenticated, (req, res) => {
res.render('index.ejs', { name: user.name })
})
app.get('/login', checkNotAuthenticated, (req, res) => {
res.render('login.ejs')
})
app.post('/login', checkNotAuthenticated, passport.authenticate('local', {
failureRedirect: '/login',
failureFlash: true
}),
(req, res)=>{
user = req.user
res.redirect('/')
}
)
app.get('/register', checkNotAuthenticated, (req, res) => {
res.render('register')
})
app.post('/register', checkNotAuthenticated, async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10)
const user = new Test_user({
username: req.body.email,
name: req.body.name,
password: hashedPassword
})
user.save().then(res.redirect('/login'));
} catch {
res.redirect('/register')
}
})
app.post('/logout', (req, res) => {
req.logOut(err=> {
if(err) return next(err)
})
res.redirect('/login')
})
app.get('/auth/google',
passport.authenticate('google', { scope: ["profile", "email"] })
)
app.get('/auth/google/check',
passport.authenticate('google', { scope: ["profile", "email"] })
)
app.get('/auth/google/secrets1',
passport.authenticate('google', { failureRedirect: '/login' }),
(req, res) => {
// Successful Google authentication - handle the redirection here
user = req.user
console.log(`user: ${user}`)
res.redirect('/');
}
)
function checkAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next()
}
res.redirect('/login')
}
function checkNotAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return res.redirect('/')
}
next()
}
app.listen(3000, ()=>{
console.log('server listenig on port 3000')
})