-
Notifications
You must be signed in to change notification settings - Fork 418
feat(clerk-js,clerk-react,types): Introduce state signals #6450
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 15 commits
415f283
fbd1f03
2d00936
e71897c
66f9a55
45e3607
57ef049
a3614d7
0ed14b6
d82111c
253bd9c
bd5b8e6
18d4b57
63c4a45
1bd1005
1e3bf21
e08b6aa
37f2eca
3ee4911
7c828e4
bc8457b
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 |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --- | ||
| --- | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,28 @@ | ||
| import { createEventBus } from '@clerk/shared/eventBus'; | ||
| import type { TokenResource } from '@clerk/types'; | ||
|
|
||
| import type { BaseResource } from './resources/Base'; | ||
|
|
||
| export const events = { | ||
| TokenUpdate: 'token:update', | ||
| UserSignOut: 'user:signOut', | ||
| EnvironmentUpdate: 'environment:update', | ||
| SessionTokenResolved: 'session:tokenResolved', | ||
| ResourceUpdate: 'resource:update', | ||
| ResourceError: 'resource:error', | ||
| } as const; | ||
|
|
||
| type TokenUpdatePayload = { token: TokenResource | null }; | ||
| export type ResourceUpdatePayload = { resource: BaseResource }; | ||
| export type ResourceErrorPayload = { resource: BaseResource; error: unknown }; | ||
|
|
||
| type InternalEvents = { | ||
| [events.TokenUpdate]: TokenUpdatePayload; | ||
| [events.UserSignOut]: null; | ||
| [events.EnvironmentUpdate]: null; | ||
| [events.SessionTokenResolved]: null; | ||
| [events.ResourceUpdate]: ResourceUpdatePayload; | ||
| [events.ResourceError]: ResourceErrorPayload; | ||
| }; | ||
|
|
||
| export const eventBus = createEventBus<InternalEvents>(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ import type { | |
| EmailCodeConfig, | ||
| EmailLinkConfig, | ||
| EnterpriseSSOConfig, | ||
| OAuthStrategy, | ||
| PassKeyConfig, | ||
| PasskeyFactor, | ||
| PhoneCodeConfig, | ||
|
|
@@ -27,6 +28,7 @@ import type { | |
| SamlConfig, | ||
| SignInCreateParams, | ||
| SignInFirstFactor, | ||
| SignInFutureResource, | ||
| SignInIdentifier, | ||
| SignInJSON, | ||
| SignInJSONSnapshot, | ||
|
|
@@ -66,6 +68,7 @@ import { | |
| clerkVerifyPasskeyCalledBeforeCreate, | ||
| clerkVerifyWeb3WalletCalledBeforeCreate, | ||
| } from '../errors'; | ||
| import { eventBus } from '../events'; | ||
| import { BaseResource, UserData, Verification } from './internal'; | ||
|
|
||
| export class SignIn extends BaseResource implements SignInResource { | ||
|
|
@@ -82,6 +85,21 @@ export class SignIn extends BaseResource implements SignInResource { | |
| createdSessionId: string | null = null; | ||
| userData: UserData = new UserData(null); | ||
|
|
||
| /** | ||
| * @experimental This experimental API is subject to change. | ||
| * | ||
| * An instance of `SignInFuture`, which has a different API than `SignIn`, intended to be used in custom flows. | ||
| */ | ||
| __internal_future: SignInFuture | null = new SignInFuture(this); | ||
|
|
||
| /** | ||
| * @internal Only used for internal purposes, and is not intended to be used directly. | ||
| * | ||
| * This property is used to provide access to underlying Client methods to `SignInFuture`, which wraps an instance | ||
| * of `SignIn`. | ||
| */ | ||
| __internal_basePost = this._basePost.bind(this); | ||
|
|
||
| constructor(data: SignInJSON | SignInJSONSnapshot | null = null) { | ||
| super(); | ||
| this.fromJSON(data); | ||
|
|
@@ -451,6 +469,8 @@ export class SignIn extends BaseResource implements SignInResource { | |
| this.createdSessionId = data.created_session_id; | ||
| this.userData = new UserData(data.user_data); | ||
| } | ||
|
|
||
| eventBus.emit('resource:update', { resource: this }); | ||
| return this; | ||
| } | ||
|
|
||
|
|
@@ -470,3 +490,132 @@ export class SignIn extends BaseResource implements SignInResource { | |
| }; | ||
| } | ||
| } | ||
|
|
||
| class SignInFuture implements SignInFutureResource { | ||
|
Member
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. We can add JSDocs now or later to the methods here, but we should do it eventually! Saves us time later too 😄 |
||
| emailCode = { | ||
| sendCode: this.sendEmailCode.bind(this), | ||
| verifyCode: this.verifyEmailCode.bind(this), | ||
| }; | ||
|
|
||
| constructor(readonly resource: SignIn) {} | ||
|
|
||
| get status() { | ||
| return this.resource.status; | ||
| } | ||
|
|
||
| async create(params: { | ||
| identifier?: string; | ||
| strategy?: OAuthStrategy | 'saml' | 'enterprise_sso'; | ||
| redirectUrl?: string; | ||
| actionCompleteRedirectUrl?: string; | ||
| }): Promise<{ error: unknown }> { | ||
| eventBus.emit('resource:error', { resource: this.resource, error: null }); | ||
| try { | ||
| await this.resource.__internal_basePost({ | ||
| path: this.resource.pathRoot, | ||
| body: params, | ||
| }); | ||
|
|
||
| return { error: null }; | ||
| } catch (err) { | ||
| eventBus.emit('resource:error', { resource: this.resource, error: err }); | ||
| return { error: err }; | ||
| } | ||
| } | ||
|
|
||
| async password({ identifier, password }: { identifier: string; password: string }): Promise<{ error: unknown }> { | ||
| eventBus.emit('resource:error', { resource: this.resource, error: null }); | ||
| try { | ||
| await this.resource.__internal_basePost({ | ||
| path: this.resource.pathRoot, | ||
| body: { identifier, password }, | ||
| }); | ||
| } catch (err) { | ||
| eventBus.emit('resource:error', { resource: this.resource, error: err }); | ||
| return { error: err }; | ||
| } | ||
|
|
||
| return { error: null }; | ||
| } | ||
|
|
||
| async sendEmailCode({ email }: { email: string }): Promise<{ error: unknown }> { | ||
| eventBus.emit('resource:error', { resource: this.resource, error: null }); | ||
| try { | ||
| if (!this.resource.id) { | ||
| await this.create({ identifier: email }); | ||
| } | ||
|
|
||
| const emailCodeFactor = this.resource.supportedFirstFactors?.find(f => f.strategy === 'email_code'); | ||
|
|
||
| if (!emailCodeFactor) { | ||
| throw new Error('Email code factor not found'); | ||
| } | ||
|
|
||
| const { emailAddressId } = emailCodeFactor; | ||
| await this.resource.__internal_basePost({ | ||
| body: { emailAddressId, strategy: 'email_code' }, | ||
| action: 'prepare_first_factor', | ||
| }); | ||
| } catch (err: unknown) { | ||
| eventBus.emit('resource:error', { resource: this.resource, error: err }); | ||
| return { error: err }; | ||
| } | ||
|
|
||
| return { error: null }; | ||
| } | ||
|
|
||
| async verifyEmailCode({ code }: { code: string }): Promise<{ error: unknown }> { | ||
| eventBus.emit('resource:error', { resource: this.resource, error: null }); | ||
| try { | ||
| await this.resource.__internal_basePost({ | ||
| body: { code, strategy: 'email_code' }, | ||
| action: 'attempt_first_factor', | ||
| }); | ||
| } catch (err: unknown) { | ||
| eventBus.emit('resource:error', { resource: this.resource, error: err }); | ||
| return { error: err }; | ||
| } | ||
|
|
||
| return { error: null }; | ||
| } | ||
|
|
||
| async sso({ | ||
| flow = 'auto', | ||
| strategy, | ||
| redirectUrl, | ||
| redirectUrlComplete, | ||
| }: { | ||
| flow?: 'auto' | 'modal'; | ||
| strategy: OAuthStrategy | 'saml' | 'enterprise_sso'; | ||
| redirectUrl: string; | ||
| redirectUrlComplete: string; | ||
| }): Promise<{ error: unknown }> { | ||
| eventBus.emit('resource:error', { resource: this.resource, error: null }); | ||
| try { | ||
| if (flow !== 'auto') { | ||
| throw new Error('modal flow is not supported yet'); | ||
| } | ||
|
|
||
| const redirectUrlWithAuthToken = SignIn.clerk.buildUrlWithAuth(redirectUrl); | ||
|
|
||
| if (!this.resource.id) { | ||
| await this.create({ | ||
| strategy, | ||
| redirectUrl: redirectUrlWithAuthToken, | ||
| actionCompleteRedirectUrl: redirectUrlComplete, | ||
| }); | ||
| } | ||
|
|
||
| const { status, externalVerificationRedirectURL } = this.resource.firstFactorVerification; | ||
|
|
||
| if (status === 'unverified' && externalVerificationRedirectURL) { | ||
| windowNavigate(externalVerificationRedirectURL); | ||
| } | ||
| } catch (err: unknown) { | ||
| eventBus.emit('resource:error', { resource: this.resource, error: err }); | ||
| return { error: err }; | ||
| } | ||
|
|
||
| return { error: null }; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { computed, signal } from 'alien-signals'; | ||
|
|
||
| import type { SignIn } from './resources/SignIn'; | ||
|
|
||
| export const signInSignal = signal<{ resource: SignIn | null }>({ resource: null }); | ||
| export const signInErrorSignal = signal<{ errors: unknown }>({ errors: null }); | ||
|
|
||
| export const signInComputedSignal = computed(() => { | ||
| const signIn = signInSignal().resource; | ||
| const errors = signInErrorSignal().errors; | ||
|
|
||
| if (!signIn) { | ||
| return { errors: null, signIn: null }; | ||
| } | ||
|
|
||
| return { errors, signIn: signIn.__internal_future }; | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type { State as StateInterface } from '@clerk/types'; | ||
| import { computed, effect } from 'alien-signals'; | ||
|
|
||
| import { eventBus } from './events'; | ||
| import type { BaseResource } from './resources/Base'; | ||
| import { SignIn } from './resources/SignIn'; | ||
| import { signInComputedSignal, signInErrorSignal, signInSignal } from './signals'; | ||
|
|
||
| export class State implements StateInterface { | ||
|
Member
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. JSDoc would be good here 👀 what's it for? Maybe it belongs on the
Member
Author
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. I put it on the |
||
| signInResourceSignal = signInSignal; | ||
| signInErrorSignal = signInErrorSignal; | ||
| signInSignal = signInComputedSignal; | ||
|
|
||
| __internal_effect = effect; | ||
| __internal_computed = computed; | ||
|
|
||
| constructor() { | ||
| eventBus.on('resource:update', this.onResourceUpdated); | ||
| eventBus.on('resource:error', this.onResourceError); | ||
| } | ||
|
|
||
| private onResourceError = (payload: { resource: BaseResource; error: unknown }) => { | ||
| if (payload.resource instanceof SignIn) { | ||
| this.signInErrorSignal({ errors: payload.error }); | ||
| } | ||
| }; | ||
|
|
||
| private onResourceUpdated = (payload: { resource: BaseResource }) => { | ||
| if (payload.resource instanceof SignIn) { | ||
| this.signInResourceSignal({ resource: payload.resource }); | ||
| } | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import type { SignInFutureResource } from '@clerk/types'; | ||
| import { useCallback, useSyncExternalStore } from 'react'; | ||
|
|
||
| import { useIsomorphicClerkContext } from '../contexts/IsomorphicClerkContext'; | ||
| import { useAssertWrappedByClerkProvider } from './useAssertWrappedByClerkProvider'; | ||
|
|
||
| function useClerkSignal(signal: 'signIn'): { errors: unknown; signIn: SignInFutureResource | null } | null { | ||
| useAssertWrappedByClerkProvider('useSignInSignal'); | ||
|
|
||
| const clerk = useIsomorphicClerkContext(); | ||
|
|
||
| const subscribe = useCallback( | ||
| (callback: () => void) => { | ||
| if (!clerk.loaded || !clerk.__internal_state) { | ||
| return () => {}; | ||
| } | ||
|
|
||
| return clerk.__internal_state.__internal_effect(() => { | ||
| switch (signal) { | ||
| case 'signIn': | ||
| // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- we know that the state is defined | ||
| clerk.__internal_state!.signInSignal(); | ||
| break; | ||
| default: | ||
| throw new Error(`Unknown signal: ${signal}`); | ||
| } | ||
| callback(); | ||
| }); | ||
| }, | ||
| [clerk, clerk.loaded, clerk.__internal_state], | ||
| ); | ||
| const getSnapshot = useCallback(() => { | ||
| if (!clerk.__internal_state) { | ||
| return null; | ||
| } | ||
|
|
||
| switch (signal) { | ||
| case 'signIn': | ||
| return clerk.__internal_state.signInSignal(); | ||
| default: | ||
| throw new Error(`Unknown signal: ${signal}`); | ||
| } | ||
| }, [clerk.__internal_state]); | ||
|
|
||
| const value = useSyncExternalStore(subscribe, getSnapshot, getSnapshot); | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| export function useSignInSignal() { | ||
| return useClerkSignal('signIn'); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.