Skip to content

Commit

Permalink
feat(elements): Compilation of updates (#2790)
Browse files Browse the repository at this point in the history
* chore(elements): Interim commit

* chore(elements): Interim commit

* chore(elements): Interim commit

* feat(elements): Large refactor; Interim commit

* chore(elements): Update folder architecture (pt 1)

* refactor(elements): Split out machines; Interim commit

* chore(elements): Rename Continue to Verify

* fix(elements): Account for resources in NEXT events

* chore(elements): Clean up

* refactor(elements): Interim commit

* chore(elements): Update SignUp to match SignIn

* chore(elements): Updated logs

* chore(elements): /errors/error -> /errors

* chore(elements): Add changeset

* feat(elements): Handle SSO callbacks (#2791)

* feat(elements): Handle SSO callbacks

* fix(elements): Use constants

---------

Co-authored-by: LekoArts <[email protected]>

* chore(elements): Use other imports

* fix(elements): Use constants

* fix(elements): Typo

* chore(elements): Add test

* chore(elements): Bump version

---------

Co-authored-by: LekoArts <[email protected]>
  • Loading branch information
tmilewski and LekoArts authored Feb 13, 2024
1 parent a905864 commit fc23339
Show file tree
Hide file tree
Showing 79 changed files with 3,503 additions and 585 deletions.
2 changes: 2 additions & 0 deletions .changeset/ten-spies-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
45 changes: 45 additions & 0 deletions packages/elements/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,48 @@ npm run e2e -- --headed
# Specific Tests: https://playwright.dev/docs/running-tests#run-specific-tests
npm run e2e -- e2e/elements.spec.ts
```

### Flows

Flows per `clerk-js` UI components

#### `<SignInRoutes>`

```
/sign-in
/factor-one <SignInFactorOne />
/factor-two <SignInFactorTwo />
/reset-password <ResetPassword />
/reset-password-success <ResetPasswordSuccess />
/sso-callback <SignInSSOCallback {...} />
/choose <SignInAccountSwitcher />
/verify <SignInEmailLinkFlowComplete {...} />
/ <SignInStart />
[ELSE] <RedirectToSignIn />
```

#### `<SignUpRoutes>`

```
/sign-up
/verify-email-address <SignUpVerifyEmail /> [Guarded: Boolean(clerk.client.signUp.emailAddress)]
/verify-phone-number <SignUpVerifyPhone /> [Guarded: Boolean(clerk.client.signUp.phoneNumber)]
/sso-callback <SignUpSSOCallback {...} />
/verify <SignUpEmailLinkFlowComplete {...} />
/continue
/verify-email-address <SignUpVerifyEmail /> [Guarded: Boolean(clerk.client.signUp.emailAddress)]
/verify-phone-number <SignUpVerifyPhone /> [Guarded: Boolean(clerk.client.signUp.phoneNumber)]
/ <SignUpContinue />
/ <SignUpStart />
[ELSE] <RedirectToSignUp />
/sign-up
/verify-email-address <SignUpVerifyEmail /> [Guarded: Boolean(clerk.client.signUp.emailAddress)]
/verify-phone-number <SignUpVerifyPhone /> [Guarded: Boolean(clerk.client.signUp.phoneNumber)]
/sso-callback <SignUpSSOCallback {...} />
/verify <SignUpEmailLinkFlowComplete {...} />
/continue
/ <SignUpContinue />
/ <SignUpStart />
[ELSE] <RedirectToSignUp />
```
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
'use client';

import { GlobalError, Submit } from '@clerk/elements/common';
import {
Continue,
Factor,
SignIn,
SocialProvider,
SocialProviderIcon,
Start,
Verification,
} from '@clerk/elements/sign-in';

import { H1, H2, H3, HR as Hr, P } from '@/components/design';
import { SignIn, SocialProvider, SocialProviderIcon, Start, Verification, Verify } from '@clerk/elements/sign-in';

import { H1, H3, HR as Hr, P } from '@/components/design';
import { CustomField, CustomSubmit } from '@/components/form';
import { SignInDebug } from '@/components/sign-in-debug';

export default function SignInPage() {
return (
Expand Down Expand Up @@ -71,13 +62,11 @@ export default function SignInPage() {
</div>
</Start>

<Continue>
<Verify>
<div className='flex gap-6 flex-col'>
<H1>STRATEGIES (FIRST/SECOND FACTOR)</H1>
<H1>VERFIY</H1>

<H2>
Current Factor: <Factor first>First</Factor> | <Factor second>Second</Factor>
</H2>
<GlobalError className='block text-red-400 font-mono' />

<Verification name='password'>
<CustomField
Expand Down Expand Up @@ -118,10 +107,10 @@ export default function SignInPage() {
</Submit>
</Verification>
</div>
</Continue>
</Verify>
</div>

<SignInDebug />
{/* <SignInDebug /> */}
</SignIn>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import {

import { H1, HR as Hr } from '@/components/design';
import { CustomField, CustomSubmit } from '@/components/form';
import { SignUpDebug } from '@/components/sign-up-debug';

export default function SignUpPage() {
return (
<SignUp>
<div className='m-auto w-max text-sm'>
<Start>
<div className='flex flex-col items-center justify-center gap-12'>
<H1>SIGN UP - START</H1>
<H1>Sign Up</H1>

<div className='flex flex-col items-stretch justify-center gap-2'>
<SocialProvider
Expand Down Expand Up @@ -58,47 +57,33 @@ export default function SignUpPage() {
label='Email'
name='emailAddress'
/>

<CustomField
label='Password'
name='password'
/>

<CustomSubmit>Sign Up</CustomSubmit>
</div>
</div>
</Start>

<Continue>
<div className='flex flex-col items-center justify-center gap-12'>
<H1>SIGN UP - CONTINUE</H1>
<H1>Please enter additional information:</H1>

<GlobalError className='block text-red-400 font-mono' />
<GlobalError className='block text-red-400 font-mono' />

<div className='flex gap-6 flex-col'>
<CustomField
label='First Name'
name='firstName'
/>
<CustomField
label='Last Name'
name='lastName'
/>
<CustomField
label='Username'
name='username'
/>
<CustomField
label='Phone'
name='phoneNumber'
/>
<CustomField
label='Phone Number'
name='phoneNumber'
/>

<CustomSubmit>Submit</CustomSubmit>
</div>
</div>
<CustomSubmit>Sign Up</CustomSubmit>
</Continue>

<Verify>
<H1>SIGN UP - VERIFY</H1>
<H1>Verify your information:</H1>

<GlobalError className='block text-red-400 font-mono' />

<Verification name='code'>
<CustomField
Expand All @@ -113,7 +98,7 @@ export default function SignUpPage() {
</Verify>
</div>

<SignUpDebug />
{/* <SignUpDebug /> */}
</SignUp>
);
}
5 changes: 4 additions & 1 deletion packages/elements/examples/nextjs/next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};
const nextConfig = {
// Disable React strict mode when using the state machine inspector
reactStrictMode: process.env['NEXT_PUBLIC_CLERK_ELEMENTS_DEBUG'] !== 'true',
};

module.exports = nextConfig;
2 changes: 1 addition & 1 deletion packages/elements/examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@radix-ui/react-form": "^0.0.3",
"clsx": "^2.0.0",
"framer-motion": "^11.0.2",
"next": "^14.1.0",
"next": "~14.0.0",
"react": "^18",
"react-dom": "^18"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/elements/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@clerk/elements",
"version": "0.1.2",
"version": "0.1.3",
"description": "Clerk Elements",
"keywords": [
"clerk",
Expand Down
3 changes: 3 additions & 0 deletions packages/elements/src/internals/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export const SSO_CALLBACK_PATH_ROUTE = '/sso-callback';
export const MAGIC_LINK_VERIFY_PATH_ROUTE = '/verify';

export const SIGN_IN_DEFAULT_BASE_PATH = '/sign-in';
export const SIGN_UP_DEFAULT_BASE_PATH = '/sign-up';
5 changes: 2 additions & 3 deletions packages/elements/src/internals/machines/form/form.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isClerkAPIResponseError } from '@clerk/shared/error';
import type { MachineContext } from 'xstate';
import { assign, enqueueActions, setup } from 'xstate';

import { ClerkElementsError, ClerkElementsFieldError } from '~/internals/errors/error';
import { ClerkElementsError, ClerkElementsFieldError } from '~/internals/errors';

import type { FieldDetails, FormFields } from './form.types';

Expand Down Expand Up @@ -69,8 +69,7 @@ export const FormMachine = setup({
const fields: Record<string, ClerkElementsFieldError[]> = {};
const globalErrors: ClerkElementsError[] = [];

for (const error of event.error.errors) {
// TODO: Why are we sending in snake and receiving camel?
for (const error of event.error.errors || [event.error]) {
const name = snakeToCamel(error.meta?.paramName);

if (!name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ClerkElementsFieldError } from '~/internals/errors/error';
import type { ClerkElementsFieldError } from '~/internals/errors';

export type FieldDetails = {
name?: string;
Expand Down
4 changes: 3 additions & 1 deletion packages/elements/src/internals/machines/shared.actors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import type { Clerk, HandleOAuthCallbackParams, HandleSamlCallbackParams, Loaded
import type { AnyEventObject, EventObject } from 'xstate';
import { fromCallback, fromPromise } from 'xstate';

import { ClerkElementsRuntimeError } from '~/internals/errors/error';
import { ClerkElementsRuntimeError } from '~/internals/errors';
import { ClerkJSNavigationEvent, isClerkJSNavigationEvent } from '~/internals/machines/utils/clerkjs';

// TODO: Remove
/** @deprecated Use clerkLoader instead */
export const waitForClerk = fromPromise<LoadedClerk, Clerk | LoadedClerk>(({ input: clerk }) => {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -43,6 +44,7 @@ export type HandleRedirectCallbackParams<T = Required<HandleOAuthCallbackParams

export type HandleRedirectCallbackInput = LoadedClerk;

// TODO: Remove
/**
* This function hijacks handleRedirectCallback from ClerkJS to handle navigation events
* from the state machine.
Expand Down
12 changes: 12 additions & 0 deletions packages/elements/src/internals/machines/sign-in/machines/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export {
SignInFirstFactorMachine,
SignInFirstFactorMachineId,
SignInSecondFactorMachine,
SignInSecondFactorMachineId,
} from './verification.machine';
export { SignInRouterMachine, SignInRouterMachineId } from './router.machine';
export { SignInStartMachine, SignInStartMachineId } from './start.machine';

export type { TSignInRouterMachine } from './router.machine';
export type { TSignInStartMachine } from './start.machine';
export type { TSignInFirstFactorMachine, TSignInSecondFactorMachine } from './verification.machine';
Loading

0 comments on commit fc23339

Please sign in to comment.