Skip to content

Building Cloud-enabled Mobile Applications with React Native & AWS Amplify

Notifications You must be signed in to change notification settings

tmhn/aws-amplify-workshop-react-native

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 

Repository files navigation

Building Mobile Applications with React Native & AWS Amplify

In this workshop we'll learn how to build cloud-enabled mobile applications with React Native & AWS Amplify.

Topics we'll be covering:

Redeeming our AWS Credit

  1. Visit the AWS Console.
  2. In the top right corner, click on My Account.
  3. In the left menu, click Credits.

Getting Started - Creating the React Native Application

To get started, we first need to create a new React Native project & change into the new directory using the React Native CLI. (See Building Projects With Native Code in the documentation)

If you already have the CLI installed, go ahead and create a new React Native app. If not, install the CLI & create a new app:

npm install -g react-native-cli

react-native init AmplifyApp

Now change into the new app directory & install the AWS Amplify, AWS Amplify React Native, & React Native Vector Icon libraries:

cd AmplifyApp
npm install --save aws-amplify aws-amplify-react-native react-native-vector-icons
# or
yarn add aws-amplify aws-amplify-react-native react-native-vector-icons

Finally, we need to link two native libraries:

react-native link react-native-vector-icons
react-native link amazon-cognito-identity-js

Now, because we've installed native dependencies, you need to compile the native project for these dependencies to be available.

Installing the CLI & Initializing a new AWS Amplify Project

Installing the CLI

Next, we'll install the AWS Amplify CLI:

npm install -g @aws-amplify/cli

Now we need to configure the CLI with our credentials:

amplify configure

If you'd like to see a video walkthrough of this configuration process, click here.

Here we'll walk through the amplify configure setup. Once you've signed in to the AWS console, continue:

  • Specify the AWS Region: eu-central-1, us-east-1 , or region closest to you
  • Specify the username of the new IAM user: amplify-workshop-user

In the AWS Console, click Next: Permissions, Next: Review, & Create User to create the new IAM user. Then, return to the command line & press Enter.

  • Enter the access key of the newly created user:
    accessKeyId: (<YOUR_ACCESS_KEY_ID>)
    secretAccessKey: (<YOUR_SECRET_ACCESS_KEY>)
  • Profile Name: (default)

Initializing A New AWS Amplify Project

Make sure to initialize this Amplify project in the root of your new React Native application

amplify init
  • Choose your default editor: Visual Studio Code (or your favorite editor)
  • Please choose the type of app that you're building javascript
  • What javascript framework are you using react-native
  • Source Directory Path: /
  • Distribution Directory Path: /
  • Build Command: npm run-script build
  • Start Command: npm run-script start
  • Do you want to use an AWS profile? Y
  • Please choose the profile you want to use: default

Now, the AWS Amplify CLI has iniatilized a new project & you will see a couple of new files & folders: amplify & .amplifyrc. These files hold your project configuration.

Adding Authentication

To add authentication, we can use the following command:

amplify add auth

When prompted for Do you want to use default authentication and security configuration?, choose Yes

Now, we'll run the push command and the cloud resources will be created in our AWS account.

amplify push

To view the new Cognito authentication service at any time after its creation, go to the dashboard at https://console.aws.amazon.com/cognito/. Also be sure that your region is set correctly.

Configuring the React Native applicaion

Now, our resources are created & we can start using them!

The first thing we need to do is to configure our React application to be aware of our new AWS Amplify project. We can do this by referencing the auto-generated aws-exports.js file that is now in our root folder.

To configure the app, open index.js and add the following code below the last import:

import Amplify from 'aws-amplify'
import config from './aws-exports'
Amplify.configure(config)

Now, our app is ready to start using our AWS services.

Using the withAuthenticator component

To add authentication, we'll go into App.js and first import the withAuthenticator HOC (Higher Order Component) from aws-amplify-react:

import { withAuthenticator } from 'aws-amplify-react-native'

Next, we'll wrap our default export (the App component) with the withAuthenticator HOC:

export default withAuthenticator(App)

Now, we can run the app and see that an Authentication flow has been added in front of our App component. This flow gives users the ability to sign up & sign in.

To view the new user that was created in Cognito, go back to the dashboard at https://console.aws.amazon.com/cognito/. Also be sure that your region is set correctly.

Accessing User Data

We can access the user's info now that they are signed in by calling Auth.currentAuthenticatedUser().

import { Auth } from 'aws-amplify'

async componentDidMount() {
  const user = await Auth.currentAuthenticatedUser()
  console.log('user:', user)
  console.log('username:', user.username)
}

Signing out the user using the withAuthenticator HOC

We can sign the user out using the Auth class & calling Auth.signOut(). This function returns a promise that is fulfilled after the user session has been ended & AsyncStorage is updated.

