Skip to content

Commit a820117

Browse files
committed
added authentication with jwt at backend
1 parent c7307fc commit a820117

File tree

608 files changed

+86829
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

608 files changed

+86829
-3
lines changed

.env

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
DB_CONNECTION_URL=mongodb+srv://wawel37:[email protected]/ChatApp?retryWrites=true&w=majority
2-
PORT=4200
2+
PORT=4200
3+
JWT_SECRET = haslo123321

app.js

+2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ db.on('error', console.error.bind(console, 'MongoDB connection error: '));
2929
//Routes
3030
const postsRoute = require('./routes/posts');
3131
const indexRoute = require('./routes/index');
32+
const authRoute = require('./routes/auth');
3233

3334
app.use('/', indexRoute);
3435
app.use('/posts', postsRoute);
36+
app.use('/auth', authRoute);
3537

3638
//Setting up the socket connection
3739
socketConnection.socketConnection(io);

middleWares/jwtValidation.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const jwt = require('jsonwebtoken');
2+
3+
function auth(req, res, next){
4+
const token = req.header('jwt');
5+
if(!token) return res.status(401).json({error: 'Access denied!'});
6+
7+
try{
8+
jwt.verify(token, process.env.JWT_SECRET);
9+
}catch(err){
10+
return res.status(400).json({error: 'Invalid Token'});
11+
}
12+
next();
13+
}

models/user.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,15 @@ var Schema = mongoose.Schema;
33

44

55
var UserSchema = mongoose.Schema({
6-
name: {type: String, required: true}
7-
})
6+
name: {type: String,
7+
required: true,
8+
min: 6},
9+
email: {type: String,
10+
required: true,
11+
min:6},
12+
password: {type: String,
13+
min:6,
14+
required: true}
15+
});
16+
17+
module.exports = mongoose.model('User', UserSchema);

node_modules/.bin/detect-libc

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/mkdirp

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/needle

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/node-pre-gyp

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/rimraf

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@hapi/address/LICENSE.md

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@hapi/address/README.md

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@hapi/address/lib/decode.js

+120
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@hapi/address/lib/domain.js

+113
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)