Permalink
Browse files

fix(): auhentication required when adding new user

  • Loading branch information...
Izak88 committed Oct 4, 2017
1 parent ac6f5f0 commit abbd17b33f34f869db2af5639d636973107aaa11
@@ -165,9 +165,19 @@ export function userRoutes(): express.Router {
});

router.post('/create', (req: express.Request, res: express.Response) => {
createUser(req.body)
.then(() => res.status(200).json({ status: true }))
.catch(err => res.status(200).json({ status: false }));
getUsers().then(users => {
if (users && users.length) {
checkApiRequestAuth(req).then(() => {
createUser(req.body)
.then(() => res.status(200).json({ status: true }))
.catch(err => res.status(200).json({ status: false }));
}).catch(err => res.status(401).json({ data: 'Not Authorized' }));
} else {
createUser(req.body)
.then(() => res.status(200).json({ status: true }))
.catch(err => res.status(200).json({ status: false }));
}
}).catch(err => res.status(200).json({ data: false }));
});

router.post('/save', (req: express.Request, res: express.Response) => {
@@ -7,7 +7,8 @@ export default function() {
let options = {
url: 'http://localhost:6500/api/user/create',
method: 'POST',
json: { test: 1 }
json: { email: 'test@gmail.com', fullname: 'test',
password: 'test', confirmPassword: 'test', admin: 1 }
};

request(options, (err, response, body) => {
@@ -0,0 +1,30 @@
import * as request from 'request';

export default function() {
return Promise.resolve()
.then(() => {
return new Promise((resolve, reject) => {
let options = {
url: 'http://localhost:6500/api/user/create',
method: 'POST',
json: { email: 'test2@gmail.com', fullname: 'test2',
password: 'test', confirmPassword: 'test', admin: 1 }
};

request(options, (err, response, body) => {
if (err) {
Promise.reject(err);
} else {
if (response.statusCode === 401 && response.body.data === 'Not Authorized') {
resolve(body);
} else {
reject({
statusCode: response.statusCode,
response: body
});
}
}
});
});
});
}

0 comments on commit abbd17b

Please sign in to comment.