Skip to content

User Registration Workflow

This example demonstrates how to create a workflow that handles user registration, including input validation, account creation, and sending a welcome notification.

  1. Define Input Schema & Workflow

    First, define the expected input for user registration using a schema (e.g., Valibot). Then, start the workflow definition, associating it with this schema.

    import * as v from '@identity-flow/sdk/valibot';
    import { defineWorkflow } from '@identity-flow/sdk';
    const UserSchema = v.object({
    email: v.string([v.email()])
    password: v.string([v.minLength(8)]),
    name: v.string(),
    });
    export default defineWorkflow('user-registration', { schema: UserSchema }, async (flow) => {
    // Workflow steps will go here
    });
  2. Validate User Data (Implicit)

    IdentityFlow automatically validates flow.params against the UserSchema at the start. If validation fails, the workflow won’t proceed. For explicit validation or transformation as a step:

    // Inside the async (flow) => { ... }
    const validatedUser = await flow.do('validate user input', async () => {
    // Assuming validateUserData performs any additional checks or transformations
    // flow.params is already schema-validated at this point
    return validateUserData(flow.params);
    });

    Note: validateUserData would be your custom validation/transformation logic if needed beyond the initial schema.

  3. Create User Account

    Perform the actual user creation, perhaps an API call. Configure retries for transient network issues.

    // Inside the async (flow) => { ... }
    const user = await flow.do(
    'create user account',
    { retries: { limit: 3, backoff: 'exponential' } },
    async () => {
    // Replace with your actual user creation logic (e.g., API call)
    return createUserInDatabase(validatedUser);
    },
    );
  4. Send Welcome Email

    After successful account creation, send a welcome email. This step also includes retries.

    // Inside the async (flow) => { ... }
    await flow.do(
    'send welcome email',
    { retries: { limit: 5, delay: '30 seconds' } },
    async () => {
    // Replace with your actual email sending logic
    await sendNotificationEmail(user.email, 'welcome');
    },
    );
  5. Complete the Workflow

    Return a result indicating the outcome.

    // Inside the async (flow) => { ... }
    return {
    userId: user.id,
    status: 'REGISTRATION_COMPLETED'
    };
import * as v from '@identity-flow/sdk/valibot';
import { defineWorkflow } from '@identity-flow/sdk';
// --- Mock external functions for demonstration ---
async function validateUserData(data: any) {
console.log('Validating user data:', data);
// In a real scenario, add more complex validation if needed
return data; // Assuming basic schema validation is enough
}
async function createUserInDatabase(data: any) {
console.log('Creating user in database:', data);
return { id: 'user-' + Math.random().toString(36).substring(7), email: data.email };
}
async function sendNotificationEmail(email: string, template: string) {
console.log(`Sending ${template} email to ${email}`);
// Simulate email sending that might fail occasionally
if (Math.random() < 0.1) throw new Error('Simulated email send failure');
}
// --- End mock functions ---
const UserSchema = v.object({
email: v.string([v.email()])
password: v.string([v.minLength(8)]),
name: v.string(),
});
export default defineWorkflow('user-registration-example', { schema: UserSchema }, async (flow) => {
flow.log('Starting user registration workflow for:', flow.params.email);
const validatedUser = await flow.do('validate user input', async () => {
return validateUserData(flow.params);
});
const user = await flow.do(
'create user account',
{ retries: { limit: 3, backoff: 'exponential' } },
async () => {
return createUserInDatabase(validatedUser);
}
);
flow.log('User account created:', user.id);
await flow.do('send welcome email',
{ retries: { limit: 5, delay: '30 seconds' } },
async () => {
await sendNotificationEmail(user.email, 'welcome');
}
);
flow.log('Welcome email step processed for:', user.email);
return {
userId: user.id,
status: 'REGISTRATION_COMPLETED'
};
});

Explore other examples or dive deeper into specific concepts: