summary refs log tree commit diff
path: root/src/api
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-06-01 04:40:09 +0200
committerRory& <root@rory.gay>2025-06-01 04:40:09 +0200
commita22c00fcefa10a99505c05393106fb3a655de243 (patch)
tree417757b1e5aa9f44702ed963569e0edf6209043d /src/api
parentAdd some tests (diff)
downloadnodejs-final-assignment-a22c00fcefa10a99505c05393106fb3a655de243.tar.xz
Add register with validation
Diffstat (limited to 'src/api')
-rw-r--r--src/api/middlewares/errorMiddleware.js39
-rw-r--r--src/api/routes/auth/registerRoute.js22
-rw-r--r--src/api/start.js6
3 files changed, 58 insertions, 9 deletions
diff --git a/src/api/middlewares/errorMiddleware.js b/src/api/middlewares/errorMiddleware.js
new file mode 100644

index 0000000..b8de68e --- /dev/null +++ b/src/api/middlewares/errorMiddleware.js
@@ -0,0 +1,39 @@ +import { SafeNSoundError } from '#util/error.js'; +import { MongoServerError } from 'mongodb'; + +export function handleErrors(err, req, res, _next) { + if (err instanceof MongoServerError) { + if (err.code === 11000) { + // Duplicate key error + const newErr = new SafeNSoundError({ + errCode: 'DUPLICATE_KEY_ERROR', + message: 'A duplicate key error occurred.', + key: Object.keys(err.keyPattern)[0], + value: err.keyValue[Object.keys(err.keyValue)[0]] + }); + + err = newErr; + } + } + + if (err instanceof SafeNSoundError) { + console.log('meow'); + console.error(err.stack); + res.status(500).json(err); + } else { + // log and handle uncaught exceptions + console.error( + 'Unhandled error:', + Object.getPrototypeOf(err), + JSON.stringify(err, null, 2) + ); + console.error(err.stack); + res.status(500).json( + new SafeNSoundError({ + errCode: 'INTERNAL_SERVER_ERROR', + message: null, + err + }) + ); + } +} diff --git a/src/api/routes/auth/registerRoute.js b/src/api/routes/auth/registerRoute.js
index f2befd8..87762d3 100644 --- a/src/api/routes/auth/registerRoute.js +++ b/src/api/routes/auth/registerRoute.js
@@ -1,14 +1,20 @@ -import { DbUser } from '#db/index.js'; +import { registerUser } from '#db/index.js'; +import { LoginDto, RegisterDto } from '#dto/index.js'; export const registerRoute = { route: '/auth/register', - async onGet(req, res) { - const result = await User.create({ - username: req.query.username, - password: req.query.password, - email: req.query.email - }); + async onPost(req, res) { + const data = await RegisterDto.create(req.body); + await registerUser(data); + res.send(data); + } +}; - res.send(result); +export const loginRoute = { + route: '/auth/login', + async onPost(req, res) { + const data = await LoginDto.create(req.body); + await registerUser(data); + res.send(data); } }; diff --git a/src/api/start.js b/src/api/start.js
index 6f57afd..dcd5cf3 100644 --- a/src/api/start.js +++ b/src/api/start.js
@@ -2,7 +2,8 @@ import express from 'express'; import { registerRoutes } from './routes.js'; import { useCors, useLogging } from './middlewares/index.js'; import { initDb } from '#db/index.js'; -import {initJwt} from "#util/index.js"; +import { initJwt } from '#util/index.js'; +import { handleErrors } from '#api/middlewares/errorMiddleware.js'; const app = express(); const PORT = process.env.PORT ?? 3000; @@ -15,6 +16,7 @@ await initJwt(); app.use(express.json()); app.use(useCors); app.disable('x-powered-by'); +app.set('json spaces', 2); if (logRequests) { app.use(useLogging(logRequests)); @@ -22,6 +24,8 @@ if (logRequests) { registerRoutes(app); +app.use(handleErrors); + app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });