Skip to content

Commit 7493db7

Browse files
authored
Merge pull request #56 from anandshukla15/feature/session-expiry
resolve bugs
2 parents 61f33d7 + ae0c561 commit 7493db7

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

backend/.env.example

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
MONGO_URI = mongodb://localhost:27017/PeerCall
2-
PORT = 3000
3-
JWT_SECRET = secret12peercall
4-
FRONTEND_URL = http://localhost:5173
1+
# MONGO_URI = mongodb://localhost:27017/PeerCall
2+
# PORT = 3000
3+
# JWT_SECRET = secret12peercall
4+
# FRONTEND_URL = http://localhost:5173
5+
6+
# GITHUB_CLIENT_ID = put yours
7+
# GITHUB_CLIENT_SECRET put yours
8+
# GOOGLE_CLIENT_ID = put yours
9+
# GOOGLE_CLIENT_SECRET = put yours
10+
# GOOGLE_CALLBACK_URL = http://localhost:3000/api/auth/google/callback
11+
# GITHUB_CALLBACK_URL = http://localhost:3000/api/auth/github/callback
512

6-
GITHUB_CLIENT_ID = put yours
7-
GITHUB_CLIENT_SECRET put yours
8-
GOOGLE_CLIENT_ID = put yours
9-
GOOGLE_CLIENT_SECRET = put yours
10-
GOOGLE_CALLBACK_URL = http://localhost:3000/api/auth/google/callback
11-
GITHUB_CALLBACK_URL = http://localhost:3000/api/auth/github/callback

backend/package-lock.json

Lines changed: 0 additions & 3 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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import bcrypt from "bcryptjs";
33
import jwt from "jsonwebtoken"; // <-- ADDED
44
import User, { type IUser } from "../models/userModel.js";
55
import {
6+
generateToken,
67
generateAccessToken, // <-- RENAMED/UPDATED
78
generateRefreshToken, // <-- ADDED
89
} from "../utils/generateToken.js";
910
import { userSchema, loginSchema } from "../utils/validateInputs.js";
1011
import dotenv from "dotenv";
11-
import jwt from "jsonwebtoken";
12+
1213
import { Session } from "../models/sessionModel.js";
1314

1415
dotenv.config();
@@ -70,7 +71,7 @@ export const registerUser = async (
7071
const typedUser = asTypedUser(newUser);
7172

7273
const token = generateToken(typedUser._id.toString());
73-
const decoded = jwt.decode(token) as { exp?: number } | null;
74+
const decoded = jwt.verify(token, process.env.JWT_SECRET!) as { exp?: number };
7475

7576
if (!decoded || !decoded.exp) {
7677
throw new Error("Invalid token format or missing expiration");

backend/src/routes/authRoutes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import express from "express";
2-
import { registerUser, loginUser, getUserProfile} from "../controllers/authController.js";
2+
import { registerUser, loginUser, getUserProfile, logoutUser,handleRefreshToken} from "../controllers/authController.js";
33
import passport from "passport";
44
import { Session } from "../models/sessionModel.js";
55
import {protect} from "../middleware/authMiddleware.js";

backend/src/utils/generateToken.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { SignOptions } from "jsonwebtoken";
33
import dotenv from 'dotenv';
44
dotenv.config();
55
const accessTokenSecret = process.env.JWT_ACCESS_SECRET;
6+
const refreshTokenSecret = process.env.JWT_REFRESH_SECRET;
67

78
export const generateToken = (userId: string) => {
89
const expiresIn = "7d";
@@ -12,6 +13,22 @@ export const generateToken = (userId: string) => {
1213
return token;
1314
};
1415

16+
const parseExpiration = (val: string | undefined, fallback: number | string): number | string => {
17+
if (!val) return fallback;
18+
const trimmed = val.trim();
19+
return /^\d+$/.test(trimmed) ? Number(trimmed) : trimmed;
20+
};
21+
22+
export const generateAccessToken = (id: string) => {
23+
if (!accessTokenSecret) throw new Error("JWT_ACCESS_SECRET is not defined");
24+
25+
const options = {
26+
expiresIn: parseExpiration(process.env.JWT_ACCESS_EXPIRATION, 900),
27+
} as SignOptions;
28+
29+
return jwt.sign({ id }, accessTokenSecret, options);
30+
};
31+
1532
export const generateRefreshToken = (id: string) => {
1633
if (!refreshTokenSecret) throw new Error("JWT_REFRESH_SECRET is not defined");
1734

0 commit comments

Comments
 (0)