Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Choose your framework/language

Page updated May 2, 2024

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:

amplify/auth/resource.ts
1import { defineAuth } from "@aws-amplify/backend"
2
3/**
4 * Define and configure your auth resource
5 * @see https://docs.amplify.aws/gen2/build-a-backend/auth
6 */
7export const auth = defineAuth({
8 loginWith: {
9 email: true,
10 },
11})

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.

Note: At a minimum you will need to pass a loginWith value to set up how your users sign in to your app. Signing in with email and password is configured by default if you do not provide any value.

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.

Terminal
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-react-native library:

Terminal
npm add \
@aws-amplify/react-native \
@aws-amplify/ui-react-native \
aws-amplify \
@react-native-community/netinfo \
@react-native-async-storage/async-storage \
react-native-safe-area-context \
react-native-get-random-values

If your project will support Federated Sign In using the React Native Authenticator the @aws-amplify/rtn-web-browser package is also required:

Terminal
npm add @aws-amplify/rtn-web-browser

Then install the iOS cocoapods by running:

Terminal
npx pod-install

For calling native libraries and platform dependencies from Expo, you need to run the prebuild command for generating the folders for related platforms.

Terminal
npx expo prebuild

Next, update the App.tsx file with the following to set up the authentication flow:

1import React from "react";
2import { Button, View, StyleSheet, SafeAreaView } from "react-native";
3import { Amplify } from "aws-amplify";
4import { Authenticator, useAuthenticator } from "@aws-amplify/ui-react-native";
5import outputs from "./amplify_outputs.json";
6
7Amplify.configure(outputs);
8
9const SignOutButton = () => {
10 const { signOut } = useAuthenticator();
11
12 return (
13 <View style={styles.signOutButton}>
14 <Button title="Sign Out" onPress={signOut} />
15 </View>
16 );
17};
18
19const App = () => {
20 return (
21 <Authenticator.Provider>
22 <Authenticator>
23 <SafeAreaView>
24 <SignOutButton />
25 </SafeAreaView>
26 </Authenticator>
27 </Authenticator.Provider>
28 );
29};
30
31const styles = StyleSheet.create({
32 signOutButton: {
33 alignSelf: "flex-end",
34 },
35});
36
37export default App;

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: