Now with that, we're almost there
and we almost got all the logic we need
to create and store new users,
but we're not entirely there yet.
Whilst this will create a new user, it is now possible
that we try to create a user for an email address
that we did already store in the database.
And that is not something we can do
because indeed the user's table is configured
such that the email must be unique.
We can't have multiple users with the same email address.
Hence in auth-actions.js,
we should actually wrap createUser with try catch.
Because this SQLite package,
which we're using for interacting with the database,
will actually throw an error if the database complains,
for example, because we violate that unique constraint.
So we will then get an error here.
And therefore in this catch block,
I wanna check if that error has a code,
which it will have if it's coming from that SQLite package.
That's equal to SQLITE_CONSTRAINT_UNIQUE.
Written like this.
So that's simply a code this package uses
if the SQLite database generates an error
because we violate such a unique constraint,
like the one on the email address.
And in that case, if we get such an error with this code,
I actually don't wanna handle this error
with an error JS file or in any other way.
But instead then, I wanna return an object again.
So return a response to the page that's submitted the form.
And in there, I want to have an errors object,
just as we did before with the email password validation.
And that should be an object that has an email field
where we say, "It seems like an account
for the chosen email already exists."
Now, you could also output a more cryptic error message
so that the user doesn't know
whether the creation now failed
because they used an existing email address
or for some other reason,
so that users can't simply try out different email addresses
to find out which users might have signed up
with your service.
So you could make this more cryptic.
Here also for us during development
so that we can see that this works,
I'll use this pretty specific message.
Now, if we have any other error,
we'll not make it into this if check.
And in that case, I just wanna throw that error
so that the default next JS error handling mechanism
can kick in,
which means that the closest error JS file
will take over if we have one.
Well, and with that,
we therefore now will create a user
if that email address isn't taken already,
otherwise will generate this error.
And if we make it past this try catch block,
we therefore will have successfully created a user.
And in that case, I want to call redirect,
a function that's imported from next navigation.
And I wanna redirect the user to the training page
because we don't need to stay on the authentication page
if we just created a new user.
Instead then, I wanna forward that user
who's using the website
to the main pages that make up my website.
In this case, to the training page.
And therefore, let's now give this a try.
If we save everything and we go back here,
if I enter an invalid password that's too short,
I get that error.
If I enter a valid password though, you see, I'm redirected.
So that seems to work.
However, if I now go back
and I try to use that same email address again,
even if I have a valid password, I get an error,
because we have that unique constraint in the database.
Of course, if I use a different email address,
it works again.
And that's therefore the user creation
and the signup process,
which is an important first step.
But at this point,
we actually haven't added any authentication yet,
we just can create users.
But we don't have any log in mechanism,
we can't check whether a request
is coming from a logged in user,
we got nothing like that.
And that's therefore what we'll work on next.