-
Notifications
You must be signed in to change notification settings - Fork 187
feat: agent org workspace + billing & auth robustness #464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4353d19
0362fd2
f89da70
7f01245
01c1a15
796a03a
ac30575
6efb97d
33b6246
93d92a9
29ce1cb
a9c4c8b
a61d668
d14b453
b73337a
7a21642
f236aad
cfbe7c3
db05d7a
350c91e
4477437
bfdf951
036abda
078a3d5
a214b35
b2011c5
5581d7e
3e8fced
396f01e
fbd5144
f3e5ddb
1c96cb3
f9e31bf
f206521
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,69 +48,64 @@ async function stripPrivilegedBody(request: Request): Promise<Request> { | |
| } | ||
|
|
||
| const text = await request.text(); | ||
| if (!text) { | ||
| return new Request(request.url, request); | ||
| } | ||
|
|
||
| let parsed: unknown; | ||
| try { | ||
| parsed = JSON.parse(text); | ||
| } catch { | ||
| return new Request(request.url, { | ||
| method: request.method, | ||
| headers: request.headers, | ||
| body: text, | ||
| }); | ||
| let body: string | null = text || null; | ||
| if (text) { | ||
| try { | ||
| body = JSON.stringify(sanitize(JSON.parse(text))); | ||
| } catch { | ||
| body = text; | ||
| } | ||
| } | ||
|
|
||
| return new Request(request.url, { | ||
| method: request.method, | ||
| headers: request.headers, | ||
| body: JSON.stringify(sanitize(parsed)), | ||
| body, | ||
| }); | ||
| } | ||
|
|
||
| const autumn = autumnHandler({ identify: identifyAutumnCustomer }); | ||
|
|
||
| export async function handleAutumnRequest(request: Request) { | ||
| const sanitized = await stripPrivilegedBody(request); | ||
| return autumnHandler({ | ||
| identify: identifyAutumnCustomer, | ||
| })(withAutumnApiPath(sanitized)); | ||
| return autumn(withAutumnApiPath(sanitized)); | ||
| } | ||
|
|
||
| async function identifyAutumnCustomer(request: Request) { | ||
| async function loadSession(request: Request) { | ||
| try { | ||
| const session = await auth.api.getSession({ headers: request.headers }); | ||
| if (!session?.user) { | ||
| return null; | ||
| } | ||
| return await auth.api.getSession({ headers: request.headers }); | ||
| } catch (error) { | ||
| const err = error instanceof Error ? error : new Error(String(error)); | ||
| useLogger().error(err, { | ||
| autumn: "identify", | ||
| autumn_stage: "getSession", | ||
| }); | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| async function identifyAutumnCustomer(request: Request) { | ||
| const session = await loadSession(request); | ||
| if (!session?.user) { | ||
| return null; | ||
| } | ||
|
|
||
| const activeOrgId = ( | ||
| session.session as { activeOrganizationId?: string | null } | ||
| )?.activeOrganizationId; | ||
| const activeOrgId = session.session.activeOrganizationId ?? null; | ||
|
|
||
| if (activeOrgId) { | ||
| const role = await getMemberRole(session.user.id, activeOrgId); | ||
| if (role !== "owner" && role !== "admin") { | ||
| return null; | ||
| } | ||
| if (activeOrgId) { | ||
| const role = await getMemberRole(session.user.id, activeOrgId); | ||
| if (role !== "owner" && role !== "admin") { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| const customerId = await getBillingCustomerId(session.user.id, activeOrgId); | ||
| const customerId = await getBillingCustomerId(session.user.id, activeOrgId); | ||
|
|
||
| return { | ||
| customerId, | ||
| customerData: { | ||
| name: session.user.name, | ||
| email: session.user.email, | ||
| }, | ||
| }; | ||
| } catch (error) { | ||
| useLogger().error( | ||
| error instanceof Error ? error : new Error(String(error)), | ||
| { | ||
| autumn: "identify", | ||
| } | ||
| ); | ||
| return null; | ||
| } | ||
| return { | ||
| customerId, | ||
| customerData: { | ||
| name: session.user.name, | ||
| email: session.user.email, | ||
| }, | ||
| }; | ||
| } | ||
|
Comment on lines
+87
to
111
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Rethrowing session lookup errors makes Autumn requests fail hard instead of degrading to anonymous customer resolution.
Prompt for AI agents