Skip to content

Commit 474bc3d

Browse files
author
Brent Engelbrecht
committed
Persist user data to DB and authenticate against DB
1 parent d27d891 commit 474bc3d

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

src/datastore/repository.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,43 @@
11

2-
function saveUserDetails() {
2+
let users
3+
let api
4+
5+
async function dbClient(connection) {
6+
if (users) {
7+
return
8+
}
9+
10+
try {
11+
api = await connection.db(process.env.DB_NAMESPACE)
12+
users = await api.collection(process.env.DB_COLLECTION)
13+
} catch (e) {
14+
console.error(`Unable to establish a collection handle to apiUsers: ${e}`)
15+
}
16+
}
17+
18+
async function saveUserDetails(userDetails) {
19+
try {
20+
await users.insertOne(userDetails, { w: 'majority' })
21+
return { success: true }
22+
} catch (e) {
23+
if (String(e).startsWith("MongoError: E11000 duplicate key error")) {
24+
return { error: "A user with that id already exists." }
25+
}
26+
console.error(`Error occurred while adding new user, ${e}.`)
27+
return { error: e }
28+
}
329
}
430

5-
function getUserDetails() {
31+
async function getUserDetails(userName) {
32+
return await users.findOne({ "userName": userName.toLowerCase() })
633
}
734

8-
function authenticateUser(username, password) {
9-
return { username: username.toLowerCase() }
35+
async function authenticateUser(userName, password) {
36+
const userDetails = await users.findOne({ "userName": userName.toLowerCase(), "password": password })
37+
if (userDetails) {
38+
return true
39+
}
40+
return false
1041
}
1142

12-
module.exports = { saveUserDetails, getUserDetails, authenticateUser }
43+
module.exports = { dbClient, saveUserDetails, getUserDetails, authenticateUser }

src/routes/users.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
const jwt = require('jsonwebtoken')
22
const repository = require('../datastore/repository')
33

4-
function register (ctx) {
4+
async function register (ctx) {
5+
const payload = JSON.parse(ctx.request.body)
56
const {
67
firstName,
78
lastName,
89
userName,
910
password,
1011
email
11-
} = ctx.request
12+
} = payload
13+
14+
const userDetails = await repository.getUserDetails(userName)
15+
if (userDetails) {
16+
ctx.throw(409, 'User already exists.')
17+
}
1218

1319
repository.saveUserDetails({
1420
firstName,
@@ -17,16 +23,18 @@ function register (ctx) {
1723
password,
1824
email
1925
})
26+
27+
ctx.response.status = 201
2028
}
2129

22-
function login (ctx) {
23-
const { username, password } = ctx.request.body
24-
if (!username) ctx.throw(422, 'Username required.')
30+
async function login (ctx) {
31+
const { userName, password } = ctx.request.body
32+
if (!userName) ctx.throw(422, 'Username required.')
2533
if (!password) ctx.throw(422, 'Password required.')
2634

27-
const userDetails = repository.authenticateUser(username, password)
35+
const userDetails = await repository.authenticateUser(userName, password)
2836
if (!userDetails) {
29-
ctx.throw(err.status || 401, err.text)
37+
ctx.throw(401, 'Not authorised.')
3038
}
3139

3240
const payload = { sub: userDetails.username }

src/server.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,20 @@ const bodyParser = require('koa-bodyparser')
66
const authorisation = require('./middleware/authorisation')
77
const userRoutes = require('./routes/users')
88
const fileRoutes = require('./routes/files')
9+
const repository = require('./datastore/repository')
10+
const MongoClient = require('mongodb').MongoClient
911

1012
const port = process.env.PORT || 5000
1113

14+
MongoClient
15+
.connect(`${process.env.DB_URL}`, { useNewUrlParser: true })
16+
.catch(err => {
17+
console.error(err.stack)
18+
})
19+
.then(client => {
20+
repository.dbClient(client)
21+
})
22+
1223
const app = new Koa()
1324
const router = koaRouter()
1425

0 commit comments

Comments
 (0)