Because withAuthenticator holds all of the state within the actual component, we must have a way to rerender the actual withAuthenticator component by forcing React to rerender the parent component.

To do so, let's make a few updates:

// index.js
class AppWrapper extends React.Component {
  rerender = () => this.forceUpdate()
  render() {
    return <App rerender={this.rerender} />
  }
}

AppRegistry.registerComponent(appName, () => AppWrapper);

// App.js
class App extends Component {
  signOut = async () => {
    await Auth.signOut()
    this.props.rerender()
  }
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Welcome to React Native!</Text>
        <Text onPress={this.signOut} style={styles.instructions}>Sign Out</Text>
      </View>
    );
  }
}

export default props =>  {
  const AppComponent = withAuthenticator(App)
  return <AppComponent {...props} />
}

Custom authentication strategies

The withAuthenticator component is a really easy way to get up and running with authentication, but in a real-world application we probably want more control over how our form looks & functions.

Let's look at how we might create our own authentication flow.

To get started, we would probably want to create input fields that would hold user input data in the state. For instance when signing up a new user, we would probably need 4 user inputs to capture the user's username, email, password, & phone number.

To do this, we could create some initial state for these values & create an event handler that we could attach to the form inputs:

// initial state
state = {
  username: '', password: '', email: '', phone_number: ''
}

// event handler
onChangeText = (key, value) => {
  this.setState({ [key]: value })
}

// example of usage with TextInput
<TextInput
  placeholder='username'
  value={this.state.username}
  style={{ width: 300, height: 50, margin: 5, backgroundColor: "#ddd" }}
  onChangeText={v => this.onChange('username', v)}
/>

We'd also need to have a method that signed up & signed in users. We can us the Auth class to do thi. The Auth class has over 30 methods including things like signUp, signIn, confirmSignUp, confirmSignIn, & forgotPassword. Thes functions return a promise so they need to be handled asynchronously.

// import the Auth component
import { Auth } from 'aws-amplify'

// Class method to sign up a user
signUp = async() => {
  const { username, password, email, phone_number } = this.state
  try {
    await Auth.signUp({ username, password, attributes: { email, phone_number }})
  } catch (err) {
    console.log('error signing up user...', err)
  }
}

Adding a GraphQL API

To add a GraphQL API, we can use the following command:

amplify add api

Answer the following questions

  • Please select from one of the above mentioned services GraphQL
  • Provide API name: AmplifyWorkshopTest
  • Choose an authorization type for the API API key
  • Do you have an annotated GraphQL schema? N
  • Do you want a guided schema creation? Y
  • What best describes your project: Single object with fields (e.g. “Todo” with ID, name, description)
  • Do you want to edit the schema now? (Y/n) Y

When prompted, update the schema to the following:

type Pet @model {
  id: ID!
  name: String!
  description: String
}

Next, let's push the configuration to our account:

amplify push

To view the new AWS AppSync API at any time after its creation, go to the dashboard at https://console.aws.amazon.com/appsync. Also be sure that your region is set correctly.

Adding mutations from within the AWS AppSync Console

In the AWS AppSync console, open your API & then click on Queries.

Execute the following mutation to create a new pet in the API:

mutation createPet {
  createPet(input: {
    name: "Zeus"
    description: "Best dog in the western hemisphere"
  }) {
    id
  }
}

Now, let's query for the pet:

query listPets {
  listPets {
    items {
      id
      name
      description
    }
  }
}

We can even add search / filter capabilities when querying:

query searchPets {
  listPets(filter: {
    description: {
      contains: "dog"
    }
  }) {
    items {
      id
      name
      description
    }
  }
}

Interacting with the GraphQL API from our client application - Querying for data

Now that the GraphQL API is created we can begin interacting with it!

The first thing we'll do is perform a query to fetch data from our API.

To do so, we need to define the query, execute the query, store the data in our state, then list the items in our UI.

// imports from Amplify library
import { API, graphqlOperation } from 'aws-amplify'

// define query
const ListPets = `
  query {
    listPets {
      items {
        id
        name
        description
      }
    }
  }
`

// define some state to hold the data returned from the API
state = {
  pets: []
}

// execute the query in componentDidMount
async componentDidMount() {
  try {
    const pets = await API.graphql(graphqlOperation(ListPets))
    console.log('pets:', pets)
    this.setState({
      pets: pets.data.listPets.items
    })
  } catch (err) {
    console.log('error fetching pets...', err)
  }
}

// add UI in render method to show data
  {
    this.state.pets.map((pet, index) => (
      <View key={index}>
        <Text>{pet.name}</Text>
        <Text>{pet.description}</Text>
      </View>
    ))
  }

Performing mutations

