Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/make header sticky while scrolling #39

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_BASE_URL = http://localhost:4000/api/v1
3 changes: 3 additions & 0 deletions backend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MONGODB_URL = mongodb://127.0.0.1:27017/
JWT_SECRET = "MYSECRET"
PORT = 4000
20 changes: 20 additions & 0 deletions backend/config/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/** @format */

const mongoose = require("mongoose");
require("dotenv").config();

const database_name = "Eventmint";

exports.dbConnect = () => {
mongoose
.connect(process.env.MONGODB_URL + database_name, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("Db connect successfully");
})
.catch((err) => {
console.log("Error in db connection");
});
};
154 changes: 154 additions & 0 deletions backend/controllers/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/** @format */
/** @format */

// Import the required modules
const express = require("express");
const router = express.Router();
const bcrypt = require("bcrypt");
const User = require("../models/User");
const jwt = require("jsonwebtoken");
require("dotenv").config();

// Route for user login
router.post("/login", async (req, res) => {
try {
// Get email and password from request body
const { email, password } = req.body;

// Check if email or password is missing
if (!email || !password) {
// Return 400 Bad Request status code with error message
return res.status(400).json({
success: false,
message: `All fields are required!`,
});
}

// Find user exist or not for this email
const user = await User.findOne({ email });

// If user not found
if (!user) {
// Return 401 Unauthorized status code with error message
return res.status(401).json({
success: false,
message: `User is not Registered with Us Please SignUp to Continue`,
});
}

// Generate JWT token and Compare Password
if (await bcrypt.compare(password, user.password)) {
const token = jwt.sign(
{
email: user.email,
id: user._id,
},
process.env.JWT_SECRET,
{
expiresIn: "24h",
}
);

// Save token to user document in database
user._doc.token = token;
user._doc.password = undefined;

// Set cookie for token and return success response
const options = {
expires: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000),
httpOnly: true,
};

res.cookie("token", token, options).status(200).json({
success: true,
token,
user,
message: `User Login Successfully!`,
});
//
} else {
return res.status(401).json({
success: false,
message: `Password is incorrect`,
});
}
//
} catch (error) {
console.error(error);
// Return 500 Internal Server Error status code with error message
return res.status(500).json({
success: false,
message: `Login Failure, Please Try Again!`,
});
}
});

// Route for user signup
router.post("/signup", async (req, res) => {
try {
// fetch all data from the request body
console.log("body");
console.log(req.body);
const { firstName, lastName, email, password, confirmPassword } = req.body;

// Check if All Details are there or not
if (!firstName || !lastName || !email || !password || !confirmPassword) {
return res.status(403).send({
success: false,
message: "All Fields are required",
});
}

if (!email.includes("@gmail.com")) {
return res.status(400).send({
success: false,
message: "Invalid Email!",
});
}

// Check if password and confirm password match or not
if (password !== confirmPassword) {
return res.status(400).json({
success: false,
message:
"Password and Confirm Password do not match. Please try again.",
});
}

// Check if user already exists
const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({
success: false,
message: "User already exists. Please login to continue.",
});
}

// Hash the password
const hashedPassword = await bcrypt.hash(password, 10);

// create user in db
const user = await User.create({
firstName,
lastName,
email,
password: hashedPassword,
image: `https://api.dicebear.com/5.x/initials/svg?seed=${firstName} ${lastName}`,
});

return res.status(200).json({
success: true,
user,
message: "User registered successfully!",
});
//
} catch (error) {
console.log(error.message);
return res.status(500).json({
success: false,
message: "User cannot be registered, Please try again!",
});
}
});

module.exports = router;
47 changes: 47 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/** @format */

// Importing necessary modules and packages
const express = require("express");
const app = express();
const userController = require("./controllers/auth");
const database = require("./config/database");
const cookieParser = require("cookie-parser");
const cors = require("cors");

// Loading environment variables from .env file
require("dotenv").config();

// Setting up port number
const PORT = process.env.PORT || 4000;

// Connecting to database
database.dbConnect();

// Middlewares
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(
cors({
origin: "*",
credentials: true,
})
);

// Setting up routes
app.use("/api/v1/auth", userController);

// Testing the server
app.get("/", (req, res) => {
return res.json({
success: true,
message: "Your server is up and running ...",
});
});

// Listening to the server
app.listen(PORT, () => {
console.log(`App is listening at ${PORT}`);
});

// End of code.
45 changes: 45 additions & 0 deletions backend/models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/** @format */

// Import the Mongoose library
const mongoose = require("mongoose");

// Define the user schema using the Mongoose Schema constructor
const userSchema = new mongoose.Schema(
{
// Define the name field with type String, required, and trimmed
firstName: {
type: String,
required: true,
trim: true,
},
lastName: {
type: String,
required: true,
trim: true,
},
// Define the email field with type String, required, and trimmed
email: {
type: String,
required: true,
trim: true,
},

// Define the password field with type String and required
password: {
type: String,
required: true,
},
token: {
type: String,
},
image: {
type: String,
required: true,
},
// Add timestamps for when the document is created and last modified
},
{ timestamps: true }
);

// Export the Mongoose model for the user schema, using the name "user"
module.exports = mongoose.model("user", userSchema);
Loading