| 68 | } |
| 69 | |
| 70 | async function getAuth(c: Context) { |
| 71 | const authHeader = c.req.header("authorization")?.split(" ")[1]; |
| 72 | |
| 73 | let user: Awaited<ReturnType<typeof getCurrentUser>> | undefined; |
| 74 | |
| 75 | if (authHeader?.length === 36) { |
| 76 | const res = await db() |
| 77 | .select() |
| 78 | .from(users) |
| 79 | .leftJoin(authApiKeys, eq(users.id, authApiKeys.userId)) |
| 80 | .where(eq(authApiKeys.id, authHeader)); |
| 81 | user = res[0]?.users; |
| 82 | } else { |
| 83 | user = await getCurrentUser(); |
| 84 | } |
| 85 | |
| 86 | if (!user) return; |
| 87 | return { user }; |
| 88 | } |
| 89 | |
| 90 | export const withOptionalAuth = createMiddleware<{ |
| 91 | Variables: { |