0
1
00:00:00,439 --> 00:00:05,310
Welcome back to BackSpace Academy. Here
we are at the stage now where we're
1

2
00:00:05,310 --> 00:00:09,929
going to be creating some code to
interact with Cognito, so what I've got
2

3
00:00:09,929 --> 00:00:17,130
here is I've got the the code for the
application on my desktop and I've just
3

4
00:00:17,130 --> 00:00:19,590
opened it up and we'll just have a quick
look at it and what we're going to be
4

5
00:00:19,590 --> 00:00:24,240
doing is modifying it and then uploading
it to that Amazon s3 bucket and then
5

6
00:00:24,240 --> 00:00:29,849
we're refreshing our browser cache and
6

7
00:00:29,849 --> 00:00:33,239
then we'll have a look and see what
happens, so first thing we want to have a
7

8
00:00:33,239 --> 00:00:37,380
look at is this index.html, so we're not
going to be touching that in any way
8

9
00:00:37,380 --> 00:00:41,239
shape or form it's really just a user
interface just some buttons and whatever
9

10
00:00:41,239 --> 00:00:47,390
but what we need to know here is at the
bottom of the page we've got our AWS
10

11
00:00:47,390 --> 00:00:53,879
software development kits so the first
one there is the Amazon Cognito identity
11

12
00:00:53,879 --> 00:01:00,300
JavaScript SDK and that will load first
and then after that we're going to load
12

13
00:01:00,300 --> 00:01:06,930
the AWS JavaScript SDK and then once
you've all loaded then we're going to
13

14
00:01:06,930 --> 00:01:13,080
start our application now when this
application first starts up the buttons
14

15
00:01:13,080 --> 00:01:17,640
won't be shown what will be shown will
be a spinner image and we need to have
15

16
00:01:17,640 --> 00:01:20,759
that spinner image there and not the
buttons because we need to make sure
16

17
00:01:20,759 --> 00:01:25,560
that we don't click on any buttons or
get any code running until our
17

18
00:01:25,560 --> 00:01:32,549
JavaScript SDKs have loaded in and then
once that happens they will be the
18

19
00:01:32,549 --> 00:01:37,770
spinner icon will be disabled and the
buttons will be enabled so let's have a
19

20
00:01:37,770 --> 00:01:44,250
look at how that works so we just jump
into app dot j s and so we've got a self
20

21
00:01:44,250 --> 00:01:48,240
invoking anonymous function so when this
invokes its going to first thing is
21

22
00:01:48,240 --> 00:01:53,729
going to hide that spinner because we've
just loaded up our JavaScript SDK and
22

23
00:01:53,729 --> 00:01:57,630
now we're at this stage and then it's
just going to show those buttons that
23

24
00:01:57,630 --> 00:02:01,740
need to be shown so that's what you need
to include in your or that feature
24

25
00:02:01,740 --> 00:02:05,369
within your application to make sure
that there is no code going to be
25

26
00:02:05,369 --> 00:02:10,590
running until that that JavaScript SDK
is loaded in then what we've got is
26

27
00:02:10,590 --> 00:02:13,800
we've got some click event listeners for
those buttons that are just go
27

28
00:02:13,800 --> 00:02:17,490
to refer a straight off to some
functions, so we click the sign up button
28

29
00:02:17,490 --> 00:02:21,090
it's gonna take us to the sign up
function there, so pretty straightforward
29

30
00:02:21,090 --> 00:02:28,350
stuff and here we have those functions,
so nothing too unusual there now as I
30

31
00:02:28,350 --> 00:02:34,230
said before we've got this app - final
which is a final code for the
31

32
00:02:34,230 --> 00:02:38,730
application so this will be running code
you can use this not a problem that this
32

33
00:02:38,730 --> 00:02:42,240
is what we're going to develop and but
if you're going to use it there's
33

34
00:02:42,240 --> 00:02:45,390
obviously a lot of things that need to
change any obviously user pool ID client
34

35
00:02:45,390 --> 00:02:51,810
ID all that sort of stuff so what I'm
going to do is just start off with in
35

36
00:02:51,810 --> 00:02:55,560
our main code there's a there are a
number of variables that need to be
36

