Skip to content

Commit

Permalink
fix: new routes
Browse files Browse the repository at this point in the history
  • Loading branch information
tamalCodes committed Sep 9, 2023
1 parent 8d05551 commit a8bf423
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 174 deletions.
11 changes: 3 additions & 8 deletions config/passport-googleAuth-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,15 @@ passport.use(
try {
let user = await User.findOne({
email: profile.emails[0].value,
}).exec();
});

if (user) {
return done(null, user);
} else {
let firstname = profile.displayName.split(" ")[0];
let lastname = profile.displayName.split(" ")[1];

if (!lastname) lastname = " ";

user = await User.create({
firstname: firstname,
lastname: lastname,
name: profile.displayName,
email: profile.emails[0].value,
slug: profile.emails[0].value.split("@")[0],
password: crypto.randomBytes(20).toString("hex"),
});

Expand Down
78 changes: 5 additions & 73 deletions routes/club/Club.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,24 @@
//* All routes related to club's LOGIN AND REGISTER

const express = require("express");
const Club = require("../../schema/club/ClubSchema");
const User = require("../../schema/user/UserSchema");
const router = express.Router();
const bcrypt = require("bcryptjs");
var jwt = require("jsonwebtoken");

router.get("/", async (req, res) => {
try {
if (req.query.slug) {
const clubdetails = await Club.findOne({ slug: req.query.slug });
console.log(clubdetails);
const clubdetails = await User.findOne({ slug: req.query.slug });
return res.status(200).json(clubdetails);
}
const allClubs = await Club.find({});
res.json(allClubs);
} catch (error) {
res.status(500).json({ message: "Internal Server Error" });
}
});

//* Route 1 - Club Registration

router.post("/register", async (req, res) => {
try {
const data = req.body;

const existingUser = await Club.findOne({ email: data.email });
if (existingUser) {
return res.status(409).json({ message: "Account already exists" });
}

const hashpassword = await bcrypt.hash(data.password, 10);

const ClubData = Club({
name: data.name,
email: data.email,
password: hashpassword,
tagLine: data.tagLine,
description: data.description,
city: data.city,
state: data.state,
address: data.address,
country: data.country,
pincode: data.pincode,
slug: data.name.toLowerCase().split(" ").join("-"),
const clubs = await User.find({
usertype: "club",
});

await ClubData.save();
res.status(201).json({ message: "Signup successful, please Login" });
res.json(clubs);
} catch (error) {
console.log(error);
res.status(500).json({ message: "Internal Server Error" });
}
});

//* ---------------------------------------------------------------------------------------------------------------------------------------------
//* Route 2 - Club Login

router.post("/login", async (req, res) => {
try {
const { email, password } = req.body;

const existingUser = await Club.findOne({ email });
if (!existingUser) {
return res.status(404).json({ message: "User not found" });
}

const validPassword = await bcrypt.compare(password, existingUser.password);
if (!validPassword) {
return res.status(401).json({ message: "Invalid Credentials" });
}

const payload = { Club: { id: existingUser.email } };
const token = jwt.sign(payload, process.env.JWT_SECRET);

res
.status(201)
.cookie("Token", token, {
sameSite: "none",
httpOnly: true,
expires: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000),
secure: true,
})
.json({ token, isuser: false, message: "Logged you in !" });
} catch (e) {
res.status(500).json({ success: false });
}
});

module.exports = router;
79 changes: 39 additions & 40 deletions routes/user/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,7 @@ router.get("/google", (req, res) => {

const redirectURL = `${googleAuthURL}?${params}`;

return res
.status(201)
.cookie("isuser", req?.query?.isuser, {
expires: new Date(new Date().getTime() + 5 * 60 * 1000),
httpOnly: false,
secure: true,
sameSite: "none",
domain: process.env.ORIGIN_DOMAIN,
})
.json({ url: redirectURL });
return res.status(201).json({ url: redirectURL });
});

//* Route 6 - google authentication callback
Expand Down Expand Up @@ -56,19 +47,18 @@ router.get("/login/failed", (req, res) => {

//* Route 8 - google authentication success
router.get("/login/success", (req, res) => {
console.log(req.user);
if (req.user) {
const data = { User: { id: req.user.email } };
const token = jwt.sign(data, process.env.JWT_SECRET);
console.log("Token is:", token);

res.cookie("Token", token, {
sameSite: "none",
httpOnly: true,
expires: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000),
secure: true,
domain: process.env.ORIGIN_DOMAIN,
});
if (!req.user.usertype) {
res.cookie("emptyProfile", true, {
httpOnly: false,
secure: true,
sameSite: "none",
domain: process.env.ORIGIN_DOMAIN,
});
}

res
.status(201)
Expand All @@ -79,8 +69,27 @@ router.get("/login/success", (req, res) => {
sameSite: "none",
domain: process.env.ORIGIN_DOMAIN,
})
.cookie("Token", token, {
sameSite: "none",
httpOnly: true,
expires: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000),
secure: true,
domain: process.env.ORIGIN_DOMAIN,
})
.cookie("username", req.user.slug, {
httpOnly: false,
secure: true,
sameSite: "none",
domain: process.env.ORIGIN_DOMAIN,
})
.cookie("isLoggedIn", true, {
expires: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000),
httpOnly: false,
secure: true,
sameSite: "none",
domain: process.env.ORIGIN_DOMAIN,
})
.json({
isuser: true,
message: "Logged you in !",
});
} else {
Expand All @@ -89,32 +98,22 @@ router.get("/login/success", (req, res) => {
});

//* Route 9 - google authentication logout
// router.post("/logout", function (req, res, next) {
// req.logout(function (err) {
// if (err) {
// return next(err);
// }

// console.log("Hello from Logout API");
// res.status(201).json({ message: "Logged you out !" });
// });
// });

router.get("/logout", (req, res) => {
req.logout(function (err) {
if (err) {
return res.status(500).json({ message: "Error while logging out." });
}

res.cookie("Token", "", {
expires: new Date(0),
sameSite: "strict",
httpOnly: true,
domain: process.env.ORIGIN_DOMAIN,
secure: true,
});

res.status(201).json({ message: "Logged you out !" });
res
.cookie("Token", "", {
expires: new Date(0),
sameSite: "strict",
httpOnly: true,
domain: process.env.ORIGIN_DOMAIN,
secure: true,
})
.status(201)
.json({ message: "Logged you out !" });
});
});

Expand Down
28 changes: 8 additions & 20 deletions routes/user/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ router.post("/register", async (req, res) => {
});
await newUser.save();
res.status(201).json({ message: "Signup successful, please Login" });
} catch (e) {
res.status(500).json({ message: "Internal Server Error" });
} catch (err) {
res.status(500).json({ message: err });
}
});

Expand Down Expand Up @@ -102,28 +102,16 @@ router.post("/login", async (req, res) => {
expires: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000),
secure: true,
})
.json({ token, isuser: true, message: "Logged you in !" });
} catch (err) {
res.status(500).json({ message: "Internal Server Error" });
}
});

