Set up Amplify Auth
Amplify Auth is powered by Amazon Cognito. Cognito is a robust user directory service that handles user registration, authentication, account recovery, and other operations. Review the concepts to learn more.
To get started with defining your authentication resource, open or create the auth resource file:
import { defineAuth } from "@aws-amplify/backend"
/** * Define and configure your auth resource * @see https://docs.amplify.aws/gen2/build-a-backend/auth */export const auth = defineAuth({ loginWith: { email: true, },})
By default, your auth resource is scaffolded using email
as the default login mechanism. You can also configure your auth resource to allow signing in with phone numbers or an external provider such as Google, Facebook, Amazon, or Sign in with Apple.
Deploy auth resource
After you have chosen and defined your authentication resource, run the following command to create your resource in your personal cloud sandbox.
npx ampx sandbox
After a successful deployment, this command also generates an outputs file (amplify_outputs.json
) to enable your frontend app to connect to your backend resources. The values you configure in your backend authentication resource are set in the generated outputs file to automatically configure the frontend Authenticator connected component
.
Connect your application code to your auth resource
Creating and correctly implementing the sign-in flow can be challenging and time-consuming. Amplify's Authenticator UI component streamlines this by enabling you to rapidly build the entire authentication flow for your app. The component works seamlessly with configuration in amplify/auth/resource.ts
to automatically connect with your backend resources.
Amplify has pre-built UI components for React, Vue, Angular, React Native, Swift, Android, and Flutter. In this guide, we are focusing on those for web applications.
First, install the @aws-amplify/ui-vue
library:
npm add @aws-amplify/ui-vue
Next, open src/App.vue and add the Authenticator
component.
Authenticator
The Authenticator
component offers a simple way to add authentication flows into your app. This component encapsulates an authentication workflow in the framework of your choice and is backed by your backend Auth resources. Authenticator
passes the user
info and signOut
function to the inner template.
<script setup> import { Authenticator } from "@aws-amplify/ui-vue"; import "@aws-amplify/ui-vue/styles.css";
import { Amplify } from 'aws-amplify'; import outputs from '../amplify_outputs.json';
Amplify.configure(outputs);</script>
<template> <authenticator> <template v-slot="{ user, signOut }"> <h1>Hello {{ user.username }}!</h1> <button @click="signOut">Sign Out</button> </template> </authenticator></template>
First, install the @aws-amplify/ui-components
library:
npm add @aws-amplify/ui-components
Now open src/main.ts and add the following below your last import:
import '@aws-amplify/ui-components';import { applyPolyfills, defineCustomElements} from '@aws-amplify/ui-components/loader';import Vue from 'vue';
Vue.config.ignoredElements = [/amplify-\w*/];
applyPolyfills().then(() => { defineCustomElements(window);});
Next, open src/App.ts and add the amplify-authenticator
component.
amplify-authenticator
The amplify-authenticator
component offers a simple way to add authentication flows into your app. This component encapsulates an authentication workflow in the framework of your choice and is backed by your backend Auth resources. The optional amplify-sign-out
component is available if you would like to render a sign-out button.
<template> <amplify-authenticator> <div> My App <amplify-sign-out></amplify-sign-out> </div> </amplify-authenticator></template>
Once you add the Authenticator component to your app, you can test the sign-up, sign-in, and sign-out functionality. You can also customize the Authenticator connected component to adjust colors and styling as needed.
Next steps
Now that you have completed setting up authentication in your Amplify app with email and password, you may also want to add some additional features. We recommend you learn more about: