AWS Amplify Gen2 Custom Fields during Authenticator Signup

0

I am developing an app using Amplify Gen2 + Vue 3

In my app, i am using standard Authenticator Signup Process.. Email and Password + Verification code..

I added some custom attributes to my users in cognito. One of them is custom:type

I want to extend the signup form to include a drop down field with 2 options "TypeA" and "TypeB" as required. And on submit i want to save this value to the user too..

1 Answer
0

Hi, In order to add custom fields to the Amplify Gen2 Authenticator signup form in Vue 3:

  1. First, customize the signup fields using the components prop:
<template>
  <authenticator :components="components">
    <template v-slot="{ user, signOut }">
      <!-- Your authenticated app content -->
    </template>
  </authenticator>
</template>

<script setup>
import { Authenticator } from '@aws-amplify/ui-vue';
import { defineComponent, ref } from 'vue';

const components = {
  SignUp: {
    FormFields() {
      return (
        <>
          {/* Default fields */}
          <authenticator-sign-up-form-fields />
          
          {/* Custom type field */}
          <div class="amplify-flex amplify-field">
            <label class="amplify-label">User Type</label>
            <select 
              name="custom:type"
              class="amplify-select"
              required
            >
              <option value="TypeA">Type A</option>
              <option value="TypeB">Type B</option>
            </select>
          </div>
        </>
      );
    },
  },
};
</script>

<style>
/* Add any custom styles here */
</style>
  1. Handle the custom field during signup by using the services prop:
<template>
  <authenticator :services="services">
    <template v-slot="{ user, signOut }">
      <!-- Your authenticated app content -->
    </template>
  </authenticator>
</template>

<script setup>
import { Authenticator } from '@aws-amplify/ui-vue';
import { defineComponent } from 'vue';

const services = {
  async handleSignUp(formData) {
    let { username, password, attributes } = formData;
    
    // Add the custom type to attributes
    attributes['custom:type'] = formData['custom:type'];
    
    return {
      username,
      password,
      attributes
    };
  }
};
</script>
  1. Make sure your Cognito User Pool is configured correctly:
// amplify/backend/auth/userPoolConfig.json
{
  "aws_cognito_user_pool_id": "YOUR_USER_POOL_ID",
  "aws_user_pools_web_client_id": "YOUR_CLIENT_ID",
  "userAttributes": {
    "custom:type": {
      "required": true,
      "mutable": true
    }
  }
}
  1. Complete implementation with validation:
<template>
  <authenticator 
    :components="components"
    :services="services"
    :validation-errors="validationErrors"
  >
    <template v-slot="{ user, signOut }">
      <div>
        <h1>Welcome {{ user.username }}</h1>
        <p>User Type: {{ user.attributes['custom:type'] }}</p>
        <button @click="signOut">Sign Out</button>
      </div>
    </template>
  </authenticator>
</template>

<script setup>
import { Authenticator } from '@aws-amplify/ui-vue';
import { ref } from 'vue';

const validationErrors = ref({});

const components = {
  SignUp: {
    FormFields() {
      return (
        <>
          <authenticator-sign-up-form-fields />
          <div class="amplify-flex amplify-field">
            <label class="amplify-label">User Type</label>
            <select 
              name="custom:type"
              class="amplify-select"
              required
              onChange={(e) => validateType(e.target.value)}
            >
              <option value="">Select Type</option>
              <option value="TypeA">Type A</option>
              <option value="TypeB">Type B</option>
            </select>
            {validationErrors.value['custom:type'] && (
              <div class="amplify-error">
                {validationErrors.value['custom:type']}
              </div>
            )}
          </div>
        </>
      );
    },
  },
};

const services = {
  async handleSignUp(formData) {
    // Validate type
    if (!formData['custom:type']) {
      validationErrors.value['custom:type'] = 'User Type is required';
      throw new Error('User Type is required');
    }

    let { username, password, attributes } = formData;
    attributes['custom:type'] = formData['custom:type'];

    return {
      username,
      password,
      attributes
    };
  }
};

const validateType = (value) => {
  if (!value) {
    validationErrors.value['custom:type'] = 'User Type is required';
  } else {
    delete validationErrors.value['custom:type'];
  }
};
</script>

<style>
.amplify-select {
  width: 100%;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.amplify-error {
  color: red;
  font-size: 0.875rem;
  margin-top: 4px;
}
</style>

For more information, see:

AWS
answered 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions