|
8 | 8 | } from "../utils/generateToken.js"; |
9 | 9 | import { userSchema, loginSchema } from "../utils/validateInputs.js"; |
10 | 10 | import dotenv from "dotenv"; |
| 11 | +import jwt from "jsonwebtoken"; |
| 12 | +import { Session } from "../models/sessionModel.js"; |
11 | 13 |
|
12 | 14 | dotenv.config(); |
13 | 15 | const asTypedUser = (user: any): IUser & { _id: string } => |
@@ -67,8 +69,25 @@ export const registerUser = async ( |
67 | 69 | const newUser = await User.create({ name, email, password: hashedPassword }); |
68 | 70 | const typedUser = asTypedUser(newUser); |
69 | 71 |
|
70 | | - // Generate and send tokens |
71 | | - sendTokens(res, typedUser); |
| 72 | + const token = generateToken(typedUser._id.toString()); |
| 73 | +const decoded = jwt.decode(token) as { exp?: number } | null; |
| 74 | + |
| 75 | +if (!decoded || !decoded.exp) { |
| 76 | + throw new Error("Invalid token format or missing expiration"); |
| 77 | +} |
| 78 | + |
| 79 | +const expiresAt = new Date(decoded.exp * 1000); |
| 80 | +await Session.create({ |
| 81 | + userId: typedUser._id, |
| 82 | + token, |
| 83 | + expiresAt, |
| 84 | +}); |
| 85 | + |
| 86 | + res.status(201).json({ |
| 87 | + success: true, |
| 88 | + message: "User registered successfully", |
| 89 | + token, |
| 90 | + }); |
72 | 91 | } catch (err) { |
73 | 92 | next(err); |
74 | 93 | } |
@@ -112,59 +131,26 @@ export const loginUser = async ( |
112 | 131 |
|
113 | 132 | const typedUser = asTypedUser(foundUser); |
114 | 133 |
|
115 | | - // Generate and send tokens |
116 | | - await typedUser.save(); // Save any potential changes (like refresh tokens) |
117 | | - sendTokens(res, typedUser); |
118 | | - } catch (err) { |
119 | | - next(err); |
120 | | - } |
121 | | -}; |
| 134 | + const token = generateToken(typedUser._id.toString()); |
| 135 | +const decoded = jwt.verify(token, process.env.JWT_SECRET!) as { exp?: number }; |
122 | 136 |
|
123 | | -// ✅ OAUTH CALLBACK CONTROLLER (New) |
124 | | -export const oauthCallback = async ( |
125 | | - req: Request, |
126 | | - res: Response, |
127 | | - next: NextFunction |
128 | | -) => { |
129 | | - try { |
130 | | - if (!req.user) { |
131 | | - return res.status(401).json({ success: false, message: "Authentication failed" }); |
132 | | - } |
| 137 | +if (!decoded.exp) { |
| 138 | + throw new Error("Token missing expiration claim"); |
| 139 | +} |
133 | 140 |
|
134 | | - const typedUser = asTypedUser(req.user); |
| 141 | +const expiresAt = new Date(decoded.exp * 1000); |
135 | 142 |
|
136 | | - // We get the user profile from passport's `done` function |
137 | | - // (which you'd have in src/utils/passport.ts) |
138 | | - // Now we just generate and send tokens |
| 143 | +await Session.create({ |
| 144 | + userId: typedUser._id, |
| 145 | + token, |
| 146 | + expiresAt, |
| 147 | +}); |
139 | 148 |
|
140 | | - // Find the user in DB (req.user is from passport) |
141 | | - const foundUser = await User.findById(typedUser._id); |
142 | | - if (!foundUser) { |
143 | | - return res.status(404).json({ success: false, message: "User not found" }); |
144 | | - } |
145 | | - |
146 | | - const typedFoundUser = asTypedUser(foundUser); |
147 | | - |
148 | | - // We send tokens the same way, but redirect the user |
149 | | - const accessToken = generateAccessToken(typedFoundUser._id.toString()); |
150 | | - const newRefreshToken = generateRefreshToken(typedFoundUser._id.toString()); |
151 | | - |
152 | | - typedFoundUser.refreshTokens = [newRefreshToken]; |
153 | | - await typedFoundUser.save(); |
154 | | - |
155 | | - res.cookie("jwt", newRefreshToken, { |
156 | | - httpOnly: true, |
157 | | - secure: process.env.NODE_ENV !== "development", |
158 | | - sameSite: "strict", |
159 | | - maxAge: 7 * 24 * 60 * 60 * 1000, |
160 | | - }); |
161 | 149 |
|
162 | | - // Set the access token in a secure, HTTP-only cookie |
163 | | - res.cookie("access_token", accessToken, { |
164 | | - httpOnly: true, |
165 | | - secure: process.env.NODE_ENV !== "development", |
166 | | - sameSite: "strict", |
167 | | - maxAge: 15 * 60 * 1000, // 15 minutes, adjust as needed |
| 150 | + res.json({ |
| 151 | + success: true, |
| 152 | + message: "Login successful", |
| 153 | + token, |
168 | 154 | }); |
169 | 155 |
|
170 | 156 | // Redirect to the frontend without passing the token in the URL |
|
0 commit comments