공개 키 등록 구현

This commit is contained in:
static
2024-12-28 01:05:31 +09:00
parent dec17ecba8
commit c00dbe7024
8 changed files with 82 additions and 4 deletions

View File

@@ -1,3 +1,4 @@
import { error } from "@sveltejs/kit";
import jwt from "jsonwebtoken";
import env from "$lib/server/loadenv";
@@ -36,3 +37,22 @@ export const verifyToken = (token: string) => {
return TokenError.INVALID;
}
};
export const authenticate = (request: Request) => {
const accessToken = request.headers.get("Authorization");
if (!accessToken?.startsWith("Bearer ")) {
error(401, "Token required");
}
const tokenData = verifyToken(accessToken.slice(7));
if (tokenData === TokenError.EXPIRED) {
error(401, "Token expired");
} else if (tokenData === TokenError.INVALID || tokenData.type !== "access") {
error(401, "Invalid token");
}
return {
userId: tokenData.userId,
clientId: tokenData.clientId,
};
};