A small demo of how to get up and running with AWS AppSync and real world authorization
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.
- Install AWS Mobile CLI
npm i -g awsmobile-cli
- Configure AWS Mobile CLI
awsmobile configure
- Create new AWS Mobile Project
awsmobile init
- Add user signin to project
awsmobile user-signin enable
- Push updated configuration to the API
awsmobile push
- Create new AppSync App
Visit the AppSync console, click "Create API"
- Change Authorization Type to "Amazon Cognito User Pool". Choose User Pool created in first series of steps. Set "Default action" as "Allow"
- Create the following Schema:
type City {
id: ID
name: String!
country: String
}
type Query {
fetchCity(id: ID): City
}
-
Click "Create Resources"
-
Click "Data Sources" in the left menu, click on the table name under "Resource"
- Create an index of "author"
- 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",
},
},
}
- 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)),
}
- Run project