1
00:00:00,000 --> 00:00:02,000
Now with that, we're almost there

2
00:00:02,000 --> 00:00:05,000
and we almost got all the logic we need

3
00:00:05,000 --> 00:00:08,000
to create and store new users,

4
00:00:08,000 --> 00:00:10,000
but we're not entirely there yet.

5
00:00:11,000 --> 00:00:15,000
Whilst this will create a new user, it is now possible

6
00:00:15,000 --> 00:00:19,000
that we try to create a user for an email address

7
00:00:19,000 --> 00:00:23,000
that we did already store in the database.

8
00:00:23,000 --> 00:00:26,000
And that is not something we can do

9
00:00:26,000 --> 00:00:29,000
because indeed the user's table is configured

10
00:00:29,000 --> 00:00:31,000
such that the email must be unique.

11
00:00:31,000 --> 00:00:35,000
We can't have multiple users with the same email address.

12
00:00:37,000 --> 00:00:40,000
Hence in auth-actions.js,

13
00:00:40,000 --> 00:00:44,000
we should actually wrap createUser with try catch.

14
00:00:44,000 --> 00:00:47,000
Because this SQLite package,

15
00:00:47,000 --> 00:00:50,000
which we're using for interacting with the database,

16
00:00:50,000 --> 00:00:54,000
will actually throw an error if the database complains,

17
00:00:54,000 --> 00:00:59,000
for example, because we violate that unique constraint.

18
00:01:00,000 --> 00:01:03,000
So we will then get an error here.

19
00:01:04,000 --> 00:01:05,000
And therefore in this catch block,

20
00:01:05,000 --> 00:01:09,000
I wanna check if that error has a code,

21
00:01:09,000 --> 00:01:13,000
which it will have if it's coming from that SQLite package.

22
00:01:13,000 --> 00:01:15,000
That's equal to SQLITE_CONSTRAINT_UNIQUE.

23
00:01:19,000 --> 00:01:20,000
Written like this.

24
00:01:21,000 --> 00:01:25,000
So that's simply a code this package uses

25
00:01:25,000 --> 00:01:28,000
if the SQLite database generates an error

26
00:01:28,000 --> 00:01:31,000
because we violate such a unique constraint,

27
00:01:31,000 --> 00:01:34,000
like the one on the email address.

28
00:01:34,000 --> 00:01:39,000
And in that case, if we get such an error with this code,

29
00:01:39,000 --> 00:01:41,000
I actually don't wanna handle this error

30
00:01:41,000 --> 00:01:45,000
with an error JS file or in any other way.

31
00:01:45,000 --> 00:01:49,000
But instead then, I wanna return an object again.

32
00:01:49,000 --> 00:01:54,000
So return a response to the page that's submitted the form.

33
00:01:54,000 --> 00:01:57,000
And in there, I want to have an errors object,

34
00:01:57,000 --> 00:02:01,000
just as we did before with the email password validation.

35
00:02:01,000 --> 00:02:06,000
And that should be an object that has an email field

36
00:02:07,000 --> 00:02:10,000
where we say, "It seems like an account

37
00:02:10,000 --> 00:02:15,000
for the chosen email already exists."

38
00:02:17,000 --> 00:02:21,000
Now, you could also output a more cryptic error message

39
00:02:21,000 --> 00:02:23,000
so that the user doesn't know

40
00:02:23,000 --> 00:02:26,000
whether the creation now failed

41
00:02:26,000 --> 00:02:29,000
because they used an existing email address

42
00:02:29,000 --> 00:02:31,000
or for some other reason,

43
00:02:31,000 --> 00:02:35,000
so that users can't simply try out different email addresses

44
00:02:35,000 --> 00:02:38,000
to find out which users might have signed up

45
00:02:38,000 --> 00:02:40,000
with your service.

46
00:02:40,000 --> 00:02:42,000
So you could make this more cryptic.

