Skip to content
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

feat(nextjs): Handle URL <> Session Org Mismatch in Middleware #3977

Merged
merged 56 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
37f0478
WIP - first rough pass at args-based approach
izaaklauer Aug 16, 2024
7c3f936
Tested end-to-end with corresponding fapi changes locally
izaaklauer Aug 28, 2024
54e6752
Starting on integration tests
izaaklauer Sep 6, 2024
d724695
Two more tests
izaaklauer Sep 6, 2024
925be56
First pass at table-driven tests
izaaklauer Sep 9, 2024
fee771a
Trying out server-per-test
izaaklauer Sep 9, 2024
5c575ee
Now in parallel!
izaaklauer Sep 9, 2024
aa84f66
The table-driven tests are now looking quite nice!
izaaklauer Sep 9, 2024
45a982b
Doc comments
izaaklauer Sep 9, 2024
edffbe0
Detecting and handling handshake org sync loops
izaaklauer Sep 9, 2024
256bef6
A handful more tests
izaaklauer Sep 10, 2024
830dfea
Handling multiple path patterns, better test structure
izaaklauer Sep 10, 2024
63519d9
testing paths with sub-resources
izaaklauer Sep 10, 2024
d77382d
Merge branch 'main' into izaak/ORGS-132-middleware-orgsync
izaaklauer Sep 13, 2024
02823ef
Upgrading path-to-regexp
izaaklauer Sep 13, 2024
a6d4136
Test case with erroneous trailing slash now passing
izaaklauer Sep 13, 2024
3a8f583
Handling header auth in addition to cookie auth
izaaklauer Sep 13, 2024
b149e6b
Cleaning up todos, documenting functions
izaaklauer Sep 13, 2024
32e4e0d
More thoughtful test cases
izaaklauer Sep 16, 2024
27257f9
Testing redirect loop protection
izaaklauer Sep 16, 2024
494e468
Not re-running every test with header/cookie auth
izaaklauer Sep 16, 2024
bf847d4
Merge branch 'main' into izaak/ORGS-132-middleware-orgsync
izaaklauer Sep 17, 2024
4c1dbb3
Using vendored path-to-regexp
izaaklauer Sep 18, 2024
5381882
Now using tsup to export types
izaaklauer Sep 18, 2024
d0d545f
Responding to https://github.com/clerk/javascript/pull/3977/files#r17…
izaaklauer Sep 18, 2024
26d07b4
Responding to:
izaaklauer Sep 18, 2024
76be5f6
Update packages/backend/src/tokens/types.ts
izaaklauer Sep 18, 2024
cbde8fd
Better naming and types
izaaklauer Sep 18, 2024
c0b291d
Switching conclusively to the internal path-to-regexp
izaaklauer Sep 19, 2024
618ac48
jsdoc comment everything
izaaklauer Sep 20, 2024
cd8ba02
Merge branch 'main' into izaak/ORGS-132-middleware-orgsync
izaaklauer Sep 20, 2024
c202832
Throwing errors that are likely to be encountered during development
izaaklauer Sep 20, 2024
6c8f99f
Making fewer type assumptions
izaaklauer Sep 20, 2024
3d2dd3b
Small test rearrangement
izaaklauer Sep 20, 2024
1539d2c
Removing the path-to-regexp dependency (now that we're using the inte…
izaaklauer Sep 20, 2024
3fef10d
Updating names in comments
izaaklauer Sep 20, 2024
59f1c15
Removing unnecesary awaits
izaaklauer Sep 20, 2024
c8aad63
Cleanining up vestages of a merge conflict
izaaklauer Sep 20, 2024
c996bda
Changeset
izaaklauer Sep 20, 2024
0b93ac1
Update .changeset/olive-trainers-heal.md
izaaklauer Sep 20, 2024
2aeda05
Not handshaking on header-based auth
izaaklauer Sep 23, 2024
e62e239
Using a consistent clerkUrl
izaaklauer Sep 24, 2024
0afcd0a
Merge branch 'main' into izaak/ORGS-132-middleware-orgsync
izaaklauer Sep 27, 2024
efc5599
Small refactor to remove redundancy
izaaklauer Sep 27, 2024
f55a074
ran `npm i`
izaaklauer Sep 27, 2024
f7bec4f
Running `npm run format`
izaaklauer Sep 27, 2024
4bce2ce
Quieting typescript's objections
izaaklauer Sep 27, 2024
b7e8ac1
Update packages/backend/src/tokens/request.ts
izaaklauer Sep 27, 2024
5ce428d
Update packages/backend/src/tokens/request.ts
izaaklauer Sep 27, 2024
5858a24
Renaming "personal wockrkspace" to "personal account"
izaaklauer Oct 1, 2024
3551d2f
avoiding spinning up a separate sample app per test case
izaaklauer Oct 2, 2024
9f36a37
Only computing matchers once for efficency.
izaaklauer Oct 3, 2024
5780554
Making types PascelCase
izaaklauer Oct 3, 2024
4076c03
Using more concise array type declaration
izaaklauer Oct 3, 2024
3aa3f67
Reverting an incidental upgrate to path-to-regexp
izaaklauer Oct 7, 2024
bc89068
Merge branch 'main' into izaak/ORGS-132-middleware-orgsync
izaaklauer Oct 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/olive-trainers-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/nextjs": patch
---

Introduces `organizationSyncOptions` option to `clerkMiddleware`, which syncs an active organization or personal account from a URL to the Clerk session.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { auth } from '@clerk/nextjs/server';

export default function Home({ params }: { params: { id: string } }) {
const { orgId } = auth();

if (params.id != orgId) {
console.log('Mismatch - returning nothing for now...', params.id, orgId);
}

console.log("I'm the server and I got this id: ", orgId);

return (
<>
<p>Org-specific home</p>
<p>From auth(), I know your org id is: {orgId}</p>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { auth } from '@clerk/nextjs/server';

export default function Home({ params }: { params: { id: string } }) {
const { orgId } = auth();

if (params.id != orgId) {
console.log('Mismatch - returning nothing for now...', params.id, orgId);
}

console.log("I'm the server and I got this id: ", orgId);

return (
<>
<p>Org-specific settings</p>
<p>From auth(), I know your org id is: {orgId}</p>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { auth } from '@clerk/nextjs/server';

export default function Home({ params }: { params: { slug: string } }) {
const { orgSlug } = auth();

if (params.slug != orgSlug) {
console.log('Mismatch - returning nothing for now...', params.slug, orgSlug);
}

console.log("I'm the server and I got this slug: ", orgSlug);

return (
<>
<p>Org-specific home</p>
<p>From auth(), I know your org slug is: {orgSlug}</p>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { auth } from '@clerk/nextjs/server';

export default function Home({ params }: { params: { slug: string } }) {
const { orgSlug } = auth();

if (params.slug != orgSlug) {
console.log('Mismatch - returning nothing for now...', params.slug, orgSlug);
}

console.log("I'm the server and I got this slug: ", orgSlug);

return (
<>
<p>Org-specific settings</p>
<p>From auth(), I know your org slug is: {orgSlug}</p>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { auth } from '@clerk/nextjs/server';

export default function Home(): {} {
const { orgId } = auth();

if (orgId != null) {
console.log('Oh no, this page should only activate on the personal account!');
}

return (
<>
<p>Welcome to your personal account</p>
</>
);
}
16 changes: 15 additions & 1 deletion integration/testUtils/handshake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ export function generateConfig({ mode, matchedKeys = true }: { mode: 'test' | 'l
exp: number;
nbf: number;
};
const generateToken = ({ state }: { state: 'active' | 'expired' | 'early' }) => {
const generateToken = ({
state,
extraClaims,
}: {
state: 'active' | 'expired' | 'early';
extraClaims?: Map<string, any>;
}) => {
const claims = { sub: 'user_12345' } as Claims;

const now = Math.floor(Date.now() / 1000);
Expand All @@ -121,6 +127,14 @@ export function generateConfig({ mode, matchedKeys = true }: { mode: 'test' | 'l
claims.nbf = now - 10 + 600;
claims.exp = now + 60 + 600;
}

// Merge claims with extraClaims
if (extraClaims) {
for (const [key, value] of extraClaims) {
claims[key] = value;
}
}

return {
token: jwt.sign(claims, rsa.private, {
algorithm: 'RS256',
Expand Down
Loading
Loading