From 212690415591cd80731e7f34133e8cae195d5661 Mon Sep 17 00:00:00 2001 From: Tamal Das Date: Fri, 6 Dec 2024 17:37:09 +0530 Subject: [PATCH] feat: new auth flow (#71) * fix: changes & tests for dev * fix: testing dev deployment 1 * fix: deployment test 2 * fix: dev deployments 3 * fix: updates to gh actions * fix: profile update API --- index.js | 1 - routes/club/Club.js | 17 +++++++++++------ routes/user/Auth.js | 22 ++++++++++++---------- schema/user/UserSchema.js | 22 ++++++++++++++-------- 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/index.js b/index.js index e7d421e..4b123d4 100644 --- a/index.js +++ b/index.js @@ -37,7 +37,6 @@ app.use(cookieParser()); app.use( session({ name: "ssid", - secret: process.env.SECRET_KEY, saveUninitialized: false, resave: false, diff --git a/routes/club/Club.js b/routes/club/Club.js index 07e03d4..6ce171d 100644 --- a/routes/club/Club.js +++ b/routes/club/Club.js @@ -11,19 +11,24 @@ router.get("/", async (req, res) => { const { userName } = req.query; if (userName) { - const clubdetails = await User.findOne({ userName }); - if (!clubdetails) + const clubdetails = await User.findOne({ userName }).select( + "-password -__v -_id", + ); + + if (!clubdetails) { return res .status(STATUSCODE.NOT_FOUND) .json({ message: STATUSMESSAGE.NOT_FOUND }); + } + return res.status(STATUSCODE.OK).json(clubdetails); } - const clubs = await User.find({ - userType: "club", - }); + const clubs = await User.find({ userType: "club" }).select( + "-password -__v -_id", + ); - res.json(clubs); + res.status(STATUSCODE.OK).json(clubs); } catch (error) { res .status(STATUSCODE.INTERNAL_SERVER_ERROR) diff --git a/routes/user/Auth.js b/routes/user/Auth.js index b026963..0bc066b 100644 --- a/routes/user/Auth.js +++ b/routes/user/Auth.js @@ -29,6 +29,16 @@ const frontendCookie = { // Hash the password and store it in the database // Create a JWT token and send it in the cookie +async function generateUniqueUsername(email) { + let userName = email.split("@")[0]; + + while (await User.findOne({ userName })) { + userName = email.split("@")[0] + Math.floor(Math.random() * 10000); + } + + return userName; +} + router.post("/signup", async (req, res) => { try { const { email, ...data } = req.body; @@ -41,15 +51,7 @@ router.post("/signup", async (req, res) => { } const hashedPassword = await bcrypt.hash(data.password, 10); - var userName = email.split("@")[0] + Math.floor(Math.random()); - - while ( - await User.findOne({ - userName, - }) - ) { - userName = email.split("@")[0] + Math.floor(Math.random()); - } + const userName = await generateUniqueUsername(email); const newUser = new User({ ...data, @@ -65,7 +67,7 @@ router.post("/signup", async (req, res) => { const { password, _id, ...userWithoutSensitiveInfo } = newUser.toObject(); const user = { ...userWithoutSensitiveInfo }; - res.status(STATUSCODE.CREATED).cookie("Token", token, defaultCookie).json({ + res.status(STATUSCODE.CREATED).cookie("Token", token, frontendCookie).json({ message: STATUSMESSAGE.SIGNUP_SUCCESS, user, }); diff --git a/schema/user/UserSchema.js b/schema/user/UserSchema.js index 23f3e55..23836db 100644 --- a/schema/user/UserSchema.js +++ b/schema/user/UserSchema.js @@ -7,24 +7,30 @@ const UserSchema = mongoose.Schema({ required: true, }, name: { type: String }, - firstName: { type: String }, - lastName: { type: String }, email: { type: String, required: true, unique: true, }, + phone: { type: String }, + profilePicture: { type: String }, + bannerPicture: { type: String }, password: { type: String, required: true, }, - tagLine: { type: String }, description: { type: String }, - city: { type: String }, - state: { type: String }, - address: { type: String }, - country: { type: String }, - pincode: { type: String }, + address: { + line1: { type: String }, + line2: { type: String }, + city: { type: String }, + state: { type: String }, + country: { type: String }, + pincode: { type: String }, + }, + config: { + hasCompletedProfile: { type: Boolean, default: false }, + }, cart: [{ id: { type: String } }], });