Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit 35796c2

Browse files
authored
Merge pull request #2 from redpwn/feature/database-auth
Database Authentication
2 parents 844ab1b + 01f7ef5 commit 35796c2

File tree

7 files changed

+392
-11
lines changed

7 files changed

+392
-11
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
APP_TOKEN_KEY=
2+
DATABASE_URL=postgresql://user:[email protected]:5432/rctf

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ We have designed rctf with the following attributes in mind:
1717

1818
## Installation
1919

20-
_TODO_
20+
### Database
21+
22+
The application is built on a PostgreSQL database. You should add the appropriate connection string in `.env`. Then run the following command to setup the database.
23+
24+
```
25+
yarn run migrate up
26+
```
27+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
exports.up = function (pgm) {
2+
pgm.createExtension('uuid-ossp')
3+
pgm.createTable('users', {
4+
id: { type: 'uuid', primaryKey: true },
5+
name: { type: 'string', unique: true, notNull: true },
6+
email: { type: 'string', unique: true, notNull: true },
7+
password: { type: 'string', notNull: true },
8+
division: { type: 'string', notNull: true }
9+
})
10+
}
11+
12+
exports.down = function (pgm) {
13+
pgm.dropTable('users')
14+
pgm.dropExtension('uuid-ossp')
15+
}
16+
17+
exports._meta = {
18+
version: 1
19+
}

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
"private": true,
77
"scripts": {
88
"lint": "standard",
9-
"start": "node index.js"
9+
"start": "node index.js",
10+
"migrate": "node-pg-migrate"
1011
},
1112
"dependencies": {
1213
"body-parser": "^1.19.0",
1314
"dotenv": "^8.2.0",
14-
"express": "^4.17.1"
15+
"express": "^4.17.1",
16+
"node-pg-migrate": "^4.2.2",
17+
"pg": "^7.18.1"
1518
},
1619
"devDependencies": {
1720
"standard": "^14.3.1"

server/database/auth.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1+
const db = require('./db')
12

2-
module.exports = {
3+
const ret = {
34
getUser: ({ id }) => {
4-
5+
return db.query('SELECT * FROM users WHERE id = $1', [id])
6+
.then(res => res.rows[0])
57
},
68
makeUser: ({ id, name, email, division, password }) => {
7-
9+
return db.query('INSERT INTO users (id, name, email, division, password) VALUES ($1, $2, $3, $4, $5) RETURNING *',
10+
[id, name, email, division, password]
11+
)
12+
.then(res => res.rows[0])
813
},
914
updateUser: ({ id, name, email, division, password }) => {
15+
return ret.getUser({ id })
16+
.then(user => {
17+
const upd = { name, email, division, password }
18+
Object.keys(upd).forEach(key => {
19+
if (upd[key] === undefined) delete upd[key]
20+
})
1021

22+
user = Object.assign(user, upd)
23+
24+
return db.query('UPDATE users SET name = $1, email = $2, division = $3, password = $4 WHERE id = $5 RETURNING *',
25+
[user.name, user.email, user.division, user.password, user.id]
26+
)
27+
})
28+
.then(res => res.rows[0])
1129
}
1230
}
31+
32+
module.exports = ret

server/database/db.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const connectionString = process.env.DATABASE_URL
2+
3+
const { Pool } = require('pg')
4+
const pool = new Pool({
5+
connectionString
6+
})
7+
8+
module.exports = pool

0 commit comments

Comments
 (0)