diff --git a/app/src/api/api.ts b/app/src/api/api.ts index 44b4ad07..8b1a5f50 100644 --- a/app/src/api/api.ts +++ b/app/src/api/api.ts @@ -1,78 +1,20 @@ import express from 'express'; import bodyParser from 'body-parser'; -import { authUser, createUser, getUserById, getNewUserAPIKey, deleteUser } from './services/user'; +import { authUser, getNewUserAPIKey, logout } from './services/user'; import { queryLocation } from './services/search'; import buildingsRouter from './routes/buildingsRouter'; +import usersRouter from './routes/usersRouter'; -const server = express.Router() +const server = express.Router(); // parse POSTed json body server.use(bodyParser.json()); server.use('/buildings', buildingsRouter); - -// POST new user -server.post('/users', function (req, res) { - const user = req.body; - if (req.session.user_id) { - res.send({ error: 'Already signed in' }); - return - } - - if (user.email) { - if (user.email != user.confirm_email) { - res.send({ error: 'Email did not match confirmation.' }); - return - } - } else { - user.email = null; - } - - createUser(user).then(function (result) { - if (result.user_id) { - req.session.user_id = result.user_id; - res.send({ user_id: result.user_id }); - } else { - req.session.user_id = undefined; - res.send({ error: result.error }); - } - }).catch(function (err) { - console.error(err); - res.send(err) - }); -}); - -// GET own user info -server.route('/users/me') - .get(function (req, res) { - if (!req.session.user_id) { - res.send({ error: 'Must be logged in' }); - return - } - - getUserById(req.session.user_id).then(function (user) { - res.send(user); - }).catch(function (error) { - res.send(error); - }); - }) - .delete((req, res) => { - if (!req.session.user_id) { - return res.send({ error: 'Must be logged in' }); - } - console.log(`Deleting user ${req.session.user_id}`); - - deleteUser(req.session.user_id).then( - () => logout(req.session) - ).then(() => { - res.send({ success: true }); - }).catch(err => { - res.send({ error: err }); - }); - }) +server.use('/users', usersRouter); // POST user auth server.post('/login', function (req, res) { @@ -98,15 +40,6 @@ server.post('/logout', function (req, res) { }); }); -function logout(session) { - return new Promise((resolve, reject) => { - session.user_id = undefined; - session.destroy(err => { - if (err) return reject(err); - return resolve(); - }); - }); -} // POST generate API key server.post('/api/key', function (req, res) { diff --git a/app/src/api/controllers/userController.ts b/app/src/api/controllers/userController.ts new file mode 100644 index 00000000..56183fbd --- /dev/null +++ b/app/src/api/controllers/userController.ts @@ -0,0 +1,65 @@ +import * as userService from '../services/user'; + +function createUser(req, res) { + const user = req.body; + if (req.session.user_id) { + res.send({ error: 'Already signed in' }); + return; + } + + if (user.email) { + if (user.email != user.confirm_email) { + res.send({ error: 'Email did not match confirmation.' }); + return; + } + } else { + user.email = null; + } + + userService.createUser(user).then(function (result) { + if (result.user_id) { + req.session.user_id = result.user_id; + res.send({ user_id: result.user_id }); + } else { + req.session.user_id = undefined; + res.send({ error: result.error }); + } + }).catch(function (err) { + console.error(err); + res.send(err); + }); +} + +function getCurrentUser(req, res) { + if (!req.session.user_id) { + res.send({ error: 'Must be logged in' }); + return; + } + + userService.getUserById(req.session.user_id).then(function (user) { + res.send(user); + }).catch(function (error) { + res.send(error); + }); +} + +function deleteCurrentUser(req, res) { + if (!req.session.user_id) { + return res.send({ error: 'Must be logged in' }); + } + console.log(`Deleting user ${req.session.user_id}`); + + userService.deleteUser(req.session.user_id).then( + () => userService.logout(req.session) + ).then(() => { + res.send({ success: true }); + }).catch(err => { + res.send({ error: err }); + }); +} + +export default { + createUser, + getCurrentUser, + deleteCurrentUser, +}; \ No newline at end of file diff --git a/app/src/api/routes/usersRouter.ts b/app/src/api/routes/usersRouter.ts new file mode 100644 index 00000000..8d4e44f2 --- /dev/null +++ b/app/src/api/routes/usersRouter.ts @@ -0,0 +1,14 @@ +import express from 'express'; + +import userController from '../controllers/userController'; + + +const router = express.Router(); + +router.post('/', userController.createUser); + +router.route('/me') + .get(userController.getCurrentUser) + .delete(userController.deleteCurrentUser); + +export default router; \ No newline at end of file diff --git a/app/src/api/services/user.ts b/app/src/api/services/user.ts index ec02b856..3297720d 100644 --- a/app/src/api/services/user.ts +++ b/app/src/api/services/user.ts @@ -140,4 +140,22 @@ function deleteUser(id) { }); } -export { getUserById, createUser, authUser, getNewUserAPIKey, authAPIUser, deleteUser } +function logout(session: Express.Session) { + return new Promise((resolve, reject) => { + session.user_id = undefined; + session.destroy(err => { + if (err) return reject(err); + return resolve(); + }); + }); +} + +export { + getUserById, + createUser, + authUser, + getNewUserAPIKey, + authAPIUser, + deleteUser, + logout +};