Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auth flow changes & actions #61

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading