generated from Code-the-Dream-School/react-node-practicum-back-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
445 additions
and
9 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const mainController = {}; | ||
const bcrypt = require('bcryptjs') | ||
|
||
const { PrismaClient } = require ("@prisma/client"); | ||
const { Public } = require('@prisma/client/runtime/library'); | ||
const prisma = new PrismaClient(); | ||
|
||
mainController.register = async (req, res) => { | ||
|
||
const {username, email, password_hash, role} = req.body; | ||
password = await hashPassword(password_hash) | ||
console.log(username, email, password_hash, role); | ||
const user = await prisma.users.create({data:{ | ||
username: username, | ||
email: email, | ||
password_hash: password, | ||
role_id:12 | ||
}}) | ||
|
||
return res.json({ | ||
message: 'This is a full stack app!', | ||
data: user, | ||
}); | ||
}; | ||
|
||
|
||
async function hashPassword(password) { | ||
const salt = await bcrypt.genSalt(10) | ||
password = await bcrypt.hash(password, salt) | ||
console.log(password); | ||
return password; | ||
} | ||
|
||
|
||
module.exports = mainController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const { createUser } = require('../services/userService'); | ||
|
||
const createUserController = async (req, res) => { | ||
try { | ||
const user = await createUser(req.body); | ||
res.status(201).json(user); | ||
} catch (error) { | ||
console.log(error) | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
createUserController, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const userSchema = require('../models/userSchema'); | ||
const bcrypt = require('bcryptjs'); | ||
|
||
const prismaValidationMiddleware = async (params, next) => { | ||
|
||
if (params.model === 'users' && params.action === 'create') { | ||
|
||
const userInput = params.args.data; | ||
// Validate input data | ||
const { error } = userSchema.validate(userInput); | ||
if (error) { | ||
throw new Error(`Validation Error: ${error.message}`); | ||
} | ||
} | ||
|
||
// Intercept User model actions | ||
if (params.model === 'users') { | ||
|
||
if (params.action === 'create' || params.action === 'update') { | ||
const userInput = params.args.data; | ||
|
||
if (userInput && userInput.password_hash) { | ||
// Hash the password | ||
const salt = await bcrypt.genSalt(10); | ||
userInput.password_hash = await bcrypt.hash(userInput.password_hash, salt); | ||
} | ||
} | ||
} | ||
|
||
return next(params); | ||
}; | ||
|
||
module.exports = prismaValidationMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const Joi = require('joi'); | ||
|
||
const userSchema = Joi.object({ | ||
username: Joi.string().min(2).max(50).required(), | ||
email: Joi.string().email().required(), | ||
password_hash: Joi.string().min(8).required(), | ||
role_id:Joi.number() | ||
}); | ||
|
||
module.exports = userSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const { PrismaClient } = require('@prisma/client'); | ||
const prismaValidationMiddleware = require('../middlewares/prismaValidationMiddleware'); | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
// Apply the middleware | ||
prisma.$use(prismaValidationMiddleware); | ||
|
||
module.exports = prisma; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const {register} = require('../controllers/authController.js'); | ||
const { route } = require('./authRouter.js'); | ||
|
||
router.route('/register').post(register) | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const express = require('express'); | ||
const { createUserController } = require('../controllers/userController'); | ||
|
||
const router = express.Router(); | ||
|
||
router.post('/users', createUserController); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const prisma = require('../prisma/prismaClient'); | ||
|
||
const createUser = async (userData) => { | ||
try { | ||
const user = await prisma.users.create({ | ||
data: userData, | ||
}); | ||
return user; | ||
} catch (error) { | ||
if (error.code === 'P2002') { | ||
// Unique constraint error | ||
throw new Error(`Field ${error.meta.target} must be unique`); | ||
} else { | ||
// Log the error and rethrow it | ||
console.error('Unexpected error:', error); | ||
throw new Error('An unexpected error occurred'); | ||
} | ||
} | ||
}; | ||
|
||
module.exports = { | ||
createUser, | ||
}; |