diff --git a/server/api/login.ts b/server/api/login.ts new file mode 100644 index 0000000..255f4ed --- /dev/null +++ b/server/api/login.ts @@ -0,0 +1,5 @@ +import process from "node:process" + +export default defineEventHandler(async (event) => { + return sendRedirect(event, `https://github.com/login/oauth/authorize?client_id=${process.env.G_CLIENT_ID}`) +}) diff --git a/server/api/me/sync.ts b/server/api/me/sync.ts index 3c07252..fa8d9cd 100644 --- a/server/api/me/sync.ts +++ b/server/api/me/sync.ts @@ -1,3 +1,4 @@ +import process from "node:process" import { verifyPrimitiveMetadata } from "@shared/verify" import { UserTable } from "#/database/user" @@ -7,6 +8,7 @@ export default defineEventHandler(async (event) => { const db = useDatabase() if (!db) throw new Error("Not found database") const userTable = new UserTable(db) + if (process.env.INIT_TABLE !== "false") await userTable.init() if (event.method === "GET") { const { data, updated } = await userTable.getData(id) return { diff --git a/server/api/oauth/github.ts b/server/api/oauth/github.ts index 9395686..1d5ff4f 100644 --- a/server/api/oauth/github.ts +++ b/server/api/oauth/github.ts @@ -3,7 +3,6 @@ import { SignJWT } from "jose" import { UserTable } from "#/database/user" export default defineEventHandler(async (event) => { - if (["JWT_SECRET", "G_CLIENT_ID", "G_CLIENT_SECRET"].find(k => !process.env[k])) throw new Error("Missing environment variables") const db = useDatabase() const userTable = db ? new UserTable(db) : undefined if (!userTable) throw new Error("db is not defined") diff --git a/server/middleware/auth.ts b/server/middleware/auth.ts index cb8a4e8..3f1b12b 100644 --- a/server/middleware/auth.ts +++ b/server/middleware/auth.ts @@ -5,11 +5,12 @@ export default defineEventHandler(async (event) => { const url = getRequestURL(event) if (["JWT_SECRET", "G_CLIENT_ID", "G_CLIENT_SECRET"].find(k => !process.env[k])) { event.context.disabledLogin = true - if (url.pathname.startsWith("/api/me")) throw createError({ statusCode: 506, message: "Server not configured" }) + if (!url.pathname.startsWith("/api/s")) + throw createError({ statusCode: 506, message: "Server not configured, disable login" }) } else { - if (/^\/api\/(?:me|s)\//.test(url.pathname)) { + if (["/api/s", "/api/me"].find(p => url.pathname.startsWith(p))) { const token = getHeader(event, "Authorization")?.replace("Bearer ", "")?.trim() - if (token && process.env.JWT_SECRET) { + if (token) { try { const { payload } = await jwtVerify(token, new TextEncoder().encode(process.env.JWT_SECRET)) as { payload?: { id: string, type: string } } if (payload?.id) { @@ -19,9 +20,12 @@ export default defineEventHandler(async (event) => { } } } catch { - if (url.pathname.startsWith("/api/me")) throw createError({ statusCode: 401, message: "JWT verification failed" }) - logger.warn("JWT verification failed") + if (url.pathname.startsWith("/api/me")) + throw createError({ statusCode: 401, message: "JWT verification failed" }) + else logger.warn("JWT verification failed") } + } else if (url.pathname.startsWith("/api/me")) { + throw createError({ statusCode: 401, message: "JWT verification failed" }) } } } diff --git a/src/components/header/menu.tsx b/src/components/header/menu.tsx index 6f9e0f6..3168749 100644 --- a/src/components/header/menu.tsx +++ b/src/components/header/menu.tsx @@ -19,7 +19,7 @@ function ThemeToggle() { } export function Menu() { - const { loggedIn, login, logout, enabledLogin, userInfo } = useLogin() + const { loggedIn, login, logout, userInfo } = useLogin() const [shown, show] = useState(false) const ref = useRef(null) const isHover = useHoverDirty(ref) @@ -30,7 +30,7 @@ export function Menu() { { - enabledLogin && loggedIn && userInfo.avatar + loggedIn && userInfo.avatar ? (