This PR implements a new authentication feature with JWT tokens. Review this code submission and provide feedback
| Author | developer@example.com |
| Repository | backend-api |
| Branch | feature/jwt-auth |
| PR Number | #1234 |
| Files Changed | 5 |
| Lines Added | 234 |
| Lines Removed | 12 |
| Breaking Changes | No |
Choose an action and submit to continue the workflow.
This PR implements a new authentication feature with JWT tokens. Review this code submission and provide feedback
| Author | developer@example.com |
| Repository | backend-api |
| Branch | feature/jwt-auth |
| PR Number | #1234 |
| Files Changed | 5 |
| Lines Added | 234 |
| Lines Removed | 12 |
| Breaking Changes | No |
Choose an action and submit to continue the workflow.
import jwt from 'jsonwebtoken';
export function generateToken(userId: string): string {
- return jwt.sign({ userId }, process.env.JWT_SECRET, { expiresIn: '24h' });
+ const secret = process.env.JWT_SECRET;
+ if (!secret) {
+ throw new Error('JWT_SECRET is not defined');
+ }
+
+ return jwt.sign(
+ { userId, iat: Math.floor(Date.now() / 1000) },
+ secret,
+ { expiresIn: '24h' }
+ );
}
export function verifyToken(token: string): { userId: string } {
- return jwt.verify(token, process.env.JWT_SECRET) as { userId: string };
+ const secret = process.env.JWT_SECRET;
+ if (!secret) {
+ throw new Error('JWT_SECRET is not defined');
+ }
+
+ return jwt.verify(token, secret) as { userId: string };
}import jwt from 'jsonwebtoken';
export function generateToken(userId: string): string {
- return jwt.sign({ userId }, process.env.JWT_SECRET, { expiresIn: '24h' });
+ const secret = process.env.JWT_SECRET;
+ if (!secret) {
+ throw new Error('JWT_SECRET is not defined');
+ }
+
+ return jwt.sign(
+ { userId, iat: Math.floor(Date.now() / 1000) },
+ secret,
+ { expiresIn: '24h' }
+ );
}
export function verifyToken(token: string): { userId: string } {
- return jwt.verify(token, process.env.JWT_SECRET) as { userId: string };
+ const secret = process.env.JWT_SECRET;
+ if (!secret) {
+ throw new Error('JWT_SECRET is not defined');
+ }
+
+ return jwt.verify(token, secret) as { userId: string };
}