Skip to content

Commit

Permalink
Adds Get /users/me and authentication middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
DeadlockDruid committed Sep 25, 2018
1 parent 981ed09 commit 1b8cb10
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
18 changes: 18 additions & 0 deletions server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ UserSchema.methods.generateAuthToken = function() {
})
};

UserSchema.statics.findByToken = function (token) {
var User = this;

var decoded;

try {
decoded = jwt.verify(token, 'abc123');
} catch(e) {
return Promise.reject();
}

return User.findOne({
'_id': decoded._id,
'tokens.token': token,
'tokens.access': 'auth'
});
};

var User = mongoose.model('User', UserSchema);

module.exports = { User };
7 changes: 6 additions & 1 deletion server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const { ObjectID } = require('mongodb');
const { mongoose } = require('./db/mongoose');
const { Todo } = require('./models/todo');
const { User } = require('./models/user');
const { authenticate } = require('./middleware/authenticate');

const app = express();
const port = process.env.PORT || 3000;
const port = process.env.PORT || 3000;

app.use(bodyParser.json());

Expand Down Expand Up @@ -98,6 +99,10 @@ app.post('/users', (req, res) => {
}).catch(e => res.status(400).send(e));
});

app.get('/users/me', authenticate, (req, res) => {
res.send(req.header(user));
});

app.listen(port, () => {
console.log(`Started on port: ${port}`);
});
Expand Down

0 comments on commit 1b8cb10

Please sign in to comment.