Now, to finally conclude this course section,
I just wanna improve this page a little bit
and I wanna again use a session
for improving the user experience
in cases where we try to sign up with an existing user,
because there at the moment
we're not showing any error message,
or also on the login page.
And in addition, I wanna clean up the navigation bar.
It makes no sense to show the logout button
if the user isn't even logged in.
The same is true for the admin
and profile navigation bar items.
That was nice for testing
that we had those items at all times,
but now it makes more sense to only show them
if you will be able to visit them.
And for signup and login, the opposite is the case.
Here, I only wanna show the signup and login items
if the user is not authenticated yet.
So let's see how we can implement that.
At least for the validation,
we already know how we can do that
with the help of sessions.
For the navigation bar items,
I will show you a new little trick we can use with Express.
And I'll start with handling the validation errors
on signup and login.
Now, for signup, for post signup,
we already do have this error handling here
if the input is invalid,
and I wanna in the end do the same
if the user already exists.
So, of course, we can just copy that.
And in this case here where we already have a user,
I'll just also set my input data to this object
with hasError is true
and I'll say user exists already as an error message.
You can think about this
if you wanna give away the information
that a given email address is already taken,
but you see it on a lot of websites and therefore here,
I think we can also implement this.
Then I still set the entered email and so on.
And then, again just as before, I wanna redirect,
but only after saving the session
to ensure that the session was saved
before I present that same page again.
So I'll call request.session.save here
and pass this anonymous function to it.
And then here, I wanna redirect only after we saved
and we don't need to return here,
but instead, we should return thereafter to still make sure
that none of the code below that executes
if we failed to sign that user up.
So that's the first step.
If we save this and I go back here
and I try to create a user that already exists,
then I now get this error and I can fix my mistake.
Now, for the login page,
we in the end will do the same again.
Here in post login, if we could not log the user in
because we did not find a user,
then I wanna set my session input data,
so I'll use the same approach as for a signup,
and I'll set this to could not log you in,
please check your credentials.
I'll just not set confirm email
because that doesn't exist in this form, in the login form,
but we do set email and password.
And I'm not saying,
hey, we couldn't find a user for that email
because I don't necessarily wanna tell the user
why authentication failed
as I don't wanna give malicious users any extra hints
on whether it's the password or the email that was wrong.
I'll just say something was wrong here,
though, of course, you could simply try to create a user
with a given email.
And if you then learn that this email already exists,
you also know that it was probably the password
that was wrong, but still it's an extra step.
And then I also wanna set this here,
if we have a user but the passwords are equal,
then I'll actually copy
this request.session.inputData from before
and use that here as well
with the same error message and so on.
And now I also wanna ensure that here in the login route,
I save the session before I redirect.
So in the first if check, I'll access request.session.save
and then add my function here
and redirect inside of this function
and return thereafter just as before
and then I'll also copy this down to the second check
where I will also redirect like this
after saving the session.
And now we just have to go to the get login page
to ensure that we use that information there.
So on get login, just like in get signup,
I wanna, in the end, extract the data and reset the data
as I did it in the get signup route
here in get login now.
I just don't set the confirm email to a default value
because we don't have that in that form here,
but then I have my sessionInputData
and I'll pass that on to the login template,
I'll set input data equal to sessionInputData like that.
And now back into the login.ejs file here,
I wanna use the same approach as for signing up.
So I will, first of all, check this,
copy this check up here if you wanna show an error message
and bring that over to login.ejs.
Here, maybe right below the title.
And I also wanna initialize those fields
and set the email equal to inputData.email
and then copy that and do the same
for the password here like this.
And now with that, we can save that all.
If you now go to the login page
and I do enter a correct email but a wrong password,
I get my error message and I can still fix my input here.