Skip to content

Commit

Permalink
Merge branch 'Add-login-and-signup-sahithi000'
Browse files Browse the repository at this point in the history
  • Loading branch information
sahithi000 committed May 15, 2024
2 parents 899b085 + 1d38ae1 commit e0aced3
Show file tree
Hide file tree
Showing 8 changed files with 1,002 additions and 11 deletions.
58 changes: 58 additions & 0 deletions Public/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

body{
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: aliceblue;
}
img{
width: 500px;
}

#login,#signup{
font-size: 30px;
line-height: 50px;
color: rgba(99, 88, 220, 1);
font-weight: bold;
}

.container{
border-radius: 8px;
box-shadow: 0px 0px 4px 0px rgba(0, 0, 0, 0.08);
background-color: #fff;
height: 400px;
width: 350px;
margin-left: 100px;
}
.container2{
border-radius: 8px;
box-shadow: 0px 0px 4px 0px rgba(0, 0, 0, 0.08);
background-color: #fff;
height: 400px;
width: 350px;
margin-left: 100px;
}
.input-group{
display: flex;
justify-content: center;
align-items: center;
padding: 10px;
}
input,button{
height: 30px;
width: 80%;
}
button{
background-color: rgba(99, 88, 220, 1);
color: white;
border: none;
}
#p1{
margin-left: 80px;
}

a{

text-decoration: none;
}
71 changes: 68 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ const express = require("express")
const bodyParser = require("body-parser")
const axios = require("axios")
const sgMail = require("@sendgrid/mail")
<<<<<<< HEAD
const ExpressError=require("./utils/ExpressError.js")
=======
const path=require("path")
const bcrypt=require("bcrypt")
const collection=require("./config.js")
>>>>>>> Add-login-and-signup-sahithi000

require("dotenv").config()

Expand All @@ -18,13 +24,72 @@ const verdict = []

app.use(express.static(__dirname+"/Public"));
app.use(bodyParser.urlencoded({extended:true}));


app.set('views', __dirname + '/views');
app.set("view engine","ejs");


app.use(express.json())// As data is stored in json format in mongoDb
app.get("/",(req,res)=>{
res.render("login")
})
app.get("/signup",(req,res)=>{
res.render("signup")
})
//user registration for loging in
app.post("/signup", async (req, res) => {
const data = {
email: req.body.email,
password: req.body.password,
};

try {
// we need to check whether user already exists to avoid duplicates
const existing=await collection.findOne({email:data.email});
if(existing){
return res.status(400).json({ message: "User already exists" });
}
else{
// we need hash password to keep it secure using bcrypt
const rounds=10;

const hashedPassword=await bcrypt.hash(data.password,rounds);
data.password=hashedPassword;
const userData = await collection.insertMany(data);
console.log(userData);
res.render('login');
}
} catch (error) {
if (error.errors && error.errors.email && error.errors.email.kind === 'user defined') {
return res.status(400).json({ message: error.errors.email.message });
} else {
console.error('Error:', error);
res.status(500).json({ message: 'Internal server error' });
}
}
});


app.post('/',async(req,res)=>{
try{
const exist=await collection.findOne({email:req.body.email});
if(!exist){
return res.status(400).json({ message: "User doesnot exist please signup" });
}
//check the password is corect or not
const passwordCheck=await bcrypt.compare(req.body.password,exist.password)
if(passwordCheck){
res.render('index');
}
else{
req.send("wrong password");
}
}
catch{
res.send("wrong credentials");
}
})


app.get("/index",(req,res)=>{
res.render("index")
})
app.get("/about",(req,res)=>{
Expand Down
30 changes: 30 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require("dotenv").config()
//MongoDB connection for login and signup
const mongoose=require("mongoose")
const connect=mongoose.connect(process.env.Mongo_URL)
connect.then(()=>{
console.log("connected to DB successfully")
})
.catch(()=>{
console.log("error connecting to DB")
})

// Custom email validator function
function validateEmail(email) {
// Regular expression for email validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
const loginSchema=new mongoose.Schema({
email:{
type:String,
required:true,
validate: [validateEmail, 'Please fill a valid email address']
},
password:{
type:String,
required:true
}
});
const collection=new mongoose.model("users",loginSchema)
module.exports=collection;
Loading

0 comments on commit e0aced3

Please sign in to comment.