fix open connection with redis
This commit is contained in:
parent
0c84751943
commit
b8a74d569d
6
.env
6
.env
@ -11,8 +11,10 @@ POSTGRES_USER=admin
|
|||||||
POSTGRES_PASSWORD=admin
|
POSTGRES_PASSWORD=admin
|
||||||
|
|
||||||
# REDIS
|
# REDIS
|
||||||
REDIS_HOST=localhost
|
REDIS_URI=redis://default@localhost:6379
|
||||||
|
REDIS_HOST=default
|
||||||
REDIS_PORT=6379
|
REDIS_PORT=6379
|
||||||
|
REDIS_USER=default
|
||||||
REDIS_PASSWORD=admin
|
REDIS_PASSWORD=admin
|
||||||
|
|
||||||
# BCRYPT
|
# BCRYPT
|
||||||
@ -22,7 +24,7 @@ SALT_ROUNDS=10
|
|||||||
# JWT
|
# JWT
|
||||||
JWT_SECRET_ACCESS_TOKEN=
|
JWT_SECRET_ACCESS_TOKEN=
|
||||||
JWT_SECRET_REFRESH_TOKEN=
|
JWT_SECRET_REFRESH_TOKEN=
|
||||||
JWT_ACCESS_TOKEN_EXPIRATION=300
|
JWT_ACCESS_TOKEN_EXPIRATION=15
|
||||||
JWT_REFRESH_TOKEN_EXPIRATION=86400
|
JWT_REFRESH_TOKEN_EXPIRATION=86400
|
||||||
|
|
||||||
# URL
|
# URL
|
||||||
|
|||||||
@ -16,8 +16,10 @@ const configs = {
|
|||||||
db_name: database,
|
db_name: database,
|
||||||
db_user: process.env.POSTGRES_USER,
|
db_user: process.env.POSTGRES_USER,
|
||||||
db_password: process.env.POSTGRES_PASSWORD,
|
db_password: process.env.POSTGRES_PASSWORD,
|
||||||
|
redis_uri: process.env.REDIS_URI,
|
||||||
redis_host: process.env.REDIS_HOST,
|
redis_host: process.env.REDIS_HOST,
|
||||||
redis_port: Number(process.env.REDIS_PORT),
|
redis_port: Number(process.env.REDIS_PORT),
|
||||||
|
redis_user: process.env.REDIS_USER,
|
||||||
redis_password: process.env.REDIS_PASSWORD,
|
redis_password: process.env.REDIS_PASSWORD,
|
||||||
salt: Number(process.env.SALT_ROUNDS),
|
salt: Number(process.env.SALT_ROUNDS),
|
||||||
pepper: process.env.SECRET_PEPPER,
|
pepper: process.env.SECRET_PEPPER,
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { createClient } from 'redis';
|
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', () => {
|
client.on('connect', () => {
|
||||||
console.log('Connected to Redis.');
|
console.log('Connected to Redis.');
|
||||||
@ -10,4 +11,6 @@ client.on('error', (error) => {
|
|||||||
console.error('Error connecting to Redis:', error);
|
console.error('Error connecting to Redis:', error);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
client.connect();
|
||||||
|
|
||||||
export default client;
|
export default client;
|
||||||
|
|||||||
@ -16,7 +16,6 @@ const privateAccessKey = path.join(
|
|||||||
);
|
);
|
||||||
|
|
||||||
export const setAccessToken = async (payload: Payload): Promise<string> => {
|
export const setAccessToken = async (payload: Payload): Promise<string> => {
|
||||||
await redisClient.connect();
|
|
||||||
try {
|
try {
|
||||||
const privateKey = await fs.promises.readFile(privateAccessKey, 'utf8');
|
const privateKey = await fs.promises.readFile(privateAccessKey, 'utf8');
|
||||||
const options: SignOptions = {
|
const options: SignOptions = {
|
||||||
@ -33,7 +32,5 @@ export const setAccessToken = async (payload: Payload): Promise<string> => {
|
|||||||
return token;
|
return token;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error('Failed to sign JWT');
|
throw new Error('Failed to sign JWT');
|
||||||
} finally {
|
|
||||||
await redisClient.disconnect();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -16,7 +16,6 @@ const privateRefreshKey = path.join(
|
|||||||
);
|
);
|
||||||
|
|
||||||
export const setRefreshToken = async (payload: Payload): Promise<string> => {
|
export const setRefreshToken = async (payload: Payload): Promise<string> => {
|
||||||
await redisClient.connect();
|
|
||||||
try {
|
try {
|
||||||
const privateKey = await fs.promises.readFile(privateRefreshKey, 'utf8');
|
const privateKey = await fs.promises.readFile(privateRefreshKey, 'utf8');
|
||||||
const options: SignOptions = {
|
const options: SignOptions = {
|
||||||
@ -33,7 +32,5 @@ export const setRefreshToken = async (payload: Payload): Promise<string> => {
|
|||||||
return token;
|
return token;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error('Failed to sign JWT');
|
throw new Error('Failed to sign JWT');
|
||||||
} finally {
|
|
||||||
await redisClient.disconnect();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -25,7 +25,6 @@ export const verifyAccessToken = async (
|
|||||||
res: Response,
|
res: Response,
|
||||||
next: NextFunction
|
next: NextFunction
|
||||||
) => {
|
) => {
|
||||||
await redisClient.connect();
|
|
||||||
try {
|
try {
|
||||||
const authorization = req.headers.authorization as string;
|
const authorization = req.headers.authorization as string;
|
||||||
if (!authorization) {
|
if (!authorization) {
|
||||||
@ -50,10 +49,8 @@ export const verifyAccessToken = async (
|
|||||||
throw new Error('Access token not found or expired');
|
throw new Error('Access token not found or expired');
|
||||||
}
|
}
|
||||||
req.user = { id: decoded.id };
|
req.user = { id: decoded.id };
|
||||||
await redisClient.disconnect();
|
|
||||||
return next();
|
return next();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await redisClient.disconnect();
|
|
||||||
if ((err as Error).name !== 'TokenExpiredError') {
|
if ((err as Error).name !== 'TokenExpiredError') {
|
||||||
throw new Error('Invalid access token');
|
throw new Error('Invalid access token');
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,6 @@ const publicRefreshKey = path.join(
|
|||||||
);
|
);
|
||||||
|
|
||||||
export const verifyRefreshToken = async (token: string): Promise<Payload> => {
|
export const verifyRefreshToken = async (token: string): Promise<Payload> => {
|
||||||
await redisClient.connect();
|
|
||||||
try {
|
try {
|
||||||
const publicKey = await fs.promises.readFile(publicRefreshKey, 'utf8');
|
const publicKey = await fs.promises.readFile(publicRefreshKey, 'utf8');
|
||||||
const decoded = jwt.verify(token, publicKey, {
|
const decoded = jwt.verify(token, publicKey, {
|
||||||
@ -29,7 +28,5 @@ export const verifyRefreshToken = async (token: string): Promise<Payload> => {
|
|||||||
return decoded;
|
return decoded;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(`Failed to verify JWT: ${(err as Error).message}`);
|
throw new Error(`Failed to verify JWT: ${(err as Error).message}`);
|
||||||
} finally {
|
|
||||||
await redisClient.disconnect();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user