mirror of
https://github.com/jiangrui1994/CloudSaver.git
synced 2026-01-12 08:08:46 +08:00
22 lines
533 B
TypeScript
22 lines
533 B
TypeScript
import jwt from "jsonwebtoken";
|
|
import { Request } from "express";
|
|
import { config } from "../config";
|
|
|
|
interface JwtPayload {
|
|
userId: string;
|
|
}
|
|
|
|
export function getUserIdFromToken(req: Request): string | null {
|
|
try {
|
|
const token = req.headers.authorization?.split(" ")[1];
|
|
if (!token) {
|
|
throw new Error("Token not found");
|
|
}
|
|
const decoded = jwt.verify(token, config.jwtSecret) as JwtPayload;
|
|
return decoded.userId;
|
|
} catch (error) {
|
|
console.error("Invalid token:", error);
|
|
return null;
|
|
}
|
|
}
|