37
00:02:55,560 --> 00:03:00,420
accessible within this self invoking
anonymous function first one being is a
37

38
00:03:00,420 --> 00:03:05,100
region we need to configure the region
we're using us east one for this lab
38

39
00:03:05,100 --> 00:03:09,870
make sure that you are as well we need
to have our user pool data so we need to
39

40
00:03:09,870 --> 00:03:17,400
have that user pool ID and the client ID
for our application we also need to have
40

41
00:03:17,400 --> 00:03:22,920
the identity pool ID from the identity
pool that we created we also need to
41

42
00:03:22,920 --> 00:03:28,680
define a database name for our key store
database that we're using with Cognito 
42

43
00:03:28,680 --> 00:03:33,480
sync and we're going to have some
variable set to hold information on our
43

44
00:03:33,480 --> 00:03:38,700
Cognito user our identity ID and the
Cognito sync database so I'm just going
44

45
00:03:38,700 --> 00:03:50,010
to copy those over now okay so let's
have a look at the signup function first
45

46
00:03:50,010 --> 00:03:53,780
so I'll just copy that over now
46

47
00:04:19,260 --> 00:04:24,520
okay so there are a number of things
that we need to collect off our user in
47

48
00:04:24,520 --> 00:04:27,700
that user form, so the first thing we
need to get off them is the username and
48

49
00:04:27,700 --> 00:04:31,960
password that they would like to use and
then we need to collect these attributes
49

50
00:04:31,960 --> 00:04:37,450
now if you remember from when we created
the user pool we selected some
50

51
00:04:37,450 --> 00:04:43,960
attributes that we wanted to collect
from our user upon registration and they
51

52
00:04:43,960 --> 00:04:48,600
were the given name, the family name,
email address, preferred username, website
52

53
00:04:48,600 --> 00:04:54,580
gender, birthdate, but we also added a
custom one there for LinkedIn, so the way
53

54
00:04:54,580 --> 00:05:00,430
we define those is custom: and then
the name of that custom
54

55
00:05:00,430 --> 00:05:05,440
attribute and then what we need to do is
we need to pass them over as parameters
55

56
00:05:05,440 --> 00:05:12,220
to the Cognito service with our client
ID for that application with that user
56

57
00:05:12,220 --> 00:05:18,460
pool and the password username and then
those user attributes so what we do is
57

58
00:05:18,460 --> 00:05:24,970
we create a Cognito identity service
provider object and we use the sign up
58

59
00:05:24,970 --> 00:05:30,820
of that and we pass the parameters to
that one of three things will occur
59

60
00:05:30,820 --> 00:05:35,410
we'll get an error
hopefully not and the kind of errors
60

61
00:05:35,410 --> 00:05:38,680
that could happen is that you've already
signed someone up and they go to sign up
61

62
00:05:38,680 --> 00:05:42,790
again then that will come up with an
error if we're trying to use a username
62

63
00:05:42,790 --> 00:05:47,710
or email that has already been used it
will come up with an error but if
63

64
00:05:47,710 --> 00:05:51,430
everything goes fine well we won't get
an error and then one of another two
64

65
00:05:51,430 --> 00:05:58,240
things will happen
if we've set up our user pool to to
65

66
00:05:58,240 --> 00:06:02,590
actually have a verification of our
email so what will happen is a person
66

67
00:06:02,590 --> 00:06:08,800
will sign up and then they will get sent
an email to verify their email address
67

68
00:06:08,800 --> 00:06:13,570
and we've actually done that with this
user pool when we when we created we did
68

69
00:06:13,570 --> 00:06:18,940
actually say that we wanted to have
verified emails so when we saw in when
69

70
00:06:18,940 --> 00:06:26,710
this person signs up the user confirmed
flag of the data will be false
70

71
00:06:26,710 --> 00:06:32,230
and so what we need to do is we need to
send a message to that user to tell them
71

72
00:06:32,230 --> 00:06:37,090
to go and check their email for that
verification link and if we look at the
72

73
00:06:37,090 --> 00:06:42,250
user pool of that person it will show it
as being unconfirmed and then when they
73

74
00:06:42,250 --> 00:06:46,150
click on that verification link and then
we go back into our user pool and have a
74

75
00:06:46,150 --> 00:06:53,320
look at it in the console will see that
that user will be confirmed if we didn't
75

76
00:06:53,320 --> 00:06:57,430
decide to have email verification and we
just said that you know just sign people
76

77
00:06:57,430 --> 00:07:02,620
up and don't bother about verifying
their email then that data dot user
77

78
00:07:02,620 --> 00:07:08,680
confirmed would be true not false so
what we'll do now is actually before we
78

79
00:07:08,680 --> 00:07:14,130
do anything I just want to point out
here that we're we're actually using AWS
79

80
00:07:14,130 --> 00:07:20,920
JavaScript SDK, now the
reason I'm using that rather than the
80

81
00:07:20,920 --> 00:07:27,880
Cognito JavaScript SDK is it has better
functionality or better reporting back
81

82
00:07:27,880 --> 00:07:34,600
of errors when something goes wrong so
in a situation like this where you don't
82

83
00:07:34,600 --> 00:07:40,110
have or where you have verifications of
email and that person will be
83

84
00:07:40,110 --> 00:07:48,310
unconfirmed what the Cognito javascript
it will send an error message back with
84

85
00:07:48,310 --> 00:07:52,570
nothing else just a 200 response with an
error message and you won't know what's
85

86
00:07:52,570 --> 00:07:59,410
going on and it's pretty poor but when
you use the AWS SDK you get more
86

87
00:07:59,410 --> 00:08:02,830
information back it will come back with
this data saying whether the user is
87

88
00:08:02,830 --> 00:08:07,510
confirmed or unconfirmed so if and it
won't come back as an error but it will
88

89
00:08:07,510 --> 00:08:11,950
come back as an error with the Cognito
JavaScript SDK so if you're wondering
89

90
00:08:11,950 --> 00:08:17,410
why I'm not using the Cognito JavaScript
SDK for this part of it that is why and
90

91
00:08:17,410 --> 00:08:24,250
it's going to create it's going to make
life a lot more easy for you now before
91

92
00:08:24,250 --> 00:08:28,930
we do anything we need to make sure that
we put the correct information in here
92

93
00:08:28,930 --> 00:08:35,920
for our user pool ID so I'm just going
to to go back into our AWS console and
93

94
00:08:35,920 --> 00:08:40,750
so we've got our general settings here
for our user pool so there is our user
94

95
00:08:40,750 --> 00:08:54,070
pool ID I'm just going to copy that and
bring that over and our client ID so we
95

96
00:08:54,070 --> 00:08:59,920
go back to our user pool we we select a
points and there's our app client ID so
96

97
00:08:59,920 --> 00:09:09,430
we'll copy that and bring that over as
well and we need our identity pool ID so
97

98
00:09:09,430 --> 00:09:13,120
we need to go into our federated
identities this is our federated
98

99
00:09:13,120 --> 00:09:20,110
identity pool here and we just need to
get the details of that so we can just
99

100
00:09:20,110 --> 00:09:27,220
go to the sample code here and select
JavaScript and that will give us our
100

101
00:09:27,220 --> 00:09:32,920
identity pool ID that we need to use so
we just copy that one from the sample
101

102
00:09:32,920 --> 00:09:38,290
code and we'll put that into our into
our application as well so there you
102

103
00:09:38,290 --> 00:09:45,340
have our identity pool ID I'll just put
that in here and I'll leave backspace -
103

104
00:09:45,340 --> 00:09:49,930
users I'll use that that's okay for a
name for a for a database not a problem
104

105
00:09:49,930 --> 00:09:56,500
so we'll save that now that that's all
done and ready to go we can upload it to
105

106
00:09:56,500 --> 00:10:01,920
our bucket and run our website and see
what happens
106

107
00:10:01,920 --> 00:10:09,640
so jumping into the s3 console we can
upload our our new app tour so we go to
107

108
00:10:09,640 --> 00:10:15,850
our bucket first and then we select our
and j/s folder and we'll upload our new
108

109
00:10:15,850 --> 00:10:23,590
app is to that making sure that we have
public permissions on that and upload
109

