GithubHelp home page GithubHelp logo

allanroscoche / appsync-react-native-with-user-authorization Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dabit3/appsync-react-native-with-user-authorization

0.0 1.0 0.0 166 KB

End to end React Native + AWS AppSync GraphQL application with queries, mutations, subscriptions, & user authentication & authorization

JavaScript 66.09% Python 7.68% Java 6.61% Objective-C 19.61%

appsync-react-native-with-user-authorization's Introduction

AppSync with User Authorization

A small demo of how to get up and running with AWS AppSync and real world authorization

User Authentication Setup

This step will set up some basic 2 factor user authentication with the current project structure.

If you would like to set up your own user authentation mechanism this would also work, you would just need to update some logic in SignUp.js & SignIn.js.

  1. Install AWS Mobile CLI
npm i -g awsmobile-cli
  1. Configure AWS Mobile CLI
awsmobile configure
  1. Create new AWS Mobile Project
awsmobile init
  1. Add user signin to project
awsmobile user-signin enable
  1. Push updated configuration to the API
awsmobile push

AppSync Configuration

  1. Create new AppSync App

Visit the AppSync console, click "Create API"

  1. Change Authorization Type to "Amazon Cognito User Pool". Choose User Pool created in first series of steps. Set "Default action" as "Allow"

  1. Create the following Schema:
type City {
  id: ID
  name: String!
  country: String
}

type Query {
  fetchCity(id: ID): City
}
  1. Click "Create Resources"

  2. Click "Data Sources" in the left menu, click on the table name under "Resource"

  1. Create an index of "author"

  1. Update "CreateCity" request mapping template to the following:
#set($attribs = $util.dynamodb.toMapValues($ctx.args.input))
#set($attribs.author = $util.dynamodb.toDynamoDB($ctx.identity.username))
{
  "version": "2017-02-28",
  "operation": "PutItem",
  "key": {
    "id": $util.dynamodb.toDynamoDBJson($ctx.args.input.id),
  },
  "attributeValues": $util.toJson($attribs),
  "condition": {
    "expression": "attribute_not_exists(#id)",
    "expressionNames": {
      "#id": "id",
    },
  },
}
  1. Update the "ListCities" request mapping template to the following:
{
  "version": "2017-02-28",
  "operation": "Query",
  "query": {
  	"expression": "author = :author",
    "expressionValues": {
      ":author": { "S": "${ctx.identity.username}" }
    }
  },
  "index": "author-index",
  "limit": $util.defaultIfNull($ctx.args.first, 20),
  "nextToken": $util.toJson($util.defaultIfNullOrEmpty($ctx.args.after, null)),
}
  1. Run project

appsync-react-native-with-user-authorization's People

Contributors

allanroscoche avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.