Skip to content

Commit

Permalink
fix: validations refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
tamalCodes committed Mar 11, 2024
1 parent bbaef7c commit b24d522
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 49 deletions.
2 changes: 1 addition & 1 deletion config/passport-googleAuth-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ passport.use(
user = await User.create({
name: profile.displayName,
email: profile.emails[0].value,
username: profile.emails[0].value.split("@")[0],
userName: profile.emails[0].value.split("@")[0],
password: crypto.randomBytes(20).toString("hex"),
});

Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ const passport = require("passport");
require("./config/passport-googleAuth-strategy");

const path = require("path");
const bodyParser = require("body-parser");

let port = process.env.PORT || 5000;
const app = express();
dotenv.config();
connectToMongo();

app.use(bodyParser.json({ limit: "10mb" }));

app.use(
cors({
origin: (origin, callback) => {
Expand Down
54 changes: 46 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@geoapify/geocoder-autocomplete": "^1.5.1",
"@geoapify/react-geocoder-autocomplete": "^1.5.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.2",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"crypto": "^1.0.1",
Expand Down
8 changes: 4 additions & 4 deletions routes/club/Club.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const router = express.Router();

router.get("/", async (req, res) => {
try {
const { username } = req.query;
const { userName } = req.query;

if (username) {
const clubdetails = await User.findOne({ username });
if (userName) {
const clubdetails = await User.findOne({ userName });
if (!clubdetails)
return res
.status(STATUSCODE.NOT_FOUND)
Expand All @@ -17,7 +17,7 @@ router.get("/", async (req, res) => {
}

const clubs = await User.find({
usertype: "club",
userType: "club",
});

res.json(clubs);
Expand Down
2 changes: 1 addition & 1 deletion routes/display/Display.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ router.get("/users", async (req, res) => {
// * Route 2 - Show all available Clubs in the DB
router.get("/clubs", async (req, res) => {
try {
const allClubs = await User.find({ usertype: "club" });
const allClubs = await User.find({ userType: "club" });
res.json(allClubs);
} catch (error) {
res
Expand Down
7 changes: 1 addition & 6 deletions routes/events/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,12 @@ router.post("/create", async (req, res) => {
!data.description ||
!data.coverImage ||
!data.mode ||
!data.startDate ||
!data.endDate ||
!data.startTime ||
!data.endTime ||
!data.city ||
!data.state ||
!data.country ||
!data.address ||
!data.mapIframe
) {
console.log(data);
return res
.status(STATUSCODE.BAD_REQUEST)
.json({ message: "Missing Required Fields" });
Expand All @@ -71,7 +66,7 @@ router.post("/create", async (req, res) => {
...data,
uid,
hostName: user.name,
hostUsername: user.username,
hostUsername: user.userName,
createdAt: setTime(),
updatedAt: setTime(),
});
Expand Down
32 changes: 16 additions & 16 deletions routes/user/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ router.post("/signup", async (req, res) => {
}

const hashedPassword = await bcrypt.hash(data.password, 10);
const username = email.split("@")[0];
const userName = email.split("@")[0];

const newUser = new User({
...data,
username,
userName,
email,
password: hashedPassword,
});
Expand All @@ -55,9 +55,9 @@ router.post("/signup", async (req, res) => {
res
.status(STATUSCODE.CREATED)
.cookie("Token", token, defaultCookie)
.cookie("username", username, frontendCookie)
.cookie("userName", userName, frontendCookie)
.cookie("isLoggedIn", true, frontendCookie)
.cookie("usertype", data?.usertype, frontendCookie)
.cookie("userType", data?.userType, frontendCookie)
.json({
message: STATUSMESSAGE.SIGNUP_SUCCESS,
user,
Expand Down Expand Up @@ -99,9 +99,9 @@ router.post("/signin", async (req, res) => {
res
.status(STATUSCODE.CREATED)
.cookie("Token", token, defaultCookie)
.cookie("username", existingUser.username, frontendCookie)
.cookie("userName", existingUser.userName, frontendCookie)
.cookie("isLoggedIn", true, frontendCookie)
.cookie("usertype", existingUser.usertype, frontendCookie)
.cookie("userType", existingUser.userType, frontendCookie)
.json({
message: STATUSMESSAGE.LOGIN_SUCCESS,
user,
Expand Down Expand Up @@ -145,8 +145,8 @@ router.post("/update", async (req, res) => {

// Updated User
const UserData = {
firstname: existingUser.firstname,
lastname: existingUser.lastname,
firstName: existingUser.firstName,
lastName: existingUser.lastName,
email: email,
password: newHashedPassword,
address: existingUser.address,
Expand All @@ -171,7 +171,7 @@ router.get("/google", (req, res) => {
redirect_uri: process.env.CALLBACK_URL,
scope: "profile email ",
client_id: process.env.CLIENT_ID,
state: req.query.usertype,
state: req.query.userType,
});

const redirectURL = `${googleAuthURL}?${params}`;
Expand All @@ -186,17 +186,17 @@ router.get(
failureRedirect: "auth/login/failed",
}),
async (req, res) => {
const usertype = req.query.state;
const userType = req.query.state;
if (req.isAuthenticated()) {
const user = req.user;
try {
const existingUser = await User.findOne({ email: user.email });

if (!existingUser) {
// This is a new account, update usertype
// This is a new account, update userType
await User.create({
email: user.email,
usertype: usertype,
userType: userType,
});
}
res
Expand Down Expand Up @@ -246,9 +246,9 @@ router.get("/login/success", (req, res) => {
domain: process.env.ORIGIN_DOMAIN,
})
.cookie("Token", token, defaultCookie)
.cookie("username", req.user.username, frontendCookie)
.cookie("userName", req.user.userName, frontendCookie)
.cookie("isLoggedIn", true, frontendCookie)
.cookie("usertype", "user", frontendCookie)
.cookie("userType", "user", frontendCookie)
.json({
message: STATUSMESSAGE.LOGIN_SUCCESS,
user,
Expand All @@ -270,7 +270,7 @@ router.get("/logout", (req, res) => {
sameSite: "none",
domain: process.env.ORIGIN_DOMAIN,
})
.cookie("username", false, {
.cookie("userName", false, {
expires: new Date(0),
httpOnly: false,
secure: true,
Expand All @@ -284,7 +284,7 @@ router.get("/logout", (req, res) => {
sameSite: "none",
domain: process.env.ORIGIN_DOMAIN,
})
.cookie("usertype", false, {
.cookie("userType", false, {
expires: new Date(0),
httpOnly: false,
secure: true,
Expand Down
12 changes: 6 additions & 6 deletions routes/user/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const jwt = require("jsonwebtoken");

router.get("/", async (req, res) => {
try {
const { username } = req.query;
const { userName } = req.query;

if (username) {
const userdetails = await User.findOne({ username });
if (userName) {
const userdetails = await User.findOne({ userName });

if (!userdetails)
return res
Expand All @@ -21,7 +21,7 @@ router.get("/", async (req, res) => {
}

const users = await User.find({
usertype: "individual",
userType: "individual",
});

res.json(users);
Expand Down Expand Up @@ -96,8 +96,8 @@ router.post("/report", async (req, res) => {
const data = req.body;

const ReportData = ReportProblem({
firstname: data.firstname,
lastname: data.lastname,
firstName: data.firstName,
lastName: data.lastName,
email: data.email,
reportmessage: data.reportmessage,
});
Expand Down
2 changes: 1 addition & 1 deletion schema/club/ClubSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const ClubsSchema = mongoose.Schema({
type: String,
required: true,
},
username: {
userName: {
type: String,
required: true,
},
Expand Down
4 changes: 2 additions & 2 deletions schema/user/ReportProblemSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const mongoose = require("mongoose");

const ReportProblemSchema = mongoose.Schema(
{
firstname: {
firstName: {
type: String,
required: true,
},
lastname: {
lastName: {
type: String,
required: true,
},
Expand Down
8 changes: 4 additions & 4 deletions schema/user/UserSchema.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const mongoose = require("mongoose");

const UserSchema = mongoose.Schema({
usertype: { type: String },
username: {
userType: { type: String },
userName: {
type: String,
required: true,
},
name: { type: String },
firstname: { type: String },
lastname: { type: String },
firstName: { type: String },
lastName: { type: String },
email: {
type: String,
required: true,
Expand Down

0 comments on commit b24d522

Please sign in to comment.