stub new user sign up
This commit is contained in:
parent
f26b8ece56
commit
e7c2bf38cf
4 changed files with 51 additions and 36 deletions
|
@ -1,12 +1,18 @@
|
|||
const knex = require('../data/db')
|
||||
const signToken = require('../services/signToken');
|
||||
|
||||
const signUp = async (req, res, next) => {
|
||||
|
||||
const user = req.body;
|
||||
try {
|
||||
signToken(res, user);
|
||||
knex('user')
|
||||
.insert(user)
|
||||
.then(newUser => {
|
||||
signToken(res, newUser[0]);
|
||||
res.send('ok').status(200);
|
||||
}
|
||||
)
|
||||
}
|
||||
catch (err) {
|
||||
res.status(500).json(err)
|
||||
}
|
||||
|
|
24
packages/play-node-go/server/middleware/verifyJWT.js
Normal file
24
packages/play-node-go/server/middleware/verifyJWT.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
const dotenv = require('dotenv');
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const verifyToken = async (req, res, next) => {
|
||||
const token = req.cookies.token || '';
|
||||
const secret = process.env.NODE_ENV === 'test' ? process.env.TEST_SECRET : process.env.JWT_SECRET;
|
||||
try {
|
||||
if (!token) {
|
||||
return res.status(401).json('You need to Login')
|
||||
}
|
||||
const decrypt = await jwt.verify(token, secret);
|
||||
req.user = {
|
||||
id: decrypt.id,
|
||||
username: username,
|
||||
};
|
||||
next();
|
||||
} catch (err) {
|
||||
return res.status(500).json(err.toString());
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = verifyToken;
|
|
@ -1,4 +1,4 @@
|
|||
const authSpec = (chai, server) => {
|
||||
const authSpec = (chai, knex, server) => {
|
||||
const newUserFormData = {
|
||||
'username':'newUser',
|
||||
'password':'password',
|
||||
|
@ -28,5 +28,20 @@ const authSpec = (chai, server) => {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('post to sign up should add user to db', done => {
|
||||
chai.request(server)
|
||||
.post('/auth/signup')
|
||||
.type('form')
|
||||
.send(newUserFormData)
|
||||
.end((err, res) => {
|
||||
if (err) done(err);
|
||||
knex('user').where({'username': newUserFormData.username}).then(results => {
|
||||
const newUser = results[0];
|
||||
if (newUser.username === newUserFormData.username) done();
|
||||
})
|
||||
});
|
||||
|
||||
})
|
||||
}
|
||||
module.exports = authSpec;
|
|
@ -8,7 +8,7 @@ const server = require('../server');
|
|||
|
||||
const should = chai.should();
|
||||
|
||||
// const authSpec = require('./auth.spec');
|
||||
const authSpec = require('./auth.spec');
|
||||
|
||||
chai.use(chaiHttp);
|
||||
// ! to run tests from other testing modules
|
||||
|
@ -25,37 +25,7 @@ const setupDb = () => {
|
|||
describe('Auth Routes', function() {
|
||||
setupDb();
|
||||
|
||||
// authSpec(chai, server)
|
||||
|
||||
const newUserFormData = {
|
||||
'username':'newUser',
|
||||
'password':'password',
|
||||
'email':'user@example.com'
|
||||
}
|
||||
|
||||
it('post to sign up should return 200 status', function(done) {
|
||||
chai.request(server)
|
||||
.post('/auth/signup')
|
||||
.type('form')
|
||||
.send(newUserFormData)
|
||||
.end((err, res) => {
|
||||
if (err) done(err);
|
||||
res.should.status(200);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('post to sign up should return token', done => {
|
||||
chai.request(server)
|
||||
.post('/auth/signup')
|
||||
.type('form')
|
||||
.send(newUserFormData)
|
||||
.end((err, res) => {
|
||||
if (err) done(err);
|
||||
res.should.cookie('token');
|
||||
done();
|
||||
});
|
||||
});
|
||||
authSpec(chai, knex, server)
|
||||
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue