Now if you remember earlier in the lab when we created our 1
Cognito identity pool, we created a role for that to access AWS 2
s3 and also to access AWS the Sync store as well I'm just going to go back into 3
the ID so we have our user pool and we have our identity pool, so just going 4
into the identity pool, I'm just going to click on dashboard and I just going to 5
click on ID edit identity pool and I just want to have a look at that that 6
role, so you can see here they've got an authenticated role attached to it now 7
this role doesn't mean anything because we haven't allowed 8
unauthenticated access to our application anyway, so that doesn't matter at all 9
that role, the one that we are concerned with here is the authenticated 10
role, so I called that a big long name, now I'll just go back into the I am 11
console and I'll see if I can find that 12
so we're going to roles and see if I can find that one I'll just search for 13
Cognito 14
okay which one was it it wasn't that one that was it there okay so that's a role 15
that we created and I'm just going to expand and have a look at it so just 16
edit policy have a look at the JSON there, okay so by default this is 17
the only allow that is by default with the Cognito service so when we sign in 18
with Cognito and we have a Cognito identity this is a standard role that 19
will be set up that'll give you access to Cognito sync and of course the 20
Cognito identity pool but what I've done here is I've also added access to s3 but 21
that s3 access is a limited to listing a bucket to that directory or that folder 22
of that bucket so it'll be the bucket which mature but all my bucket is called 23
backspace - lab - pcoady yours will be different of course they are unique, and 24
then this Cognito use or any Cognito user will be able to list buckets or 25
will have permissions to list the buckets within a folder Cognito 26
backspace Academy and then we also have an allow for "get put and delete" objects 27
so readwrite access to objects and that will be again in that in that bucket and 28
then a Cognito folder and then a subfolder called Backspace Academy but 29
we've also got here the Cognito ID, okay so the the Cognito 30
ID number or user ID for that user there will be created as a subfolder 31
within this BackSpace Academy folder as well so they can only access a folder 32
that is in their identity they can't access any other folder within that 33
bucket and we've also got their permission to access subfolders of that 34
Cognito identity ID bucket that they've got there so that allows our users to 35
sign into into Cognito into a user pool then get their use those credentials or 36
those those those trust relationship to then get credentials out of Cognito 37
identity and then use those credentials to access a subfolder in their own 38
Cognito identity name and only that and that will allow them to read/write 39
access to that so let's have a look at how that works and back in the Adam IDE 40
so we need to create two functions here so the first one we need to create is to 41
get our Cognito sync token and then once we've got our Cognito sync token we can 42
pass that over to the OWS JavaScript SDK and use it to add an inner record to our 43
new key store database that we've created so again I'm just going to copy 44
that straight over both of those functions over from Final and put it 45
into our app jeaious so we can see here we've got a function for adding that 46
record and we've also got it a function there for getting that sync token the 47
first thing that we need to do is to make sure that we do actually have a 48
logged in user if not then we safely exit the application and what we need to 49
do is that we need to create a Cognito sync object using the JavaScript SDK now 50
even though that we've created that object we haven't done anything with it 51
okay we haven't passed anything information to it and we haven't 52
actually even created a Cognito sync database now this is something that you 53
will not find in the AWS JavaScript documentation pretty crazy I don't know 54
but it's not there it doesn't tell you how to 55
the create the Sync database I know from experience of having working with 56
Cognito back in the early days that you need to call list records to create that 57
Cognito Sync database so what we do is we call list records which is normally 58
used to obviously list the records that are in that in that database but if 59
there's no database then it will create a database for you so we need to give it 60
the data set name which will be we've already defined that earlier on in this 61
code I'll just find that here, we have already defined it as backspace -users 62
so our data set or database whatever you want to do it key store will be called 63
backspace - users and then we need to pass her identity ID for that user and 64
the identity pool ID so that we can cross-check that identity once that's 65
passed - Cognito sync list of Records then we'll either get an error or else 66
we're going to get a Cognito sync session token' sent to us 67
and we can see here so we're going to have our sync token and our sync count 68
and what we need is we need both of those to actually pass to the Cognito 69
sync service in order to to add a record so if that's successful we don't 70
actually have a database before we started but now it will automatically 71
create that for us and pass us a token to add records once we have done that we 72
can pass that token and that sync count to this add record function so the 73
parameters that we need to pass to update the records or to add a record we 74
need that data set name again we need the identity ID of the user the identity 75
pool ID and that sync session token that we just got previously and we also need 76
the sync count and the identity ID in here as well so 77
what I'm we don't actually have after actually have the identity identity as 78
such I've just put it in there as a value so what I'm doing is I'm putting a 79
record in here and the key for that record is going to be user ID and we're 80
telling it to replace if there is one already there if there isn't one there 81
then it's going to just create that record and we need to pass sync count on 82
that which we get from which we got with our Cognito token and then we can put a 83
value in there so I'm just putting in the value there of the users identity ID 84
so it's going to create a record that will have record user ID and the value 85
of that user ID will be the user's identity ID but you could create a key 86
that says you know their birthday and then the value of their birthday or 87
whatever it's up to you you use it you use this sync database however you want 88
it might be for top scorers of a game or something, okay so once we've done that 89
then we've got that information available ready to hand over to update 90
records so update record is how you can actually create a record from scratch or 91
you can update an existing record you that's okay then we won't get an error 92
this works fine so we'll just save that and upload that to our bucket and see 93
how we go and again we make sure that we've got public access to that and 94
again we make sure that we clear our browser cache and then we can refresh 95
Ok I will sign in 96
okay I remember my password this time that's 97
great, okay let's add a sync record to our sync database and see what happens 98
okay let's have a look and see what happened 99
so we're signed in successfully with we have called list records and it's given 100
our count of records which is zero and a Sync count is zero and it's also 101
passed us our sync session token' which is in here okay and here we go we've got 102
our sync session token' and a data set count that we've passing over to our 103
add record, okay and so what we get back here is the record so I'm just 104
going to copy that over and see what we get back instead of looking at JSON editor 105
and see what happens 106
okay so we've created so it sends back to us what the record that has been 107
created so we've created a a record with a key user ID and the value is that 108
users Cognito identity ID so it's done exactly what we 109
want it to do but again you could create records for whatever you know high score 110
and then the high score value for that user for a game for example so that 111
that's that's working quite well so what we'll do now is that we'll look at using 112
the Amazon s3 service this time with these federated credentials, let's have a 113
go at that