Skip to content

Commit

Permalink
feat: auth flow changes & actions (#61)
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
  • Loading branch information
tamalCodes authored Oct 18, 2024
1 parent 0107437 commit a479040
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -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 }}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
43 changes: 22 additions & 21 deletions routes/user/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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 });
}
Expand Down Expand Up @@ -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 });
}
Expand Down

0 comments on commit a479040

Please sign in to comment.