Skip to content

Commit

Permalink
Implement auth
Browse files Browse the repository at this point in the history
  • Loading branch information
arghmatey committed Apr 13, 2020
1 parent 4960200 commit e2e9935
Show file tree
Hide file tree
Showing 16 changed files with 726 additions and 30 deletions.
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node server.js
19 changes: 19 additions & 0 deletions config/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const jwt = require('jsonwebtoken');
const SECRET = process.env.SECRET;

module.exports = function (req, res, next) {
let token = req.get('Authorization') || req.query.token || req.body.token;
if (token) {
token = token.replace('Bearer ', '');
jwt.verify(token, SECRET, function (err, decoded) {
if (err) {
next(err);
} else {
req.user = decoded.user;
next();
}
});
} else {
next();
}
};
2 changes: 1 addition & 1 deletion config/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ mongoose.connect(process.env.DATABASE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
})
});
36 changes: 33 additions & 3 deletions controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
const User = require('../models/user');
const jwt = require('jsonwebtoken');
const SECRET = process.env.SECRET;

module.exports - {
signup
module.exports = {
signup,
login
};

async function signup(req, res) {
const user = new User(req.body);
try {
console.log('controllers/users/13');
await user.save();
res.join(user);
const token = createJWT(user);
res.json({ token });
} catch (err) {
res.status(400).json(err);
};
}

async function login(req, res) {
try {
const user = await User.findOne({ email: req.body.email });
if (!user) return res.status(401).json({ err: 'bad credentials' });
user.comparePassword(req.body.pw, (err, isMatch) => {
if (isMatch) {
const token = createJWT(user);
res.json({ token });
} else {
return res.status(401).json({ err: 'bad credentials' });
}
});
} catch (err) {
return res.status(401).json(err);
}
}

function createJWT(user) {
return jwt.sign(
{ user },
SECRET,
{ expiresIn: '24h' }
);
}
24 changes: 24 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');

const SALT_ROUNDS = 6;

const userSchema = new mongoose.Schema({
name: String,
Expand All @@ -13,4 +16,25 @@ const userSchema = new mongoose.Schema({
timestamps: true
});

userSchema.set('toJSON', {
transform: function (doc, ret) {
delete ret.password;
return ret;
}
});

userSchema.pre('save', function (next) {
const user = this;
if (!user.isModified('password')) return next();
bcrypt.hash(user.password, SALT_ROUNDS, function (err, hash) {
if (err) return next(err);
user.password = hash;
next();
});
});

userSchema.methods.comparePassword = function (tryPassword, cb) {
bcrypt.compare(tryPassword, this.password, cb);
};

module.exports = mongoose.model('User', userSchema);
Loading

0 comments on commit e2e9935

Please sign in to comment.