Now, let's look at how we can create mutations.

// additional imports
import {
  // ...existing imports
  TextInput, Button
} from 'react-native'

import { graphqlOperation, API } from 'aws-amplify'

// define the new mutation
const CreatePet = `
  mutation($name: String!, $description: String) {
    createPet(input: {
      name: $name, description: $description
    }) {
      id
      name
      description
    }
  }
`

// add name & description fields to initial state
state = {
  name: '', description: '', pets: []
}

createPet = async() => {
  const { name, description } = this.state
  if (name === '') return
  let pet = { name }
  if (description !== '') {
    pet = { ...pet, description }
  }
  const updatedPetArray = [...this.state.pets, pet]
  this.setState({ pets: updatedPetArray })
  try {
    await API.graphql(graphqlOperation(CreatePet, pet))
    console.log('item created!')
  } catch (err) {
    console.log('error creating pet...', err)
  }
}

// change state then user types into input
onChange = (key, value) => {
  this.setState({ [key]: value })
}

// add UI with event handlers to manage user input
<TextInput
  onChangeText={v => this.onChange('name', v)}
  value={this.state.name}
  style={{ width: 300, height: 50, margin: 5, backgroundColor: "#ddd" }}
/>
<TextInput
  style={{ width: 300, height: 50, margin: 5, backgroundColor: "#ddd" }}
  onChangeText={v => this.onChange('description', v)}
  value={this.state.description}
/>
<Button onPress={this.createPet} title='Create Pet' />

GraphQL Subscriptions

Next, let's see how we can create a subscription to subscribe to changes of data in our API.

To do so, we need to define the subscription, listen for the subscription, & update the state whenever a new piece of data comes in through the subscription.

// define the subscription
const SubscribeToNewPets = `subscription {
  onCreatePet {
    id
    name
    description
  }
}`;

// subscribe in componentDidMount
API.graphql(
  graphqlOperation(SubscribeToNewPets)
).subscribe({
    next: (eventData) => {
      console.log('eventData', eventData)
      const pet = eventData.value.data.onCreatePet
      const pets = [
        ...this.state.pets.filter(p => {
          const val1 = p.name + p.description
          const val2 = pet.name + pet.description
          return val1 !== val2
        }),
        pet
      ]
      this.setState({ pets })
    }
});

Adding a REST API

To add a REST API, we can use the following command:

amplify add api

Answer the following questions

  • Please select from one of the above mentioned services REST
  • Provide a friendly name for your resource that will be used to label this category in the project: amplifyrestapi
  • Provide a path, e.g. /items /pets
  • Choose lambda source Create a new Lambda function
  • Provide a friendly name for your resource that will be used to label this category in the project: amplifyrestapilambda
  • Provide the Lambda function name: amplifyrestapilambda
  • Please select the function template you want to use: Serverless express function (Integration with Amazon API Gateway)
  • Do you want to edit the local lambda function now? Y

