change server to heroku

This commit is contained in:
Priyatham Sai Chand 2021-03-25 11:28:04 +05:30
parent 28cf8a7312
commit 454160f415
13 changed files with 5 additions and 1872 deletions

2
.gitignore vendored
View File

@ -6,7 +6,7 @@ client/.pnp
client/.env
/client.pnp.js
server/node_modules
node_modules
# testing
/coverage

View File

@ -1 +0,0 @@
web: nodemon server.js

View File

@ -1,23 +0,0 @@
const mongoose = require("mongoose");
const config = require('config');
const connectToDb = async () => {
try{
await mongoose.connect(
config.get('mongoURI'),
{
useCreateIndex:true,
useFindAndModify:true,
useUnifiedTopology:true,
useNewUrlParser: true
}
)
console.log("mongo connection secure!");
} catch(error){
console.log(error);
process.exit(1);
}
}
module.exports = connectToDb;

View File

@ -1,4 +0,0 @@
{
"mongoURI": "mongodb+srv://admin:ABxXFUBs5FMiAaDJ@form.iynew.mongodb.net/<dbname>?retryWrites=true&w=majority",
"jwtSecret": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
}

View File

@ -1,25 +0,0 @@
const jwt = require("jsonwebtoken");
const config = require("config");
const auth = (req, res, next) => {
try {
const token = req.header("x-auth-token");
if (!token)
return res
.status(401)
.json({ msg: "No authentication token, authorization denied." });
const verified = jwt.verify(token, config.get("jwtSecret") );
if (!verified)
return res
.status(401)
.json({ msg: "Token verification failed, authorization denied." });
req.user = verified.id;
next();
} catch (err) {
res.status(500).json({ error: err.message });
}
};
module.exports = auth;

1583
server/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,28 +0,0 @@
{
"name": "auth",
"version": "1.0.0",
"description": "sign up and sign in auth",
"main": "server.js",
"scripts": {
"start": "nodemon server.js",
"app": "cd .. && npm start"
},
"author": "B. Priyatham Sai chand",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"config": "^3.3.2",
"context": "^1.1.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-validator": "^6.6.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.10.13",
"nodemon": "^2.0.6"
},
"engines": {
"node": "14.x",
"npm": "6.14.8"
}
}

View File

@ -1,137 +0,0 @@
const router = require("express").Router();
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const auth = require("../middleware/auth");
const User = require("../schemas/User");
const config = require("config");
router.post("/register", async (req, res) => {
try {
let { username,email,phonenumber,password} = req.body;
if (!email || !password )
return res.status(400).json({ msg: "Not all fields have been entered." });
if (password.length < 5)
return res
.status(400)
.json({ msg: "The password needs to be at least 5 characters long." });
const existingUser = await User.findOne({ email: email });
if (existingUser)
return res
.status(400)
.json({ msg: "An account with this email already exists." });
if (!username) username = email;
const salt = await bcrypt.genSalt();
const passwordHash = await bcrypt.hash(password, salt);
const newUser = new User({
username,
email,
phonenumber,
password: passwordHash,
});
const savedUser = await newUser.save();
res.json(savedUser);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post("/login", async (req, res) => {
try {
const { email, password } = req.body;
// validate
if (!email || !password)
return res.status(400).json({ msg: "Not all fields have been entered." });
const user = await User.findOne({ email: email });
if (!user)
return res
.status(400)
.json({ msg: "No account with this email has been registered." });
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) return res.status(400).json({ msg: "Invalid credentials." });
const token = jwt.sign({ id: user._id },config.get('jwtSecret'));
if(token) return res
.json({
token,
user: {
id: user._id,
username: user.username,
pricing: user.pricing
},
});
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.delete("/delete", auth, async (req, res) => {
try {
const deletedUser = await User.findByIdAndDelete(req.user);
res.json(deletedUser);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post("/tokenIsValid", async (req, res) => {
try {
const token = req.header("x-auth-token");
if (!token) return res.json({error: message});
const verified = jwt.verify(token, config.get("jwtSecret"));
if (!verified) return res.json({error: message});
const user = await User.findById(verified.id);
if (!user) return res.json({error: message});
return res.json(true);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.get("/", auth, async (req, res) => {
const user = await User.findById(req.user);
console.log(user);
res.json({
username: user.username,
id: user._id,
});
});
router.put("/update", async (req, res) => {
const { id,pricing } = req.body;
console.log("id " + id)
if (!id) {
return res.status(400).json({ Msg: "Not all fields have been entered." });
}
User.findByIdAndUpdate(id, { pricing: pricing }).then(() => {
User.findOne({ _id: id }).then((user) => {
res.send(user);
console.log(user)
})
})
})
module.exports = router;

View File

@ -1,33 +0,0 @@
const mongoose = require('mongoose');
const UserSchema = mongoose.Schema({
username: {
type: String,
required: true
},
email: {
type: String,
required:true,
unique: true
},
phonenumber: {
type: Number,
required: true
},
password: {
type:String,
required:true,
minlength: 5
},
pricing: {
type:String,
enum: ['free','basic','intermediate','luxury'],
default:'free',
required:false
}
});
module.exports = User = mongoose.model('user',UserSchema);

View File

@ -1,33 +0,0 @@
const express = require("express");
const config = require("config");
const mongoose = require("mongoose");
const cors = require("cors");
// set up express
const app = express();
app.use(express.json());
app.use(cors());
app.get('/', (req, res) => { res.send('Hello from Express!')});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`The server has started on port: ${PORT}`));
// set up mongoose
mongoose.connect(
config.get('mongoURI'),
{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
},
(err) => {
if (err) throw err;
console.log("MongoDB connection established");
}
);
// set up routes
app.use("/users", require("./routes/users"));

View File

@ -19,7 +19,7 @@ const Login = () => {
try {
const loginUser = { email, password };
const loginRes = await Axios.post(
"http://localhost:5000/users/login",
"https://server-locaft.heroku.app/users/login",
loginUser
);
setUserData({

View File

@ -211,7 +211,7 @@ const submit = async (props) => {
try {
const id = userData.user.id;
const pricingRes = await Axios.put(
"http://localhost:5000/users/update", {
"https://server-locaft.herokuapp.com/users/update", {
id,
pricing

View File

@ -20,8 +20,8 @@ import ErrorNotice from "./ErrorNotice";
try {
const newUser = { username,email,phonenumber,password};
await Axios.post("http://localhost:5000/users/register", newUser);
const loginRes = await Axios.post("http://localhost:5000/users/login", {
await Axios.post("https://server-locaft.herokuapp.com/users/register", newUser);
const loginRes = await Axios.post("https://server-locaft.herokuapp.com/users/login", {
email,
password,
});