Skip to content

Commit 6746a7b

Browse files
committed
Fix session problems, misc changes
1 parent 3653868 commit 6746a7b

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

logo.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
██████████████████████
2-
██████████████████████
3-
██████████████████████████████
1+
██████████████████████
2+
██████████████████████
3+
██████████████████████████████
44
██████████████████████████████ Welcome to the Cryb API!
5-
██████████████████████████████████████
5+
██████████████████████████████████████
66
██████████████████████████████████████ This server runs on port :PORT and requires the following:
77
████████████ ████████████
88
████████████ ████████████ * MongoDB (:27017)
99
████ ████ * Redis (:6379)
10-
████ ████████ ████ * @cryb/portals (:1337)
11-
████ ████████ ████
10+
████ ████████ ████ * @cryb/portals (:5000)
11+
████ ████████ ████
1212
████ ████████ ████ Have any issues? Open them at https://github.com/crybapp/api/issues
1313
████ ████████ ████ Working on new features? Merge them in at https://github.com/crybapp/api/merge_requests
14-
████ ████████ ████
14+
████ ████████ ████
1515
██████████████████████████████
1616
██████████████████████████████

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
"name": "@cryb/api",
33
"version": "1.0.0",
44
"description": "The core service used to handle REST and WS events from clients",
5-
"main": "index.js",
5+
"main": "dist/index.js",
66
"repository": "https://github.com/crybapp/api",
77
"author": "William Gibson <w.gibbo3@icloud.com>",
88
"license": "MIT",
99
"scripts": {
1010
"start": "node dist",
11-
"dev": "tsc-watch --outDir ./dist --onSuccess \"yarn start\" --compiler typescript/bin/tsc",
11+
"dev": "tsc-watch --compiler typescript/bin/tsc --onSuccess \"yarn start\"",
1212
"build": "tsc",
1313
"lint": "tslint src/**/*.ts{,x}"
1414
},
@@ -33,8 +33,6 @@
3333
"qs": "^6.8.0",
3434
"query-string": "^6.8.2",
3535
"supertest": "^4.0.2",
36-
"tsc-watch": "^2.2.1",
37-
"typescript": "^3.5.3",
3836
"ws": "^7.1.1"
3937
},
4038
"devDependencies": {
@@ -51,6 +49,8 @@
5149
"@types/passport-discord": "^0.1.3",
5250
"@types/passport-jwt": "^3.0.1",
5351
"@types/ws": "^6.0.1",
54-
"tslint": "^5.20.1"
52+
"tsc-watch": "^2.2.1",
53+
"tslint": "^5.20.1",
54+
"typescript": "^3.5.3"
5555
}
5656
}

src/config/passport.config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ passport.use(new Strategy({
1313
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
1414
secretOrKey: process.env.JWT_KEY
1515
}, async ({ id }, done) => {
16+
const noSessionResponses = ['USER_NO_AUTH', 'USER_NOT_FOUND']
17+
1618
try {
1719
const user = await new User().load(id)
1820

1921
return done(null, user)
2022
} catch (error) {
23+
if (error.response.indexOf(noSessionResponses))
24+
return done(UserNoAuth)
25+
2126
done(error)
2227
}
2328
}))
@@ -44,7 +49,7 @@ const fetchUser = async (
4449
} else
4550
passport.authenticate('jwt', { session: false }, async (err, user: User) => {
4651
if (err)
47-
return res.sendStatus(500)
52+
return handleError(err, res)
4853

4954
if (!user)
5055
return handleError(UserNoAuth, res)

src/controllers/internal.controller.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ app.put('/portal', authenticate, async (req, res) => {
5454
* Broadcast allocation to all online clients
5555
*/
5656
const updateMessage = new WSMessage(0, allocation, 'PORTAL_UPDATE')
57-
updateMessage.broadcast(online)
57+
await updateMessage.broadcast(online)
5858

5959
if (status === 'open') {
6060
const token = signApertureToken(id),
6161
apertureMessage = new WSMessage(0, { ws: process.env.APERTURE_WS_URL, t: token }, 'APERTURE_CONFIG')
6262

63-
apertureMessage.broadcast(online)
63+
await apertureMessage.broadcast(online)
6464
}
6565
}
6666

@@ -73,12 +73,12 @@ app.put('/portal', authenticate, async (req, res) => {
7373
app.post('/queue', authenticate, (req, res) => {
7474
const { queue } = req.body as { queue: string[] }
7575

76-
queue.forEach((id, i) => {
76+
queue.forEach(async (id, i) => {
7777
try {
7878
const op = 0, d = { pos: i, len: queue.length }, t = 'PORTAL_QUEUE_UPDATE',
7979
message = new WSMessage(op, d, t)
8080

81-
message.broadcastRoom(id)
81+
await message.broadcastRoom(id)
8282
} catch (error) {
8383
handleError(error, res)
8484
}

src/utils/errors.utils.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,13 @@ export const PortalNotOpen: IAPIResponse = {
217217
}
218218

219219
export const handleError = (error: any, res: Response) => {
220-
console.error(error)
221-
222-
if (!error) res.sendStatus(500)
223-
else if (error.response && error.error && error.status) res.status(error.status).send(error)
224-
else if (error.status) res.sendStatus(error.status)
225-
else res.sendStatus(500)
220+
if (process.env.NODE_ENV === 'development')
221+
console.error(error)
222+
223+
if (error)
224+
if (error.response && error.error && error.status)
225+
return res.status(error.status).send(error)
226+
else if (error.status)
227+
return res.sendStatus(error.status)
228+
res.sendStatus(500)
226229
}

0 commit comments

Comments
 (0)