110
00:10:23,590 --> 00:10:28,210
that next thing we need to make sure is
that we clear our browser cache so I'm
110

111
00:10:28,210 --> 00:10:33,490
just clearing the data in my cache and
once we've done that we can go and
111

112
00:10:33,490 --> 00:10:41,250
refresh our website so you can see there
the spinner is showing while the
112

113
00:10:41,250 --> 00:10:48,250
JavaScript SDK is loading and then after
some time that will stop so obviously if
113

114
00:10:48,250 --> 00:10:51,370
you're going to have a home page you
wouldn't have this happening on your
114

115
00:10:51,370 --> 00:10:54,550
home page every time someone
to it so you'd have a landing page and
115

116
00:10:54,550 --> 00:10:58,360
then you'd have a sign-up page that
would do this because you're not going
116

117
00:10:58,360 --> 00:11:04,000
to need this you know unless someone
signed in or signing in okay so we've
117

118
00:11:04,000 --> 00:11:12,850
got our buttons they're available so
let's go through the signup process so
118

119
00:11:12,850 --> 00:11:19,030
we'll put in all your details okay so
thanks to the magic of pre-recorded
119

120
00:11:19,030 --> 00:11:22,000
video
I've very quickly put that in there so
120

121
00:11:22,000 --> 00:11:25,750
we've got our email address there we've
got our password all of those attributes
121

122
00:11:25,750 --> 00:11:30,670
we've also got our custom attribute in
there which is our LinkedIn profile page
122

123
00:11:30,670 --> 00:11:35,680
well click on actually what I'll do
first is I'll press f12 so we can see
123

124
00:11:35,680 --> 00:11:40,960
anything that's coming through on our on
our console and I'll just click on sign
124

125
00:11:40,960 --> 00:11:44,110
up and see what happens so we're
starting the signup process user
125

126
00:11:44,110 --> 00:11:50,980
confirmed and let's have a look at that
so we've got here user confirmed is
126

127
00:11:50,980 --> 00:11:55,810
false so remember when we're saying that
this will come back as false if you
127

128
00:11:55,810 --> 00:11:59,710
still need to click on a verification
link if we didn't have that happening
128

129
00:11:59,710 --> 00:12:05,710
then this user confirmed here would come
up as true and so because of that our
129

130
00:12:05,710 --> 00:12:10,960
code has picked up on that rather than
using the Cognito JavaScript SDK which
130

131
00:12:10,960 --> 00:12:14,970
would just give us an error which i
think is absolutely crazy
131

132
00:12:14,970 --> 00:12:21,730
using the AWS SDK we can quite safely
handle this and get our verification
132

133
00:12:21,730 --> 00:12:25,600
link so we just click on OK and that's
all we need to do so what we do now is
133

134
00:12:25,600 --> 00:12:31,900
we go into our into our email and click
on that verification link and then that
134

135
00:12:31,900 --> 00:12:35,740
will change so what we'll do is before
we do that I'm going to jump into the
135

136
00:12:35,740 --> 00:12:41,320
user pool and have a look at our users
and groups so it's saying there no users
136

137
00:12:41,320 --> 00:12:46,060
found but there are so what we need to
do is just click on the refresh button
137

138
00:12:46,060 --> 00:12:51,010
here on the right hand side click on
that and there we can see so there is
138

139
00:12:51,010 --> 00:12:57,580
that user it's enabled but the status is
unconfirmed because I haven't clicked on
139

140
00:12:57,580 --> 00:13:02,110
their email verification link which I'll
do now
140

141
00:13:02,110 --> 00:13:06,820
okay so I've just clicked on that
verification link and it says that my
141

142
00:13:06,820 --> 00:13:11,320
registration has been confirmed so I'm
just going to close that go back to my
142

143
00:13:11,320 --> 00:13:16,030
user pool and I'm going to click again
that refresh icon and see what happens
143

144
00:13:16,030 --> 00:13:21,850
and there we can see that the status is
now confirmed, okay so the next thing we
144

145
00:13:21,850 --> 00:13:29,760
need to do is to go through a sign-in
process, so let's have a look at that
