-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
149 lines (129 loc) · 3.86 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//EXPRESS, PASSPORT (FLASH, SESSION), BCRYPT, METHOD-OVERRIDE, EJS, DOTENV, FORMIDABLE, FS
require('dotenv').config()
const { application } = require('express')
const express = require('express')
const passport = require('passport')
const bcrypt = require('bcrypt')
const path = require('path')
const initializePassport = require('./passport-config')
const fileupload = require("express-fileupload");
const bodyParser = require('body-parser')
const flash = require('express-flash')
const session = require('express-session')
const methodOverride = require('method-override')
const fs = require('fs');
const app = express()
initializePassport(
passport,
email => users.find(user => user.email === email),
id => users.find(user => user.id === id)
)
let users = []
users.push({
id: Date.now().toString(),
name: 'Klaudijus Miskinis',
email: 'asd@asd',
password: bcrypt.hashSync('asd', 10),
profileImage: '../assets/avatar.png'
})
app.set('set engine', 'ejs')
app.use(express.static(__dirname + '/views/assets'))
app.use(express.urlencoded({ extended: false }))
app.use(express.static('views'));
app.use(express.json())
app.use(fileupload());
app.use(flash())
app.use(session({
secret: process.env.KEY_SESSION,
resave: false,
saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(methodOverride('_method'))
app.get('/', checkAuthenticated, (req, res) => {
let user = {
id: req.user.id,
name: req.user.name,
email: req.user.email,
profileImage: req.user.profileImage
}
res.render('ejs/user.ejs', {user: user})
})
app.get('/login', checkNotAuthenticated, (req, res) => {
res.render('ejs/login.ejs')
})
app.get('/register', checkNotAuthenticated, (req, res) => {
res.render('ejs/register.ejs')
})
app.post('/register', checkNotAuthenticated, async (req, res) => {
try {
let user = {
id: Date.now().toString(),
name: req.body.name,
email: req.body.email,
password: req.body.password,
profileImage: '../assets/avatar.png'
}
user.password = await bcrypt.hash(user.password, 10)
users.push(user)
res.redirect('/login')
} catch {
res.redirect('/register')
}
})
app.post('/updateProfile', checkAuthenticated, async (req, res) => {
user = users.find(user => user.id === req.user.id)
if(req.body.name) {
console.log(req.body.name)
user.name = req.body.name
}
if(req.body.email) {
console.log(req.body.email)
user.email = req.body.email
}
if(req.body.password) {
console.log(req.body.password)
user.password = await bcrypt.hash(req.body.password, 10)
}
users.splice(user)
req.user = user
users.push(user)
res.redirect('/')
})
app.post('/upload', checkAuthenticated, async (req, res) => {
if (req.files.filetoupload.mimetype.substr(0,6) == 'image/') {
req.files.filetoupload.tempFilePath = __dirname + '/views/assets/' + req.files.filetoupload.name
req.user.profileImage = '../assets/'+req.files.filetoupload.name
try {
await req.files.filetoupload.mv(__dirname + '/views/assets/' + req.files.filetoupload.name)
} catch(e){
console.log(e)
}
}
res.redirect('/')
});
app.post('/login', passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login',
failureFlash: true
}))
app.delete('/logout', (req, res) => {
req.logOut()
res.redirect('/login')
})
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('Servidor', 'ON')
})