Skip to content

Commit 4ef5cfd

Browse files
Merge pull request #8 from TeamProjectsReact/v6.0.0/Start
V6.0.0/start
2 parents aba6fc2 + efab8c4 commit 4ef5cfd

File tree

13 files changed

+2252
-47
lines changed

13 files changed

+2252
-47
lines changed

BackEndMongoMVC/server/.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
MONGODB_URI=mongodb://localhost:27017/DBNAME
2+
JWT_SECRET=your_jwt_secret_key
3+
PORT=5000
4+
5+
# DBNAME change to your Database Name

BackEndMongoMVC/server/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
uploads
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const bcrypt = require('bcrypt')
2+
const jwt = require('jsonwebtoken')
3+
const User = require('../Models/User')
4+
5+
const AuthController = {
6+
SignUp: async (req, res) => {
7+
try{
8+
const {username, email, password} = req.body
9+
10+
const chechuser = await User.findOne({
11+
$or: [
12+
{username: username},
13+
{email: email}
14+
]
15+
})
16+
17+
if(chechuser) {
18+
return res.json({ Error: "User Already Exists"})
19+
}
20+
else{
21+
const hashPass = await bcrypt.hash(password, 10)
22+
23+
const NewUser = new User({
24+
username: username,
25+
email: email,
26+
password: hashPass,
27+
Role: "User"
28+
})
29+
30+
const ResultUser = NewUser.save()
31+
32+
if(ResultUser) {
33+
return res.json({ Status: "Success"})
34+
}
35+
else{
36+
return res.json({ Error: "Internal Server Error"})
37+
}
38+
}
39+
40+
}
41+
catch (err){
42+
console.log(err)
43+
}
44+
},
45+
46+
SignIn: async(req, res) => {
47+
try{
48+
const {email, password} = req.body
49+
50+
const findUser = await User.findOne({ email: email})
51+
52+
if(findUser){
53+
const chechpass = await bcrypt.compare(password, findUser.password)
54+
55+
if(chechpass){
56+
const token = jwt.sign({ userId: findUser._id, userEmail: findUser.email, userRole: findUser.Role }, process.env.JWT_SECRET, { expiresIn: '1h' });
57+
58+
return res.json({Status: "Success", Token:token, Result: findUser})
59+
}
60+
else{
61+
return res.json({ Error: "Passowrd not Match...."})
62+
}
63+
}
64+
else{
65+
return res.json({ Error: "No User Found...."})
66+
}
67+
68+
}
69+
catch (err) {
70+
console.log(err)
71+
}
72+
}
73+
}
74+
75+
module.exports = AuthController

BackEndMongoMVC/server/Models/User.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const mongoose = require('mongoose')
2+
3+
const UserSchema = new mongoose.Schema({
4+
username: {
5+
type: String,
6+
required: true,
7+
unique:true,
8+
},
9+
email: {
10+
type: String,
11+
required: true,
12+
unique: true,
13+
},
14+
password: {
15+
type: String,
16+
required: true
17+
},
18+
Role: {
19+
type: String,
20+
required: true,
21+
},
22+
createdAt: {
23+
type: Date,
24+
default: Date.now
25+
}
26+
})
27+
28+
const User = mongoose.model('User', UserSchema)
29+
30+
module.exports = User
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const express = require('express');
2+
const AuthController = require('../Controllers/AuthController');
3+
4+
const router = express.Router()
5+
6+
router.post('/SignUp', AuthController.SignUp)
7+
router.post('/SignIn', AuthController.SignIn)
8+
9+
module.exports = router

0 commit comments

Comments
 (0)