-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clerk-react,nextjs,shared): Introduce experimental `useReverific…
…ation` (#4362)
- Loading branch information
1 parent
24cd779
commit 08c5a2a
Showing
42 changed files
with
690 additions
and
338 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@clerk/nextjs": minor | ||
--- | ||
|
||
Bug fix: For next>=14 applications resolve `__unstable__onBeforeSetActive` once `invalidateCacheAction` resolves. |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
"@clerk/nextjs": minor | ||
"@clerk/clerk-react": minor | ||
--- | ||
|
||
Introduce a new experimental hook called `useReverification` that makes it easy to handle reverification errors. | ||
It returns a high order function (HOF) and allows developers to wrap any function that triggers a fetch request which might fail due to a user's session verification status. | ||
When such error is returned, the recommended UX is to offer a way to the user to recover by re-verifying their credentials. | ||
This helper will automatically handle this flow in the developer's behalf, by displaying a modal the end-user can interact with. | ||
Upon completion, the original request that previously failed, will be retried (only once). | ||
|
||
Example with clerk-js methods. | ||
```tsx | ||
import { __experimental_useReverification as useReverification } from '@clerk/nextjs'; | ||
|
||
function DeleteAccount() { | ||
const { user } = useUser(); | ||
const [deleteUserAccount] = useReverification(() => { | ||
if (!user) return; | ||
return user.delete() | ||
}); | ||
|
||
return <> | ||
<button | ||
onClick={async () => { | ||
await deleteUserAccount(); | ||
}}> | ||
Delete account | ||
</button> | ||
</> | ||
} | ||
|
||
``` |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@clerk/shared": minor | ||
--- | ||
|
||
Introduce experimental reverification error helpers. | ||
- `reverificationMismatch` returns the error as an object which can later be used as a return value from a React Server Action. | ||
- `reverificationMismatchResponse` returns a Response with the above object serialized. It can be used in any Backend Javascript frameworks that supports `Response`. |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@clerk/clerk-js": patch | ||
--- | ||
|
||
Chore: Replace beforeEmit with an explicit call after `setActive`, inside the experimental UserVerification. |
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
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
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
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
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
30 changes: 30 additions & 0 deletions
30
...emplates/next-app-router/src/app/(reverification)/action-with-use-reverification/page.tsx
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use client'; | ||
import { useState, useTransition } from 'react'; | ||
import { __experimental_useReverification as useReverification } from '@clerk/nextjs'; | ||
import { logUserIdActionReverification } from '@/app/(reverification)/actions'; | ||
|
||
function Page() { | ||
const [logUserWithReverification] = useReverification(logUserIdActionReverification); | ||
const [pending, startTransition] = useTransition(); | ||
const [res, setRes] = useState(null); | ||
|
||
return ( | ||
<> | ||
<button | ||
disabled={pending} | ||
onClick={() => { | ||
startTransition(async () => { | ||
await logUserWithReverification().then(e => { | ||
setRes(e as any); | ||
}); | ||
}); | ||
}} | ||
> | ||
LogUserId | ||
</button> | ||
<pre>{JSON.stringify(res)}</pre> | ||
</> | ||
); | ||
} | ||
|
||
export default Page; |
27 changes: 27 additions & 0 deletions
27
integration/templates/next-app-router/src/app/(reverification)/actions.ts
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use server'; | ||
|
||
import { auth } from '@clerk/nextjs/server'; | ||
import { __experimental_reverificationMismatch as reverificationMismatch } from '@clerk/shared/authorization-errors'; | ||
|
||
const logUserIdActionReverification = async () => { | ||
const { userId, has } = await auth.protect(); | ||
|
||
const config = { | ||
level: 'secondFactor', | ||
afterMinutes: 1, | ||
} as const; | ||
|
||
const userNeedsReverification = !has({ | ||
__experimental_reverification: config, | ||
}); | ||
|
||
if (userNeedsReverification) { | ||
return reverificationMismatch(config); | ||
} | ||
|
||
return { | ||
userId, | ||
}; | ||
}; | ||
|
||
export { logUserIdActionReverification }; |
23 changes: 23 additions & 0 deletions
23
integration/templates/next-app-router/src/app/(reverification)/button-action.tsx
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use client'; | ||
import { useState, useTransition } from 'react'; | ||
|
||
export function ButtonAction({ action }: { action: () => Promise<any> }) { | ||
const [pending, startTransition] = useTransition(); | ||
const [res, setRes] = useState(null); | ||
|
||
return ( | ||
<> | ||
<button | ||
disabled={pending} | ||
onClick={() => { | ||
startTransition(async () => { | ||
await action().then(setRes); | ||
}); | ||
}} | ||
> | ||
LogUserId | ||
</button> | ||
<pre>{JSON.stringify(res)}</pre> | ||
</> | ||
); | ||
} |
8 changes: 8 additions & 0 deletions
8
...tion/templates/next-app-router/src/app/(reverification)/requires-re-verification/page.tsx
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { logUserIdActionReverification } from '../actions'; | ||
import { ButtonAction } from '../button-action'; | ||
|
||
function Page() { | ||
return <ButtonAction action={logUserIdActionReverification} />; | ||
} | ||
|
||
export default Page; |
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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { Browser, BrowserContext } from '@playwright/test'; | ||
|
||
import type { createAppPageObject } from './appPageObject'; | ||
import { common } from './commonPageObject'; | ||
|
||
export type EnchancedPage = ReturnType<typeof createAppPageObject>; | ||
export type TestArgs = { page: EnchancedPage; context: BrowserContext; browser: Browser }; | ||
|
||
export const createUserVerificationComponentPageObject = (testArgs: TestArgs) => { | ||
const { page } = testArgs; | ||
const self = { | ||
...common(testArgs), | ||
waitForMounted: (selector = '.cl-userVerification-root') => { | ||
return page.waitForSelector(selector, { state: 'attached' }); | ||
}, | ||
getUseAnotherMethodLink: () => { | ||
return page.getByRole('link', { name: /use another method/i }); | ||
}, | ||
getAltMethodsEmailCodeButton: () => { | ||
return page.getByRole('button', { name: /email code to/i }); | ||
}, | ||
getAltMethodsEmailLinkButton: () => { | ||
return page.getByRole('button', { name: /email link to/i }); | ||
}, | ||
}; | ||
return self; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import type { OrganizationMembershipRole } from '@clerk/backend'; | ||
import { expect, test } from '@playwright/test'; | ||
|
||
import { appConfigs } from '../presets'; | ||
import type { FakeOrganization, FakeUser } from '../testUtils'; | ||
import { createTestUtils, testAgainstRunningApps } from '../testUtils'; | ||
|
||
const utils = [ | ||
'action', | ||
// , 'route' | ||
]; | ||
const capitalize = (type: string) => type[0].toUpperCase() + type.slice(1); | ||
testAgainstRunningApps({ withEnv: [appConfigs.envs.withReverification] })( | ||
'@nextjs require re-verification', | ||
({ app }) => { | ||
test.describe.configure({ mode: 'parallel' }); | ||
|
||
let fakeAdmin: FakeUser; | ||
let fakeViewer: FakeUser; | ||
let fakeOrganization: FakeOrganization; | ||
|
||
test.beforeAll(async () => { | ||
const m = createTestUtils({ app }); | ||
fakeAdmin = m.services.users.createFakeUser(); | ||
const admin = await m.services.users.createBapiUser(fakeAdmin); | ||
fakeOrganization = await m.services.users.createFakeOrganization(admin.id); | ||
fakeViewer = m.services.users.createFakeUser(); | ||
const viewer = await m.services.users.createBapiUser(fakeViewer); | ||
await m.services.clerk.organizations.createOrganizationMembership({ | ||
organizationId: fakeOrganization.organization.id, | ||
role: 'org:viewer' as OrganizationMembershipRole, | ||
userId: viewer.id, | ||
}); | ||
}); | ||
|
||
test.afterAll(async () => { | ||
await fakeOrganization.delete(); | ||
await fakeViewer.deleteIfExists(); | ||
await fakeAdmin.deleteIfExists(); | ||
await app.teardown(); | ||
}); | ||
|
||
utils.forEach(type => { | ||
test(`reverification error from ${capitalize(type)}`, async ({ page, context }) => { | ||
test.setTimeout(270_000); | ||
const u = createTestUtils({ app, page, context }); | ||
|
||
await u.po.signIn.goTo(); | ||
await u.po.signIn.waitForMounted(); | ||
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password }); | ||
await u.po.expect.toBeSignedIn(); | ||
|
||
await u.po.organizationSwitcher.goTo(); | ||
await u.po.organizationSwitcher.waitForMounted(); | ||
await u.po.organizationSwitcher.waitForAnOrganizationToSelected(); | ||
|
||
await u.page.goToRelative(`/requires-re-verification`); | ||
await u.page.getByRole('button', { name: /LogUserId/i }).click(); | ||
await expect(u.page.getByText(/\{\s*"userId"\s*:\s*"user_[^"]+"\s*\}/i)).toBeVisible(); | ||
|
||
const total = 1000 * 120; | ||
await page.waitForTimeout(total / 3); | ||
await page.waitForTimeout(total / 3); | ||
await u.po.userProfile.goTo(); | ||
await page.waitForTimeout(total / 3); | ||
await u.page.goToRelative(`/requires-re-verification`); | ||
await u.page.getByRole('button', { name: /LogUserId/i }).click(); | ||
await expect( | ||
u.page.getByText( | ||
/\{\s*"clerk_error"\s*:\s*\{\s*"type"\s*:\s*"forbidden"\s*,\s*"reason"\s*:\s*"reverification-mismatch"\s*,\s*"metadata"\s*:\s*\{\s*"reverification"\s*:\s*\{\s*"level"\s*:\s*"secondFactor"\s*,\s*"afterMinutes"\s*:\s*1\s*\}\s*\}\s*\}\s*\}/i, | ||
), | ||
).toBeVisible(); | ||
}); | ||
|
||
test(`reverification recovery from ${capitalize(type)}`, async ({ page, context }) => { | ||
test.setTimeout(270_000); | ||
const u = createTestUtils({ app, page, context }); | ||
|
||
await u.po.signIn.goTo(); | ||
await u.po.signIn.waitForMounted(); | ||
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password }); | ||
await u.po.expect.toBeSignedIn(); | ||
|
||
await u.po.organizationSwitcher.goTo(); | ||
await u.po.organizationSwitcher.waitForMounted(); | ||
await u.po.organizationSwitcher.waitForAnOrganizationToSelected(); | ||
|
||
await u.page.goToRelative(`/requires-re-verification`); | ||
await u.page.getByRole('button', { name: /LogUserId/i }).click(); | ||
await expect(u.page.getByText(/\{\s*"userId"\s*:\s*"user_[^"]+"\s*\}/i)).toBeVisible(); | ||
|
||
const total = 1000 * 120; | ||
await page.waitForTimeout(total / 3); | ||
await page.waitForTimeout(total / 3); | ||
await u.po.userProfile.goTo(); | ||
await page.waitForTimeout(total / 3); | ||
await u.page.goToRelative(`/action-with-use-reverification`); | ||
await u.po.expect.toBeSignedIn(); | ||
await u.page.getByRole('button', { name: /LogUserId/i }).click(); | ||
await u.po.userVerification.waitForMounted(); | ||
}); | ||
}); | ||
}, | ||
); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.