-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
680d4b4
commit a22979e
Showing
3 changed files
with
13 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "backend", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"description": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,137 +0,0 @@ | ||
const port = 4000; | ||
const express = require("express"); | ||
const app = express(); | ||
const mongoose = require("mongoose"); | ||
const jwt = require("jsonwebtoken"); | ||
const cors = require("cors"); | ||
const passport = require("passport"); | ||
const GoogleStrategy = require("passport-google-oauth20").Strategy; | ||
const session = require("express-session"); | ||
const bcrypt = require("bcrypt"); | ||
|
||
app.use(express.json()); | ||
app.use(cors()); | ||
app.use(session({ secret: "secret_ecom", resave: false, saveUninitialized: true })); | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
|
||
// Database connection with MongoDB | ||
mongoose.connect( | ||
"Mongodb Uri" | ||
).then(() => console.log("DATABASE CONNECTED")); | ||
|
||
// User Schema | ||
const Users = mongoose.model("Users", { | ||
name: String, | ||
email: { | ||
type: String, | ||
unique: true, | ||
}, | ||
password: String, | ||
cartData: Object, | ||
date: { | ||
type: Date, | ||
default: Date.now, | ||
}, | ||
}); | ||
|
||
// Passport configuration | ||
passport.use(new GoogleStrategy({ | ||
clientID: "CLIENT_ID", | ||
clientSecret: "CLIENT_SECRET_ID", | ||
callbackURL: "http://localhost:4000/auth/google/callback" | ||
}, | ||
async (accessToken, refreshToken, profile, done) => { | ||
try { | ||
let user = await Users.findOne({ email: profile.emails[0].value }); | ||
if (!user) { | ||
user = new Users({ | ||
name: profile.displayName, | ||
email: profile.emails[0].value, | ||
password: "", // No password for Google-authenticated users | ||
}); | ||
await user.save(); | ||
} | ||
done(null, user); | ||
} catch (err) { | ||
done(err, false); | ||
} | ||
} | ||
)); | ||
|
||
passport.serializeUser((user, done) => { | ||
done(null, user.id); | ||
}); | ||
|
||
passport.deserializeUser(async (id, done) => { | ||
try { | ||
const user = await Users.findById(id); | ||
done(null, user); | ||
} catch (err) { | ||
done(err, false); | ||
} | ||
}); | ||
|
||
// API Endpoints | ||
app.get("/", (req, res) => { | ||
res.send("Express App is Running"); | ||
}); | ||
|
||
// Sign Up Endpoint | ||
app.post("/signup", async (req, res) => { | ||
let check = await Users.findOne({ email: req.body.email }); | ||
if (check) { | ||
return res.status(400).json({ | ||
success: false, | ||
errors: "User already exists with this email id", | ||
}); | ||
} | ||
|
||
const hashedPassword = await bcrypt.hash(req.body.password, 10); | ||
|
||
const user = new Users({ | ||
name: req.body.name, | ||
email: req.body.email, | ||
password: hashedPassword, | ||
}); | ||
await user.save(); | ||
const data = { user: { id: user.id } }; | ||
const token = jwt.sign(data, "secret_ecom", { expiresIn: '1h' }); | ||
res.json({ success: true, token }); | ||
}); | ||
|
||
// Login Endpoint | ||
app.post("/login", async (req, res) => { | ||
let user = await Users.findOne({ email: req.body.email }); | ||
if (user) { | ||
const passCompare = await bcrypt.compare(req.body.password, user.password); | ||
if (passCompare) { | ||
const data = { user: { id: user.id } }; | ||
const token = jwt.sign(data, "secret_ecom", { expiresIn: '1h' }); | ||
res.json({ success: true, token }); | ||
} else { | ||
res.status(401).json({ success: false, error: "Wrong Password" }); | ||
} | ||
} else { | ||
res.status(401).json({ success: false, error: "Wrong Email Id" }); | ||
} | ||
}); | ||
|
||
// Google Authentication Endpoints | ||
app.get("/auth/google", passport.authenticate("google", { scope: ["profile", "email"] })); | ||
|
||
app.get("/auth/google/callback", | ||
passport.authenticate("google", { failureRedirect: "/" }), | ||
(req, res) => { | ||
// Successful authentication, redirect home. | ||
res.redirect("http://localhost:3000/"); | ||
} | ||
); | ||
|
||
app.listen(port, (error) => { | ||
if (!error) { | ||
console.log("Server Running on Port:" + port); | ||
} else { | ||
console.log("Error" + error); | ||
} | ||
}); | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters