UNPKG

@asgardeo/react

Version:
145 lines (144 loc) 4.34 kB
/** * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com). * * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import { FC, ReactNode } from 'react'; import { BaseSignInProps } from './BaseSignIn'; import { EmbeddedFlowComponentV2 as EmbeddedFlowComponent, EmbeddedSignInFlowRequestV2 } from '@asgardeo/browser'; /** * Render props function parameters */ export interface SignInRenderProps { /** * Function to manually initialize the flow */ initialize: () => Promise<void>; /** * Function to submit authentication data (primary) */ onSubmit: (payload: EmbeddedSignInFlowRequestV2) => Promise<void>; /** * Loading state indicator */ isLoading: boolean; /** * Whether the flow has been initialized */ isInitialized: boolean; /** * Current flow components */ components: EmbeddedFlowComponent[]; /** * Current error if any */ error: Error | null; } /** * Props for the SignIn component. * Matches the interface from the main SignIn component for consistency. */ export type SignInProps = { /** * Custom CSS class name for the form container. */ className?: string; /** * Callback function called when authentication is successful. * @param authData - The authentication data returned upon successful completion. */ onSuccess?: (authData: Record<string, any>) => void; /** * Callback function called when authentication fails. * @param error - The error that occurred during authentication. */ onError?: (error: Error) => void; /** * Theme variant for the component. */ variant?: BaseSignInProps['variant']; /** * Size variant for the component. */ size?: 'small' | 'medium' | 'large'; /** * Render props function for custom UI */ children?: (props: SignInRenderProps) => ReactNode; }; /** * A component-driven SignIn component that provides authentication flow with pre-built styling. * This component handles the flow API calls for authentication and delegates UI logic to BaseSignIn. * It automatically transforms simple input-based responses into component-driven UI format. * * @example * // Default UI * ```tsx * import { SignIn } from '@asgardeo/react/component-driven'; * * const App = () => { * return ( * <SignIn * onSuccess={(authData) => { * console.log('Authentication successful:', authData); * }} * onError={(error) => { * console.error('Authentication failed:', error); * }} * size="medium" * variant="outlined" * /> * ); * }; * ``` * * @example * // Custom UI with render props * ```tsx * import { SignIn } from '@asgardeo/react/component-driven'; * * const App = () => { * return ( * <SignIn * onSuccess={(authData) => console.log('Success:', authData)} * onError={(error) => console.error('Error:', error)} * > * {({signIn, isLoading, components, error, isInitialized}) => ( * <div className="custom-signin"> * <h1>Custom Sign In</h1> * {!isInitialized ? ( * <p>Initializing...</p> * ) : error ? ( * <div className="error">{error.message}</div> * ) : ( * <form onSubmit={(e) => { * e.preventDefault(); * signIn({inputs: {username: 'user', password: 'pass'}}); * }}> * <button type="submit" disabled={isLoading}> * {isLoading ? 'Signing in...' : 'Sign In'} * </button> * </form> * )} * </div> * )} * </SignIn> * ); * }; * ``` */ declare const SignIn: FC<SignInProps>; export default SignIn;