Token will be sent in authorization via Bearer prefix
This commit is contained in:
+11
-11
@@ -1,18 +1,18 @@
|
||||
import "dotenv/config";
|
||||
|
||||
import { IncomingMessage } from "node:http";
|
||||
import { ExtendedIncomingMessage } from "../interfaces/extendedIncomingMessage";
|
||||
import { JwtAuthenticator } from "./jwtAuthenticator";
|
||||
import {IncomingMessage} from "node:http";
|
||||
import {ExtendedIncomingMessage} from "../interfaces/extendedIncomingMessage";
|
||||
import {JwtAuthenticator} from "./jwtAuthenticator";
|
||||
|
||||
export function verifyClient(
|
||||
request: IncomingMessage,
|
||||
callback: (res: boolean, code?: number, message?: string) => void,
|
||||
request: IncomingMessage,
|
||||
callback: (res: boolean, code?: number, message?: string) => void,
|
||||
) {
|
||||
const jwtAuthenticator = new JwtAuthenticator(
|
||||
process.env.SECRET_KEY as string,
|
||||
process.env.SECRET_KEY as string,
|
||||
);
|
||||
|
||||
const token = jwtAuthenticator.verifyToken(request.headers["authorization"]);
|
||||
const token = jwtAuthenticator.verifyToken(request.headers["authorization"]?.slice("Bearer ".length));
|
||||
if (!token) {
|
||||
reject(request, callback);
|
||||
} else {
|
||||
@@ -22,12 +22,12 @@ export function verifyClient(
|
||||
}
|
||||
|
||||
const reject = (
|
||||
request: IncomingMessage,
|
||||
callback: (res: boolean, code?: number, message?: string) => void,
|
||||
request: IncomingMessage,
|
||||
callback: (res: boolean, code?: number, message?: string) => void,
|
||||
) => {
|
||||
console.log(
|
||||
"Connection refused",
|
||||
`${request.socket.remoteAddress}:${request.socket.remotePort}`,
|
||||
"Connection refused",
|
||||
`${request.socket.remoteAddress}:${request.socket.remotePort}`,
|
||||
);
|
||||
callback(false, 401, "Unauthorized");
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user