AWS Amplify with React Native to implement authentication using Amazon Cognito

Sanjana Human In Tech
2 min readDec 19, 2023

--

Using AWS services in a React Native application allows you to leverage cloud services for various functionalities such as authentication, storage, databases, and more.

Below is a basic example demonstrating how to use AWS Amplify with React Native to implement authentication using Amazon Cognito.

AWS Cognito for federated identities

Step 1: Install AWS Amplify CLI

First, install the Amplify CLI globally:

npm install -g @aws-amplify/cli

Step 2: Configure Amplify in Your React Native Project

Navigate to your React Native project directory and configure Amplify:

amplify configure

Follow the prompts to set up an AWS account, create a new IAM user, and configure the Amplify CLI.

Step 3: Install Amplify Libraries in Your React Native Project

Install the necessary Amplify libraries in your React Native project:

npm install aws-amplify @react-native-community/netinfo @react-native-community/async-storage

Step 4: Initialize Amplify

Initialize Amplify in your project:

amplify init

Step 5: Add Authentication

Add authentication to your React Native app with Amplify:

amplify add auth

Follow the prompts to configure user authentication settings.

Step 6: Deploy Authentication

Deploy the authentication configuration to your AWS account:

amplify push

Step 7: Use AWS Amplify in Your React Native App

Now, you can use Amplify in your React Native components. Here’s an example using the Auth module for user authentication:

// App.js
import React, { useState, useEffect } from 'react';
import { View, Text, Button } from 'react-native';
import { Auth } from 'aws-amplify';

const App = () => {
const [user, setUser] = useState(null);

useEffect(() => {
checkUser();
}, []);

const checkUser = async () => {
try {
const userData = await Auth.currentAuthenticatedUser();
setUser(userData);
} catch (error) {
console.log('User not signed in');
}
};

const signOut = async () => {
try {
await Auth.signOut();
setUser(null);
} catch (error) {
console.error('Error signing out:', error);
}
};

return (
<View>
{user ? (
<View>
<Text>Welcome, {user.attributes.email}!</Text>
<Button title="Sign Out" onPress={signOut} />
</View>
) : (
<Button title="Sign In" onPress={() => Auth.federatedSignIn()} />
)}
</View>
);
};

export default App;

This example checks if a user is authenticated and displays a welcome message with a sign-out button or a sign-in button depending on the user’s authentication status.

Note: This is a basic example. Depending on your application’s requirements, you might want to implement more features, handle different AWS services, and integrate with other Amplify modules. Refer to the AWS Amplify documentation for more details and advanced usage.

Thank you for reading this article! Don’t forget to clap o

If you have any queries related to ReactNative, I’m always happy to help you. You can reach me on LinkedIn and Gmail.

--

--

Sanjana Human In Tech
Sanjana Human In Tech

Written by Sanjana Human In Tech

A React Native front-end enthusiast and dedicated development engineer, eager to expand knowledge on development techniques and collaborate with others.

No responses yet