@asgardeo/nextjs
Version:
Next.js implementation of Asgardeo JavaScript SDK.
96 lines • 3.38 kB
JavaScript
/**
* 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.
*/
'use client';
import { jsx as _jsx } from "react/jsx-runtime";
import { AsgardeoRuntimeError } from '@asgardeo/node';
import { forwardRef, useState } from 'react';
import { BaseSignUpButton, useTranslation } from '@asgardeo/react';
import useAsgardeo from '../../../contexts/Asgardeo/useAsgardeo';
import { useRouter } from 'next/navigation';
/**
* SignUpButton component that supports both render props and traditional props patterns.
* It redirects the user to the Asgardeo sign-up page configured for the application.
*
* @remarks This component is only supported in browser based React applications (CSR).
*
* @example Using render props pattern
* ```tsx
* <SignUpButton>
* {({ signUp, isLoading }) => (
* <button onClick={signUp} disabled={isLoading}>
* {isLoading ? 'Creating Account...' : 'Create Account'}
* </button>
* )}
* </SignUpButton>
* ```
*
* @example Using traditional props pattern
* ```tsx
* <SignUpButton className="custom-button">Create Account</SignUpButton>
* ```
*
* @example Using component-level preferences
* ```tsx
* <SignUpButton
* preferences={{
* i18n: {
* bundles: {
* 'en-US': {
* translations: {
* 'buttons.signUp': 'Custom Sign Up Text'
* }
* }
* }
* }
* }}
* >
* Custom Sign Up
* </SignUpButton>
* ```
*/
const SignUpButton = forwardRef(({ children, onClick, preferences, ...rest }, ref) => {
const { signUp, signUpUrl } = useAsgardeo();
const router = useRouter();
const { t } = useTranslation(preferences?.i18n);
const [isLoading, setIsLoading] = useState(false);
const handleSignUp = async (e) => {
try {
setIsLoading(true);
// If a custom `signUpUrl` is provided, use it for navigation.
if (signUpUrl) {
router.push(signUpUrl);
}
else {
signUp && (await signUp());
}
if (onClick) {
onClick(e);
}
}
catch (error) {
throw new AsgardeoRuntimeError(`Sign up failed: ${error instanceof Error ? error.message : String(error)}`, 'SignUpButton-handleSignUp-RuntimeError-001', 'nextjs', 'Something went wrong while trying to sign up. Please try again later.');
}
finally {
setIsLoading(false);
}
};
return (_jsx(BaseSignUpButton, { ref: ref, onClick: handleSignUp, isLoading: isLoading, signUp: handleSignUp, preferences: preferences, ...rest, children: children ?? t('elements.buttons.signup.text') }));
});
SignUpButton.displayName = 'SignUpButton';
export default SignUpButton;
//# sourceMappingURL=SignUpButton.js.map