Skip to content

Commit f63d42a

Browse files
committed
hide env
1 parent 1c3f1c0 commit f63d42a

7 files changed

Lines changed: 53 additions & 50 deletions

File tree

.env

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
PORT=5000
22

3-
URL = mongodb+srv://admin:043@mycloud.ekjf4.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
3+
URL = mongodb+srv://admin:043@mycloud.ekjf4.mongodb.net/mblogc?retryWrites=true&w=majority
4+
5+
SECRET = myiddi

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
# misc
1515
.env
16+
/.env
1617
.DS_Store
1718
.env
1819
.env.local

controllers/userController.js

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const {body, validationResult} = require("express-validator");
1+
const { body, validationResult } = require("express-validator");
22
const User = require("../models/User")
33
const bcrypt = require("bcrypt");
44
const jwt = require("jsonwebtoken");
5+
require('dotenv').config()
56

67

78

@@ -12,50 +13,48 @@ const jwt = require("jsonwebtoken");
1213
module.exports.registerValidations = [
1314
body("name").not().isEmpty().trim().withMessage("Name is Required"),
1415
body("email").not().isEmpty().trim().withMessage("Email is Required"),
15-
body("password").isLength({min: 8}).withMessage('Password must be eight character Long')
16+
body("password").isLength({ min: 8 }).withMessage('Password must be eight character Long'),
1617
];
1718
// trim() extra spaces ko remove karega
1819

1920

2021
// ye yaha export hraha hy our routes/userroutes may imort hoga
21-
module.exports.register = async (req,res)=>{
22-
// res.json(req.body);
23-
const {name, email, password}= req.body;
24-
// res.send(password);
25-
const errors = validationResult(req);
26-
if(!errors.isEmpty())
27-
{
28-
res.status(400).json({errors: errors.array()})
29-
}
30-
// else{
31-
// res.json("You have done! thankYou")
32-
// }
33-
34-
try{
35-
const checkUser = await User.findOne({email})
36-
if(checkUser){
37-
return res.status(400).json({errors: [{msg: 'This Email Is Already Exist'}]});
38-
}
39-
40-
const salt = await bcrypt.genSalt(10);
41-
// hash password
42-
const hash = await bcrypt.hash(password, salt);
43-
try{
44-
const user = await User.create({
45-
name,
46-
email,
47-
password: hash,
48-
})
49-
50-
}catch(error){
51-
return res.status(500).json({errors: error});
52-
}
53-
54-
55-
}
56-
57-
catch(error){
58-
return res.status(500).json({errors: error});
59-
}
60-
// 5000 server internal error
22+
module.exports.register = async (req, res) => {
23+
const { name, email, password } = req.body;
24+
const errors = validationResult(req);
25+
if (!errors.isEmpty()) {
26+
return res.status(400).json({ errors: errors.array() });
27+
}
28+
try {
29+
const checkUser = await User.findOne({ email });
30+
if (checkUser) {
31+
return res
32+
.status(400)
33+
.json({ errors: [{ msg: 'Email is already taken' }] });
34+
}
35+
36+
// Hash password
37+
const salt = await bcrypt.genSalt(10);
38+
const hash = await bcrypt.hash(password, salt);
39+
try {
40+
const user = await User.create({
41+
name,
42+
email,
43+
password: hash,
44+
});
45+
const token = createToken(user);
46+
return res
47+
.status(200)
48+
.json({ msg: 'Your account has been created', token });
49+
} catch (error) {
50+
return res.status(500).json({ errors: "error h" });
51+
}
52+
53+
54+
}
55+
56+
catch (error) {
57+
return res.status(500).json({ errors: "one" });
58+
}
59+
// 5000 server internal error
6160
}

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const express = require("express");
2-
const bodyParser = require("body-parser");
2+
const bodyParser = require("body-parser");
33
const connect = require("./config/db");
44
const router = require("./routes/userRoutes");
55
require('dotenv').config()
@@ -20,7 +20,7 @@ app.use("/", router);
2020
// })
2121

2222
const PORT = process.env.PORT || 5000
23-
app.listen(PORT, ()=>{
23+
app.listen(PORT, () => {
2424
console.log('Your app is running');
2525
});
2626

models/User.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {model, Schema} = require("mongoose");
1+
const { model, Schema } = require("mongoose");
22
const { use } = require("../routes/userRoutes");
33

44
const userSchema = new Schema({
@@ -10,11 +10,11 @@ const userSchema = new Schema({
1010
type: String,
1111
required: true,
1212
},
13-
password:{
13+
password: {
1414
type: String,
1515
required: true,
1616
},
17-
},{
17+
}, {
1818
timestamps: true
1919
});
2020

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"dev": "nodemon index.js"
7+
"dev": "nodemon index.js",
8+
"stop": "nodemon index.js"
89
},
910
"keywords": [],
1011
"author": "",

routes/userRoutes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const app = require("express");
22
const router = app.Router();
3-
const {register, registerValidations} = require("../controllers/userController");
3+
const { register, registerValidations } = require("../controllers/userController");
44
router.post("/register", registerValidations, register); // ye register imrt howa usercontroler se
55

66
module.exports = router;

0 commit comments

Comments
 (0)