fix open connection with redis

This commit is contained in:
adhamhaddad 2023-04-26 13:42:00 +02:00
parent 0c84751943
commit b8a74d569d
7 changed files with 10 additions and 15 deletions

6
.env
View File

@ -11,8 +11,10 @@ POSTGRES_USER=admin
POSTGRES_PASSWORD=admin
# REDIS
REDIS_HOST=localhost
REDIS_URI=redis://default@localhost:6379
REDIS_HOST=default
REDIS_PORT=6379
REDIS_USER=default
REDIS_PASSWORD=admin
# BCRYPT
@ -22,7 +24,7 @@ SALT_ROUNDS=10
# JWT
JWT_SECRET_ACCESS_TOKEN=
JWT_SECRET_REFRESH_TOKEN=
JWT_ACCESS_TOKEN_EXPIRATION=300
JWT_ACCESS_TOKEN_EXPIRATION=15
JWT_REFRESH_TOKEN_EXPIRATION=86400
# URL

View File

@ -16,8 +16,10 @@ const configs = {
db_name: database,
db_user: process.env.POSTGRES_USER,
db_password: process.env.POSTGRES_PASSWORD,
redis_uri: process.env.REDIS_URI,
redis_host: process.env.REDIS_HOST,
redis_port: Number(process.env.REDIS_PORT),
redis_user: process.env.REDIS_USER,
redis_password: process.env.REDIS_PASSWORD,
salt: Number(process.env.SALT_ROUNDS),
pepper: process.env.SECRET_PEPPER,

View File

@ -1,6 +1,7 @@
import { createClient } from 'redis';
import configs from '../configs';
const client = createClient({ url: 'redis://default@localhost:6379' });
const client = createClient({ url: configs.redis_uri });
client.on('connect', () => {
console.log('Connected to Redis.');
@ -10,4 +11,6 @@ client.on('error', (error) => {
console.error('Error connecting to Redis:', error);
});
client.connect();
export default client;

View File

@ -16,7 +16,6 @@ const privateAccessKey = path.join(
);
export const setAccessToken = async (payload: Payload): Promise<string> => {
await redisClient.connect();
try {
const privateKey = await fs.promises.readFile(privateAccessKey, 'utf8');
const options: SignOptions = {
@ -33,7 +32,5 @@ export const setAccessToken = async (payload: Payload): Promise<string> => {
return token;
} catch (err) {
throw new Error('Failed to sign JWT');
} finally {
await redisClient.disconnect();
}
};

View File

@ -16,7 +16,6 @@ const privateRefreshKey = path.join(
);
export const setRefreshToken = async (payload: Payload): Promise<string> => {
await redisClient.connect();
try {
const privateKey = await fs.promises.readFile(privateRefreshKey, 'utf8');
const options: SignOptions = {
@ -33,7 +32,5 @@ export const setRefreshToken = async (payload: Payload): Promise<string> => {
return token;
} catch (err) {
throw new Error('Failed to sign JWT');
} finally {
await redisClient.disconnect();
}
};

View File

@ -25,7 +25,6 @@ export const verifyAccessToken = async (
res: Response,
next: NextFunction
) => {
await redisClient.connect();
try {
const authorization = req.headers.authorization as string;
if (!authorization) {
@ -50,10 +49,8 @@ export const verifyAccessToken = async (
throw new Error('Access token not found or expired');
}
req.user = { id: decoded.id };
await redisClient.disconnect();
return next();
} catch (err) {
await redisClient.disconnect();
if ((err as Error).name !== 'TokenExpiredError') {
throw new Error('Invalid access token');
}

View File

@ -15,7 +15,6 @@ const publicRefreshKey = path.join(
);
export const verifyRefreshToken = async (token: string): Promise<Payload> => {
await redisClient.connect();
try {
const publicKey = await fs.promises.readFile(publicRefreshKey, 'utf8');
const decoded = jwt.verify(token, publicKey, {
@ -29,7 +28,5 @@ export const verifyRefreshToken = async (token: string): Promise<Payload> => {
return decoded;
} catch (err) {
throw new Error(`Failed to verify JWT: ${(err as Error).message}`);
} finally {
await redisClient.disconnect();
}
};