mirror of
https://github.com/bubblecup-12/VogelSocialMedia.git
synced 2025-07-09 00:08:46 +00:00
25 lines
678 B
TypeScript
25 lines
678 B
TypeScript
import { Request, Response, NextFunction } from "express";
|
|
import jwt from "jsonwebtoken";
|
|
import { JwtPayload } from "../types/tokens";
|
|
const JWT_SECRET: string = process.env.TOKEN_SECRET!;
|
|
|
|
export const optionalAuthenticateToken = (
|
|
req: Request,
|
|
res: Response,
|
|
next: NextFunction
|
|
) => {
|
|
const authHeader = req.headers["authorization"]; // Get the authorization header
|
|
const token = authHeader && authHeader.split(" ")[1]; // Extract the token from the "Bearer <token>" format
|
|
|
|
if (!token) {
|
|
return next();
|
|
}
|
|
|
|
jwt.verify(token!, JWT_SECRET, (err, user) => {
|
|
if (err) {
|
|
return next();
|
|
}
|
|
req.user = user as JwtPayload;
|
|
next();
|
|
});
|
|
};
|