router.post("/generate-token", async (req, res) => {
try {
const payload = { User: { id: req.body.email } };
const token = jwt.sign(payload, process.env.JWT_SECRET);
res
.status(201)
.cookie("Token", token, {
sameSite: "strict",
httpOnly: true,
path: "/",
.cookie("isLoggedIn", true, {
expires: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000),
httpOnly: false,
secure: true,
sameSite: "none",
domain: process.env.ORIGIN_DOMAIN,
})
.json({ token, isuser: true, message: "Logged you in !" });
.json({ token, message: "Logged you in !" });
} catch (err) {
res.status(500).json({ message: "Internal Server Error" });
res.status(500).json({ message: err });
}
});

Expand Down
44 changes: 11 additions & 33 deletions schema/user/UserSchema.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
const mongoose = require("mongoose");

const UserSchema = mongoose.Schema({
usertype: {
type: String,
},
usertype: { type: String },
slug: {
type: String,
required: true,
},
name: {
type: String,
required: true,
},
firstname: {
type: String,
},
lastname: {
type: String,
},
name: { type: String },
firstname: { type: String },
lastname: { type: String },
email: {
type: String,
required: true,
Expand All @@ -27,26 +18,13 @@ const UserSchema = mongoose.Schema({
type: String,
required: true,
},
city: {
type: String,
required: true,
},
state: {
type: String,
required: true,
},
address: {
type: String,
required: false,
},
country: {
type: String,
required: true,
},
pincode: {
type: String,
required: false,
},
tagLine: { type: String },
description: { type: String },
city: { type: String },
state: { type: String },
address: { type: String },
country: { type: String },
pincode: { type: String },
cart: [{ id: { type: String } }],
});

Expand Down

0 comments on commit a8bf423

Please sign in to comment.