This will open amplify/backend/function/<FUNCTIONNAME>/src/app.js In this file, update the existing `app.get('/pets') route with the following:

app.get('/pets', function(req, res) {
  // Add your code here
  // Return the API Gateway event and query string parameters for example
  const pets = [
    { name: 'Spike', description: 'my favorite dog' },
    { name: 'Zeus', description: 'my second favorite dog' },
    { name: 'Butch', description: "mom's bff" }
  ]
  res.json({
    success: 'get call succeed!',
    url: req.url,
    pets
  });
});
  • Restrict API access Y
  • Who should have access? Authenticated users only
  • What kind of access do you want for Authenticated users read/write
  • Do you want to add another path? (y/N) N

Now the resources have been created & configured & we can push them to our account:

amplify push

Interacting with the new API

Now that the API is created we can start sending requests to it & interacting with it.

Let's request some data from the API:

import { API } from 'aws-amplify'

// create initial state
state = { pets: [] }

// fetch data at componentDidMount
componentDidMount() {
  this.getData()
}
getData = async() => {
  try {
    const data = await API.get('amplifyrestapilambda', '/pets')
    this.setState({ pets: data.pets })
  } catch (err) {
    console.log('error fetching data..', err)
  }
}

// implement into render method
{
  this.state.pets.map((pet, index) => (
    <View key={index}>
      <Text>{pet.name}</Text>
      <Text>{pet.description}</Text>
    </View>
  ))
}

Fetching data from another API in a Lambda function.

Next, let's configure the REST API to add another endpoint that will fetch data from an external resource.

First, we'll need to configure the API to know about the new path:

amplify configure api
  • Please select from one of the below mentioned services REST
  • Please select the REST API you would want to update amplifyrestapi
  • What would you like to do Add another path
  • Provide a path (e.g., /items) /people
  • Choose a Lambda source Use a Lambda function already added in the current Amplify project
  • Choose the Lambda function to invoke by this path amplifyrestapilambda
  • Restrict API access Yes
  • Who should have access? Authenticated users only
  • What kind of access do you want for Authenticated users read/write
  • Do you want to add another path? No

Now, we'll push the new configuration to our account:

amplify push

The next thing we need to do is install axios in our Lambda function folder.

Navigate to amplify/backend/function/<FUNCTION_NAME>/src and install axios:

yarn add axios

# or

npm install axios

Next, in amplify/backend/function/<FUNCTION_NAME>/src/app.js, let's add a new endpoint that will fetch a list of people from the Star Wars API.

// require axios
var axios = require('axios')

// add new /people endpoint
app.get('/people', function(req, res) {
  axios.get('https://swapi.co/api/people/')
    .then(response => {
      res.json({
        people: response.data.results,
        success: 'get call succeed!',
        url: req.url
      });
    })
    .catch(err => {
      res.json({
        error: 'error fetching data'
      });
    })
});

Now we can add a new function called getPeople that will call this API:

getPeople = async() => {
  try {
    const data = await API.get('amplifyrestapilambda', '/people')
    console.log('people data:', data)
    this.setState({ people: data.people })
  } catch (err) {
    console.log('error fetching data..', err)
  }
}

Adding Analytics

To add analytics, we can use the following command:

amplify add analytics

Next, we'll be prompted for the following:

  • Provide your pinpoint resource name: amplifyanalytics
  • Apps need authorization to send analytics events. Do you want to allow guest/unauthenticated users to send analytics events (recommended when getting started)? Y
  • overwrite YOURFILEPATH-cloudformation-template.yml Y

Recording events

Now that the service has been created we can now begin recording events.

To record analytics events, we need to import the Analytics class from Amplify & then call Analytics.record:

import { Analytics } from 'aws-amplify'

state = {username: ''}

async componentDidMount() {
  try {
    const user = await Auth.currentAuthenticatedUser()
    this.setState({ username: user.username })
  } catch (err) {
    console.log('error getting user: ', err)
  }
}

recordEvent = () => {
  Analytics.record({
    name: 'My test event',
    attributes: {
      username: this.state.username
    }
  })
}

<Button onPress={this.recordEvent} title='Record Event' />

Working with Storage

To add storage, we can use the following command:

amplify add storage

Answer the following questions

  • Please select from one of the below mentioned services Content (Images, audio, video, etc.)
  • Please provide a friendly name for your resource that will be used to label this category in the project: YOURAPINAME
  • Please provide bucket name: YOURBUCKETNAME
  • Who should have access: Auth users only
  • What kind of access do you want for Authenticated users read/write
amplify push

Now, storage is configured & ready to use.

What we've done above is created configured an Amazon S3 bucket that we can now start using for storing items.

For example, if we wanted to test it out we could store some text in a file like this:

import { Storage } from 'aws-amplify'

// create function to work with Storage
addToStorage = () => {
  Storage.put('javascript/MyReactComponent.js', `
    import React from 'react'
    const App = () => (
      <p>Hello World</p>
    )
    export default App
  `)
    .then (result => {
      console.log('result: ', result)
    })
    .catch(err => console.log('error: ', err));
}

// add click handler
<Button onPress={this.addToStorage} title='Add to Storage' />

This would create a folder called javascript in our S3 bucket & store a file called MyReactComponent.js there with the code we specified in the second argument of Storage.put.

If we want to read everything from this folder, we can use Storage.list:

readFromStorage = () => {
  Storage.list('javascript/')
    .then(data => console.log('data from S3: ', data))
    .catch(err => console.log('error fetching from S3', err))
}

If we only want to read the single file, we can use Storage.get:

readFromStorage = () => {
  Storage.get('javascript/MyReactComponent.js')
    .then(data => {
      console.log('data from S3: ', data)
      fetch(data)
        .then(r => r.text())
        .then(text => {
          console.log('text: ', text)
        })
        .catch(e => console.log('error fetching text: ', e))
    })
    .catch(err => console.log('error fetching from S3', err))
}

If we wanted to pull down everything, we can use Storage.list:

readFromStorage = () => {
  Storage.list('')
    .then(data => console.log('data from S3: ', data))
    .catch(err => console.log('error fetching from S3', err))
}

Removing Services

If at any time, or at the end of this workshop, you would like to delete a service from your project & your account, you can do this by running the amplify remove command:

amplify remove auth

amplify push

If you are unsure of what services you have enabled at any time, you can run the amplify status command:

amplify status

amplify status will give you the list of resources that are currently enabled in your app.

About

Building Cloud-enabled Mobile Applications with React Native & AWS Amplify

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published