From a479040d32cafde4f9a518dc21c55ab697a6ae87 Mon Sep 17 00:00:00 2001 From: Tamal Das Date: Sat, 19 Oct 2024 00:43:43 +0530 Subject: [PATCH] feat: auth flow changes & actions (#61) * fix: changes & tests for dev * fix: testing dev deployment 1 * fix: deployment test 2 * fix: dev deployments 3 * fix: updates to gh actions --- ...production-beta.yml => production-dev.yml} | 55 ++++++++++--------- index.js | 2 +- routes/user/Auth.js | 43 ++++++++------- 3 files changed, 51 insertions(+), 49 deletions(-) rename .github/workflows/{production-beta.yml => production-dev.yml} (76%) diff --git a/.github/workflows/production-beta.yml b/.github/workflows/production-dev.yml similarity index 76% rename from .github/workflows/production-beta.yml rename to .github/workflows/production-dev.yml index 80c9e3f..599d59f 100644 --- a/.github/workflows/production-beta.yml +++ b/.github/workflows/production-dev.yml @@ -1,27 +1,28 @@ -name: 📢 Deploy to Beta - -env: - VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} - VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID_BETA }} - -on: - workflow_dispatch: - push: - branches: - - beta - -jobs: - Deploy-to-Production: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Install Vercel CLI - run: npm install --global vercel@canary - - name: Pull Vercel Environment Information - run: - vercel pull --yes --environment=production --token=${{ - secrets.VERCEL_TOKEN }} - - name: Build Project Artifacts - run: vercel build --prod --token=${{ secrets.VERCEL_TOKEN }} - - name: Deploy Project Artifacts to Vercel - run: vercel deploy --prebuilt --prod --token=${{ secrets.VERCEL_TOKEN }} +name: 📢 Deploy to Dev + +env: + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID_DEV }} + +on: + workflow_dispatch: + push: + branches: + - dev + - main + +jobs: + Deploy-to-Production: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Install Vercel CLI + run: npm install --global vercel@canary + - name: Pull Vercel Environment Information + run: + vercel pull --yes --environment=development --token=${{ + secrets.VERCEL_TOKEN }} + - name: Build Project Artifacts + run: vercel build --prod --token=${{ secrets.VERCEL_TOKEN }} + - name: Deploy Project Artifacts to Vercel + run: vercel deploy --prebuilt --prod --token=${{ secrets.VERCEL_TOKEN }} diff --git a/index.js b/index.js index 454b83b..e7d421e 100644 --- a/index.js +++ b/index.js @@ -52,7 +52,7 @@ app.use(passport.session()); app.use(express.static(path.join(__dirname, "public"))); app.get("/", (req, res) => { - res.send("HELLO FROM HOME"); + res.send("HELLO FROM API"); }); app.use("/user", require("./routes/user/User")); diff --git a/routes/user/Auth.js b/routes/user/Auth.js index 9061d53..b026963 100644 --- a/routes/user/Auth.js +++ b/routes/user/Auth.js @@ -24,6 +24,11 @@ const frontendCookie = { }; // Route 1 - User Signup +// Check if the user already exists, if not create a new user +// Make a random username for the user +// Hash the password and store it in the database +// Create a JWT token and send it in the cookie + router.post("/signup", async (req, res) => { try { const { email, ...data } = req.body; @@ -36,7 +41,15 @@ router.post("/signup", async (req, res) => { } const hashedPassword = await bcrypt.hash(data.password, 10); - const userName = email.split("@")[0]; + var userName = email.split("@")[0] + Math.floor(Math.random()); + + while ( + await User.findOne({ + userName, + }) + ) { + userName = email.split("@")[0] + Math.floor(Math.random()); + } const newUser = new User({ ...data, @@ -52,16 +65,10 @@ router.post("/signup", async (req, res) => { const { password, _id, ...userWithoutSensitiveInfo } = newUser.toObject(); const user = { ...userWithoutSensitiveInfo }; - res - .status(STATUSCODE.CREATED) - .cookie("Token", token, defaultCookie) - .cookie("userName", userName, frontendCookie) - .cookie("isLoggedIn", true, frontendCookie) - .cookie("userType", data?.userType, frontendCookie) - .json({ - message: STATUSMESSAGE.SIGNUP_SUCCESS, - user, - }); + res.status(STATUSCODE.CREATED).cookie("Token", token, defaultCookie).json({ + message: STATUSMESSAGE.SIGNUP_SUCCESS, + user, + }); } catch (err) { res.status(STATUSCODE.INTERNAL_SERVER_ERROR).json({ message: err }); } @@ -96,16 +103,10 @@ router.post("/signin", async (req, res) => { const payload = { User: { id: existingUser.email } }; const token = jwt.sign(payload, process.env.JWT_SECRET); - res - .status(STATUSCODE.CREATED) - .cookie("Token", token, defaultCookie) - .cookie("userName", existingUser.userName, frontendCookie) - .cookie("isLoggedIn", true, frontendCookie) - .cookie("userType", existingUser.userType, frontendCookie) - .json({ - message: STATUSMESSAGE.LOGIN_SUCCESS, - user, - }); + res.status(STATUSCODE.OK).cookie("Token", token, frontendCookie).json({ + message: STATUSMESSAGE.LOGIN_SUCCESS, + user, + }); } catch (err) { res.status(STATUSCODE.INTERNAL_SERVER_ERROR).json({ message: err }); }