Auth routes
parent
e889d37333
commit
78604bc8bd
|
@ -3,6 +3,8 @@ const auth = require('express').Router();
|
|||
const register = require('./register');
|
||||
const login = require('./login');
|
||||
|
||||
const UserSchema = require('../../schemas/user');
|
||||
|
||||
/**
|
||||
* @api {post} /auth/register Register
|
||||
* @apiName Register
|
||||
|
@ -15,17 +17,17 @@ const login = require('./login');
|
|||
*
|
||||
* @apiSuccess {Array} Array Array of Note objects.
|
||||
*/
|
||||
auth.post('/register', register);
|
||||
auth.post('/register', UserSchema, register);
|
||||
|
||||
/**
|
||||
* @api {post} /auth/login Request JWT token
|
||||
* @api {post} /auth/login Get access token
|
||||
* @apiName Login
|
||||
* @apiGroup Auth
|
||||
*
|
||||
* @apiParam {String} username username of the user.
|
||||
* @apiParam {String} password password of the user.
|
||||
*
|
||||
* @apiSuccess {string} jwt_token JWT token.
|
||||
* @apiSuccess {string} access_token Access token.
|
||||
*/
|
||||
auth.post('/login', login);
|
||||
|
||||
|
|
|
@ -1,5 +1,29 @@
|
|||
module.exports = (req, res) => {
|
||||
const auth = [];
|
||||
const mongoose = require('mongoose');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const bcrypt = require('bcrypt-nodejs');
|
||||
|
||||
res.status(200).json({ auth });
|
||||
module.exports = (req, res, next) => {
|
||||
const User = mongoose.model('User');
|
||||
|
||||
User.findOne({
|
||||
username: req.body.username,
|
||||
}, (err, user) => {
|
||||
if (err) throw err;
|
||||
|
||||
if (!user) {
|
||||
next({ status: 400, message: 'Authentication failed. User not found.' });
|
||||
} else {
|
||||
// check if password matches
|
||||
const isMatch = bcrypt.compare(req.body.password, User.password);
|
||||
if (isMatch && !err) {
|
||||
// if user is found and password is right create a token
|
||||
const token = 'test';
|
||||
// return the information including token as JSON
|
||||
res.json({ success: true, access_token: `JWT ${token}` });
|
||||
} else {
|
||||
next({ status: 401, message: 'Authentication failed. User not found.' });
|
||||
res.status(401).send({ success: false, msg: 'Authentication failed. Wrong password.' });
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
|
@ -1,5 +1,29 @@
|
|||
module.exports = (req, res) => {
|
||||
const auth = [];
|
||||
const mongoose = require('mongoose');
|
||||
const bcrypt = require('bcrypt-nodejs');
|
||||
|
||||
res.status(200).json({ auth });
|
||||
module.exports = (req, res, next) => {
|
||||
const UserModel = mongoose.model('User');
|
||||
|
||||
const passwordHash = bcrypt.hashSync(req.body.password);
|
||||
|
||||
const User = new UserModel({
|
||||
firstname: req.body.firstname,
|
||||
lastname: req.body.lastname,
|
||||
username: req.body.username,
|
||||
password: passwordHash,
|
||||
});
|
||||
|
||||
UserModel.findOne({
|
||||
username: req.body.username,
|
||||
}, (err, user) => {
|
||||
next({ status: 401, message: 'Username is already taken.' });
|
||||
});
|
||||
|
||||
return User.save((saveErr) => {
|
||||
if (saveErr) {
|
||||
return next({ status: 500, message: 'Database error', error: [] });
|
||||
}
|
||||
|
||||
return res.status(200).json(User);
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue