diff --git a/package-lock.json b/package-lock.json index f5990cb..69c89ce 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "@prisma/client": "^4.15.0", + "bcryptjs": "^2.4.3", "dotenv": "^16.1.4", "express": "^4.18.2", "jsonwebtoken": "^9.0.0", @@ -17,6 +18,7 @@ "prisma": "^4.15.0" }, "devDependencies": { + "@types/bcryptjs": "^2.4.2", "@types/express": "^4.17.17" } }, @@ -51,6 +53,12 @@ "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-4.15.0-28.8fbc245156db7124f997f4cecdd8d1219e360944.tgz", "integrity": "sha512-sVOig4tjGxxlYaFcXgE71f/rtFhzyYrfyfNFUsxCIEJyVKU9rdOWIlIwQ2NQ7PntvGnn+x0XuFo4OC1jvPJKzg==" }, + "node_modules/@types/bcryptjs": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/@types/bcryptjs/-/bcryptjs-2.4.2.tgz", + "integrity": "sha512-LiMQ6EOPob/4yUL66SZzu6Yh77cbzJFYll+ZfaPiPPFswtIlA/Fs1MzdKYA7JApHU49zQTbJGX3PDmCpIdDBRQ==", + "dev": true + }, "node_modules/@types/body-parser": { "version": "1.19.2", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.2.tgz", @@ -168,6 +176,11 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" }, + "node_modules/bcryptjs": { + "version": "2.4.3", + "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz", + "integrity": "sha512-V/Hy/X9Vt7f3BbPJEi8BdVFMByHi+jNXrYkW3huaybV/kQ0KJg0Y6PkEMbn+zeT+i+SiKZ/HMqJGIIt4LZDqNQ==" + }, "node_modules/body-parser": { "version": "1.20.1", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", diff --git a/package.json b/package.json index 472380d..3c8b04a 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "@prisma/client": "^4.15.0", + "bcryptjs": "^2.4.3", "dotenv": "^16.1.4", "express": "^4.18.2", "jsonwebtoken": "^9.0.0", @@ -17,6 +18,7 @@ "prisma": "^4.15.0" }, "devDependencies": { + "@types/bcryptjs": "^2.4.2", "@types/express": "^4.17.17" } } diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts new file mode 100644 index 0000000..a7b7539 --- /dev/null +++ b/src/controllers/UserController.ts @@ -0,0 +1,34 @@ +import { Request, Response } from 'express'; +import bcrypt from 'bcryptjs'; +import { User } from '@prisma/client'; +import { createUser } from '../models/User'; + +async function createUserController(req: Request, res: Response) { + try { + const { firstName, lastName, email, password, address } = req.body; + + const hashedPassword = await bcrypt.hash(password, 10); + + const user: User = await createUser({ + firstName, + lastName, + email, + password: hashedPassword, + address, + id: '', + createdAt: undefined, + updatedAt: undefined + }); + + res.json(hashedPassword); + } catch (error) { + console.error('Error creating user:', error); + res.status(500).json({ error: 'An error occurred while creating the user.' }); + } +} + +const UserController = { + createUser: createUserController, +}; + +export default UserController; diff --git a/src/index.ts b/src/index.ts index 49b4ea9..c4cb7c4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,14 @@ import express from 'express'; -import router from './routes'; +import userRoutes from './routes/user'; + const app = express(); +const PORT = 3000; app.use(express.json()); -app.use(router) -app.listen(3000, () => { +app.use('/users', userRoutes); + +app.listen(PORT, () => { console.log('Server started on port 3000!'); }) diff --git a/src/models/User.ts b/src/models/User.ts new file mode 100644 index 0000000..bb4f841 --- /dev/null +++ b/src/models/User.ts @@ -0,0 +1,17 @@ +import { PrismaClient, User } from '@prisma/client'; + +const prisma = new PrismaClient(); + +export async function createUser(user: User): Promise { + const createdUser = await prisma.user.create({ + data: { + firstName: user.firstName, + lastName: user.lastName, + email: user.email, + password: user.password, + address: user.address, + }, + }); + + return createdUser; +} diff --git a/src/routes/index.ts b/src/routes/index.ts deleted file mode 100644 index 5dcdcb5..0000000 --- a/src/routes/index.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { User } from '@prisma/client'; -import Router from 'express'; - -const router = Router() - -router.get('/', (req, res) => { - res.send('Hello World!') -}) - -router.post('/create-user', (req, res) => { - const { firstName, lastName, email, password, address } = req.body - - const newUser: User = { - firstName, - lastName, - email, - password, - address, - id: '', - createdAt: undefined, - updatedAt: undefined - } - -}) - - -export default router; \ No newline at end of file diff --git a/src/routes/user.ts b/src/routes/user.ts new file mode 100644 index 0000000..a8807ad --- /dev/null +++ b/src/routes/user.ts @@ -0,0 +1,8 @@ +import express from 'express'; +import UserController from '../controllers/UserController'; + +const router = express.Router(); + +router.post('/', UserController.createUser); + +export default router; \ No newline at end of file