-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
155 lines (129 loc) · 4.08 KB
/
server.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
const express = require('express');
const connectDB = require('./config/db');
const gravatar = require('gravatar');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const config = require('config');
const authMiddleWare = require('./middleware/auth');
const { check, validationResult} = require('express-validator/check');
const User = require('./routes/models/User');
const app = express();
// Connect DataBase
connectDB();
// Init parses
app.use(express.json( {extended: false} ));
app.get('/api/auth', authMiddleWare, async (req, res) => {
try {
const user = await User.findById(req.user.id).select('-password');
res.json(user);
} catch (error) {
console.error(error.message);
res.status(500).json([{ msg: 'User not found'}]);
}
});
app.get('/', authMiddleWare, (req, res) => res.send('Auth Test API'));
app.get('/api/profiles', authMiddleWare, (req, res) => res.send('Profile Test API'));
app.get('/api/posts', authMiddleWare, (req, res) => res.send('Posts Test API'));
app.get('/api/users', authMiddleWare, (req, res) => res.send('Users Test API'));
//Users API's
app.post('/user/register', [
check('name', 'Name is required').not().isEmpty(),
check('email', 'Include valid e-mail').isEmail(),
check('password', 'Please set a password with 6 or more characters').isLength({
min:6
})
],async (req, res) => {
const errors = validationResult(req);
if(!errors.isEmpty()){
return res.status(400).json({errors : errors.array()});
}
console.log(req.body);
const {name, email, password} = req.body;
try {
let user = await User.findOne({ email });
if(user){
return res.status(400).json({ errors : [{ msg : 'User already exists'}]});
}
// Get user gravatar
const avatar = gravatar.url(email, {
s:'200',
r:'pg',
d:'mm'
});
user = new User({
name,
email,
avatar,
password
});
// Encrypt password
const salt = await bcrypt.genSalt(10);
user.password = await bcrypt.hash(password, salt);
await user.save();
// Return jsonwebtoken
const payLoad = {
user : {
id: user.id
}
};
jwt.sign(
payLoad,
config.get('jwtSecret'),
{ expiresIn: 360000},
(err, token) => {
if(err) throw err;
res.json({token});
}
);
} catch (error) {
console.error(error.message);
return res.status(500).send('Server error.');
}
});
//Users API's
app.post('/auth/authenticate', [
check('email', 'Include valid e-mail').isEmail(),
check('password', 'Password required').exists()
],async (req, res) => {
const errors = validationResult(req);
if(!errors.isEmpty()){
return res.status(400).json({errors : errors.array()});
}
console.log(req.body);
const {email, password} = req.body;
try {
let user = await User.findOne({ email });
if(!user){
return res.status(400).json({ errors : [{ msg : 'Invalid credentials.'}]});
}
const isMatch = await bcrypt.compare(password, user.password);
if(!isMatch){
return res.status(400).json({ errors : [{ msg : 'Invalid credentials.'}]});
}
// Return jsonwebtoken
const payLoad = {
user : {
id: user.id
}
};
jwt.sign(
payLoad,
config.get('jwtSecret'),
{ expiresIn: 360000},
(err, token) => {
if(err) throw err;
res.json({token});
}
);
} catch (error) {
console.error(error.message);
return res.status(500).send('Server error.');
}
});
//Define routes
var users = require('./routes/api/users');
app.use('api/test', users);
// Users
app
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log('App started on port :'+PORT));