47
00:02:42,000 --> 00:02:44,000
Here also for us during development

48
00:02:44,000 --> 00:02:46,000
so that we can see that this works,

49
00:02:46,000 --> 00:02:48,000
I'll use this pretty specific message.

50
00:02:50,000 --> 00:02:52,000
Now, if we have any other error,

51
00:02:52,000 --> 00:02:54,000
we'll not make it into this if check.

52
00:02:54,000 --> 00:02:57,000
And in that case, I just wanna throw that error

53
00:02:57,000 --> 00:03:01,000
so that the default next JS error handling mechanism

54
00:03:01,000 --> 00:03:03,000
can kick in,

55
00:03:03,000 --> 00:03:06,000
which means that the closest error JS file

56
00:03:06,000 --> 00:03:08,000
will take over if we have one.

57
00:03:09,000 --> 00:03:10,000
Well, and with that,

58
00:03:10,000 --> 00:03:12,000
we therefore now will create a user

59
00:03:12,000 --> 00:03:15,000
if that email address isn't taken already,

60
00:03:15,000 --> 00:03:17,000
otherwise will generate this error.

61
00:03:17,000 --> 00:03:20,000
And if we make it past this try catch block,

62
00:03:20,000 --> 00:03:24,000
we therefore will have successfully created a user.

63
00:03:24,000 --> 00:03:28,000
And in that case, I want to call redirect,

64
00:03:29,000 --> 00:03:33,000
a function that's imported from next navigation.

65
00:03:35,000 --> 00:03:38,000
And I wanna redirect the user to the training page

66
00:03:38,000 --> 00:03:42,000
because we don't need to stay on the authentication page

67
00:03:42,000 --> 00:03:44,000
if we just created a new user.

68
00:03:44,000 --> 00:03:48,000
Instead then, I wanna forward that user

69
00:03:48,000 --> 00:03:49,000
who's using the website

70
00:03:49,000 --> 00:03:52,000
to the main pages that make up my website.

71
00:03:52,000 --> 00:03:54,000
In this case, to the training page.

72
00:03:56,000 --> 00:03:59,000
And therefore, let's now give this a try.

73
00:03:59,000 --> 00:04:01,000
If we save everything and we go back here,

74
00:04:01,000 --> 00:04:04,000
if I enter an invalid password that's too short,

75
00:04:04,000 --> 00:04:05,000
I get that error.

76
00:04:06,000 --> 00:04:10,000
If I enter a valid password though, you see, I'm redirected.

77
00:04:10,000 --> 00:04:12,000
So that seems to work.

78
00:04:13,000 --> 00:04:15,000
However, if I now go back

79
00:04:15,000 --> 00:04:18,000
and I try to use that same email address again,

80
00:04:18,000 --> 00:04:22,000
even if I have a valid password, I get an error,

81
00:04:22,000 --> 00:04:26,000
because we have that unique constraint in the database.

82
00:04:27,000 --> 00:04:29,000
Of course, if I use a different email address,

83
00:04:29,000 --> 00:04:31,000
it works again.

84
00:04:31,000 --> 00:04:33,000
And that's therefore the user creation

85
00:04:33,000 --> 00:04:35,000
and the signup process,

86
00:04:35,000 --> 00:04:38,000
which is an important first step.

87
00:04:38,000 --> 00:04:39,000
But at this point,

88
00:04:39,000 --> 00:04:43,000
we actually haven't added any authentication yet,

89
00:04:43,000 --> 00:04:46,000
we just can create users.

90
00:04:46,000 --> 00:04:49,000
But we don't have any log in mechanism,

91
00:04:49,000 --> 00:04:51,000
we can't check whether a request

92
00:04:51,000 --> 00:04:54,000
is coming from a logged in user,

93
00:04:54,000 --> 00:04:56,000
we got nothing like that.

94
00:04:56,000 --> 00:04:59,000
And that's therefore what we'll work on next.

