Skip to content

Commit

Permalink
feat: new auth flow (#71)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tamalCodes authored Dec 6, 2024
1 parent a479040 commit 2126904
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ app.use(cookieParser());
app.use(
session({
name: "ssid",

secret: process.env.SECRET_KEY,
saveUninitialized: false,
resave: false,
Expand Down
17 changes: 11 additions & 6 deletions routes/club/Club.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 12 additions & 10 deletions routes/user/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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,
});
Expand Down
22 changes: 14 additions & 8 deletions schema/user/UserSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }],
});

Expand Down

0 comments on commit 2126904

Please sign in to comment.