Skip to content

Commit

Permalink
update authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
popey456963 committed Jul 21, 2020
1 parent 51e3b1e commit ec8b268
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
4 changes: 4 additions & 0 deletions errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
"ERR_USER_FIND": {
"msg": "Unable to find any users with the username '{username}'.'",
"code": 400
},
"ERR_USER_FIND_BY_ID": {
"msg": "Unable to find any users with the username '{id}'.'",
"code": 400
}
}
15 changes: 14 additions & 1 deletion handlers/user.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const userService = require('../services/user')
const Errors = require('@femto-apps/errors')('../errors.json')

async function getUsers(req, res) {
const users = await userService.readUsers()
Expand All @@ -19,7 +20,19 @@ async function getUser(req, res) {
return res.json(user)
}

async function getUserById(req, res) {
const id = req.params.id
const user = await userService.readUserById(id)

if (!user) {
return res.status(404).json({ error: Errors('ERR_USER_FIND_BY_ID', { id }) })
}

return res.json(user)
}

module.exports = {
getUsers,
getUser
getUser,
getUserById
}
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ const userHandler = require('./handlers/user')
app.put('/api/consumer/:consumerId', consumerHandler.putConsumer)
app.delete('/api/consumer/:consumerId', consumerHandler.deleteConsumer)

app.get('/api/user', userHandler.getUser)
app.get('/api/user/:username', userHandler.getUsers)
app.get('/api/user', userHandler.getUsers)
app.get('/api/user/:username', userHandler.getUser)
app.get('/api/user/id/:id', userHandler.getUserById)

app.get('/api/auth', login.isAuthenticated, foreign.getAuth)
app.get('/api/verify', (req, res) => res.sendStatus(410))
Expand Down
13 changes: 13 additions & 0 deletions services/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@ async function readUser(username) {
return user
}

async function readUserById(id) {
const user = await User.findOne({ _id: id })
.catch(err => {
throw Errors('ERROR_USER_FIND_BY_ID', { idd, err })
})

return user
}

module.exports = {
readUser,
readUserById
}
3 changes: 3 additions & 0 deletions views/login.pug
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ block main

h3 Register

blockquote.
Whilst we allow any username to be used, if you use an email you can recover your account at a later point in time! You can do this #[a(href='/recover') here].

fieldset
input(type='hidden', name='goto', value=req.query.goto)#goto-register

Expand Down

0 comments on commit ec8b268

Please sign in to comment.