Skip to content

Commit 2a6ca6a

Browse files
committed
initial migration for auth and simple entities
1 parent cfcd8b1 commit 2a6ca6a

18 files changed

+834
-211
lines changed

.eslintrc.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
module.exports = {
22
"parserOptions": {
3-
"ecmaVersion": 9,
4-
"ecmaFeatures": {
5-
"impliedStrict": true,
6-
experimentalObjectRestSpread: true
7-
}
3+
"ecmaVersion": 2022
84
},
95
"globals": {
106
"describe": false,

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:16.11-alpine
1+
FROM node:20.10-alpine3.19
22
RUN mkdir -p /mustachebash
33
WORKDIR /mustachebash
44
COPY package.json package-lock.json ticket-logo.png ./

lib/config.js

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ module.exports = {
1313
transactionSecret: process.env.JWT_TRANSACTION_SECRET,
1414
ticketSecret: process.env.JWT_TICKET_SECRET
1515
},
16+
google: {
17+
identityClientId: process.env.GOOGLE_IDENTITY_CLIENT_ID
18+
},
1619
mailgun: {
1720
domain: process.env.MAILGUN_DOMAIN,
1821
apiKey: process.env.MAILGUN_API_KEY

lib/middleware/auth.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
const { role, sub } = validateAccessToken(accessToken);
1515

1616
ctx.user = {
17+
id: sub,
1718
username: sub,
1819
role
1920
};

lib/routes/events.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ eventsRouter
2424
try {
2525
const event = await getEvent(ctx.params.id);
2626

27-
if(!event) ctx.throw(404);
28-
2927
return ctx.body = event;
3028
} catch(e) {
29+
if(e.code === 'NOT_FOUND') ctx.throw(404);
30+
3131
ctx.throw(e);
3232
}
3333
})
3434
.patch('/:id', requiresPermission('admin'), async ctx => {
3535
try {
36-
const event = await updateEvent(ctx.params.id, {updatedBy: ctx.user.username, ...ctx.request.body});
36+
const event = await updateEvent(ctx.params.id, {...ctx.request.body, updatedBy: ctx.user.id});
3737

3838
return ctx.body = event;
3939
} catch(e) {

lib/routes/index.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
const Router = require('@koa/router'),
66
{ authorizeUser, requiresPermission } = require('../middleware/auth'),
7-
{ authenticateUser, refreshAccessToken } = require('../services/auth'),
7+
{ authenticateUser, authenticateGoogleUser, refreshAccessToken } = require('../services/auth'),
88
{ validateTransactionToken } = require('../services/transactions'),
99
{ generateTicketsPDF } = require('../services/pdf'),
1010
{ getTransactionTickets, checkInWithTicket } = require('../services/guests'),
@@ -114,10 +114,23 @@ apiRouter
114114

115115
apiRouter
116116
.post('/authenticate', async ctx => {
117-
if(!ctx.request.body.username || !ctx.request.body.password) ctx.throw(400);
117+
if(
118+
(!ctx.request.body.username || !ctx.request.body.password) &&
119+
!ctx.request.body.token
120+
) ctx.throw(400);
118121

119122
try {
120-
const user = await authenticateUser(ctx.request.body.username, ctx.request.body.password);
123+
let user;
124+
switch(ctx.request.body.authority) {
125+
case 'google':
126+
user = await authenticateGoogleUser(ctx.request.body.token);
127+
break;
128+
129+
case 'email':
130+
default:
131+
user = await authenticateUser(ctx.request.body.username, ctx.request.body.password);
132+
break;
133+
}
121134

122135
return ctx.body = user;
123136
} catch(e) {

lib/routes/products.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ productsRouter
4646
})
4747
.patch('/:id', async ctx => {
4848
try {
49-
const product = await updateProduct(ctx.params.id, ctx.request.body);
49+
const product = await updateProduct(ctx.params.id, {...ctx.request.body, updatedBy: ctx.user.id});
5050

5151
return ctx.body = product;
5252
} catch(e) {

lib/routes/promos.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ promosRouter
1919
})
2020
.post('/', authorizeUser, requiresPermission('admin'), async ctx => {
2121
try {
22-
const promo = await createPromo({createdBy: ctx.user.username, ...ctx.request.body});
22+
const promo = await createPromo({...ctx.request.body, createdBy: ctx.user.id});
2323

2424
ctx.set('Location', `https://${ctx.host}${ctx.path}/${promo.id}`);
2525
ctx.status= 201;
@@ -61,7 +61,7 @@ promosRouter
6161
})
6262
.delete('/:id', authorizeUser, requiresPermission('admin'), async ctx => {
6363
try {
64-
const promo = await updatePromo(ctx.params.id, {updatedBy: ctx.user.username, status: 'disabled'});
64+
const promo = await updatePromo(ctx.params.id, {updatedBy: ctx.user.id, status: 'disabled'});
6565

6666
return ctx.body = promo;
6767
} catch(e) {

lib/routes/users.js

+3-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Router = require('@koa/router'),
2-
{ authorizeUser, requiresPermission } = require('../middleware/auth'),
3-
{ createUser, getUsers, getUser, updateUser } = require('../services/auth');
2+
{ authorizeUser } = require('../middleware/auth'),
3+
{ getUsers, getUser } = require('../services/auth');
44

55
const usersRouter = new Router({
66
prefix: '/users'
@@ -17,39 +17,15 @@ usersRouter
1717
} catch(e) {
1818
ctx.throw(e);
1919
}
20-
})
21-
.post('/', requiresPermission('god'), async ctx => {
22-
if(!ctx.request.body.username || !ctx.request.body.password) ctx.throw(400);
23-
24-
try {
25-
const user = await createUser(ctx.request.body);
26-
27-
delete user.password;
28-
ctx.set('Location', `https://${ctx.host}${ctx.path}/${user.id}`);
29-
ctx.status = 201;
30-
return ctx.body = user;
31-
} catch(e) {
32-
ctx.throw(e);
33-
}
3420
});
3521

3622
usersRouter
37-
.get('/:username', async ctx => {
23+
.get('/:id', async ctx => {
3824
try {
3925
const user = await getUser(ctx.params.username);
4026

4127
if(!user) ctx.throw(404);
4228

43-
delete user.password;
44-
return ctx.body = user;
45-
} catch(e) {
46-
ctx.throw(e);
47-
}
48-
})
49-
.patch('/:username', requiresPermission('god'), async ctx => {
50-
try {
51-
const user = await updateUser(ctx.params.username, ctx.request.body);
52-
5329
delete user.password;
5430
return ctx.body = user;
5531
} catch(e) {

0 commit comments

Comments
 (0)