Browse Source

Fix TypeScript JWT type errors with proper type assertions

drawing-pad
Stephanie Gredell 1 month ago
parent
commit
52aab425fa
  1. 14
      backend/src/services/auth.service.ts

14
backend/src/services/auth.service.ts

@ -1,4 +1,4 @@
import jwt from 'jsonwebtoken'; import jwt, { Secret, SignOptions } from 'jsonwebtoken';
import bcrypt from 'bcrypt'; import bcrypt from 'bcrypt';
import crypto from 'crypto'; import crypto from 'crypto';
import { env } from '../config/env.js'; import { env } from '../config/env.js';
@ -8,16 +8,16 @@ export async function createTokens(userId: number, username: string) {
// Access token (short-lived) // Access token (short-lived)
const accessToken = jwt.sign( const accessToken = jwt.sign(
{ userId, username, type: 'access' }, { userId, username, type: 'access' },
env.jwtSecret, env.jwtSecret as Secret,
{ expiresIn: env.accessTokenExpiry as string } { expiresIn: env.accessTokenExpiry } as SignOptions
); );
// Refresh token (long-lived) // Refresh token (long-lived)
const refreshTokenValue = crypto.randomBytes(64).toString('hex'); const refreshTokenValue = crypto.randomBytes(64).toString('hex');
const refreshToken = jwt.sign( const refreshToken = jwt.sign(
{ token: refreshTokenValue, userId, type: 'refresh' }, { token: refreshTokenValue, userId, type: 'refresh' },
env.jwtRefreshSecret, env.jwtRefreshSecret as Secret,
{ expiresIn: env.refreshTokenExpiry as string } { expiresIn: env.refreshTokenExpiry } as SignOptions
); );
// Store refresh token in database // Store refresh token in database
@ -58,8 +58,8 @@ export async function refreshAccessToken(refreshToken: string) {
// Generate new access token // Generate new access token
const accessToken = jwt.sign( const accessToken = jwt.sign(
{ userId: tokenData.user_id, username: tokenData.username, type: 'access' }, { userId: tokenData.user_id, username: tokenData.username, type: 'access' },
env.jwtSecret, env.jwtSecret as Secret,
{ expiresIn: env.accessTokenExpiry as string } { expiresIn: env.accessTokenExpiry } as SignOptions
); );
return { accessToken }; return { accessToken };

Loading…
Cancel
Save