-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
21 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,29 @@ | ||
import jwt from "jsonwebtoken"; | ||
|
||
export default defineEventHandler(async event => { | ||
if ( | ||
(!["get", "options"].includes(event.method.toLowerCase()) && !event.path.startsWith("/api/admin/login")) && !event.path.startsWith("/api/file") && !(/\/api\/article\/\d+\/comment/.test(event.path)) | ||
|| event.path.startsWith("/api/admin/info") | ||
if (["get", "options"].includes(event.method.toLowerCase())) | ||
return; | ||
if (event.path.startsWith("/api/admin/login")) | ||
return; | ||
if (event.path.startsWith("/api/file")) | ||
return; | ||
if (/\/api\/article\/\d+\/comment/.test(event.path)) | ||
return; | ||
if (/\/api\/comment\/\d+\/comment/.test(event.path)) | ||
return; | ||
if (event.path.startsWith("/api/admin/info")) | ||
return; | ||
|
||
) { | ||
const authorization = getHeader(event, "Authorization") || ""; | ||
const authorization = getHeader(event, "Authorization") || ""; | ||
|
||
// if (!authorization) return new Error("Authorization不存在"); | ||
// if (!authorization) return new Error("Authorization不存在"); | ||
|
||
try { | ||
// 偷懒暂时先拿 EMAIL_PASS 做secretOrPublicKey | ||
const admin = jwt.verify(authorization, import.meta.env.EMAIL_PASS); | ||
event.context.admin = admin; | ||
} | ||
catch (err) { | ||
throw new Error("无可奈何"); | ||
} | ||
try { | ||
// 偷懒暂时先拿 EMAIL_PASS 做secretOrPublicKey | ||
const admin = jwt.verify(authorization, import.meta.env.EMAIL_PASS); | ||
event.context.admin = admin; | ||
} | ||
catch (err) { | ||
throw new Error("无可奈何"); | ||
} | ||
}); |