Skip to content

Commit 9e2881e

Browse files
authored
Merge pull request #48 from ekanshgupta2046/added-authentication-and-join-room-routes
Added authentication and join room routes
2 parents fdaf2e8 + 6338c11 commit 9e2881e

File tree

11 files changed

+502
-346
lines changed

11 files changed

+502
-346
lines changed

backend/package-lock.json

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/src/controllers/authController.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { Request, Response, NextFunction } from "express";
22
import bcrypt from "bcryptjs";
3-
import User, { type IUser } from "../models/userModel.js";
3+
import User, { type IUser } from "../models/userModel.js";
44
import { generateToken } from "../utils/generateToken.js";
55
import { userSchema, loginSchema } from "../utils/validateInputs.js";
66
import dotenv from "dotenv";
77

88
dotenv.config();
99
const asTypedUser = (user: any): IUser & { _id: string } => user as IUser & { _id: string };
1010

11-
//signup controller
11+
// ✅ SIGNUP CONTROLLER
1212
export const registerUser = async (req: Request, res: Response, next: NextFunction) => {
1313
try {
1414
const parseResult = userSchema.safeParse(req.body);
@@ -19,10 +19,15 @@ export const registerUser = async (req: Request, res: Response, next: NextFuncti
1919
});
2020
}
2121

22-
const { name, email, password } = parseResult.data;
22+
const { email, password } = parseResult.data;
23+
24+
// ✅ Auto-derive name from email
25+
const name = email.split("@")[0];
26+
2327
const existingUser = await User.findOne({ email });
24-
if (existingUser)
28+
if (existingUser) {
2529
return res.status(400).json({ success: false, message: "Email already registered" });
30+
}
2631

2732
const hashedPassword = await bcrypt.hash(password, 10);
2833
const newUser = await User.create({ name, email, password: hashedPassword });
@@ -38,7 +43,7 @@ export const registerUser = async (req: Request, res: Response, next: NextFuncti
3843
}
3944
};
4045

41-
//sign in controller
46+
// ✅ LOGIN CONTROLLER (same as before)
4247
export const loginUser = async (req: Request, res: Response, next: NextFunction) => {
4348
try {
4449
const parseResult = loginSchema.safeParse(req.body);
@@ -53,6 +58,7 @@ export const loginUser = async (req: Request, res: Response, next: NextFunction)
5358
const foundUser = await User.findOne({ email });
5459
if (!foundUser)
5560
return res.status(400).json({ success: false, message: "Invalid credentials" });
61+
5662
if (!foundUser.password || foundUser.password === "") {
5763
return res.status(400).json({
5864
success: false,
@@ -76,22 +82,27 @@ export const loginUser = async (req: Request, res: Response, next: NextFunction)
7682
}
7783
};
7884

79-
//OAuth callback handler
80-
export const oauthCallback = (req: Request & { user?: any }, res: Response) => {
85+
// ✅ GET PROFILE CONTROLLER (unchanged)
86+
export const getUserProfile = async (req: Request, res: Response) => {
8187
try {
82-
const frontendUrl = process.env.FRONTEND_URL || "http://localhost:5173";
83-
const user = req.user as IUser & { _id: string } | undefined;
88+
const user = await User.findById(req.userId).select("-password");
8489

8590
if (!user) {
86-
return res.redirect(`${frontendUrl}/signin?error=oauth_failed`);
91+
return res.status(404).json({
92+
success: false,
93+
message: "User not found",
94+
});
8795
}
8896

89-
const token = generateToken(user._id.toString());
90-
const redirectUrl = `${frontendUrl}/oauth-success#token=${token}`;
91-
92-
return res.redirect(redirectUrl);
93-
} catch (err) {
94-
const frontendUrl = process.env.FRONTEND_URL || "http://localhost:5173";
95-
return res.redirect(`${frontendUrl}/signin?error=server_error`);
97+
res.status(200).json({
98+
success: true,
99+
user,
100+
});
101+
} catch (error) {
102+
console.error(error);
103+
res.status(500).json({
104+
success: false,
105+
message: "Server error",
106+
});
96107
}
97108
};

backend/src/routes/authRoutes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import express from "express";
2-
import { registerUser, loginUser, oauthCallback } from "../controllers/authController.js";
2+
import { registerUser, loginUser, getUserProfile } from "../controllers/authController.js";
33
import passport from "passport";
4+
import { protect } from "../middleware/authMiddleware.js";
45

56
const router = express.Router();
67

78
router.post("/signup", registerUser);
89
router.post("/signin", loginUser);
10+
router.get("/me", protect, getUserProfile);
911

1012
// Google OAuth
1113
router.get(

0 commit comments

Comments
 (0)