[No Audio]
In the last video we started to think about how to
code up the controller, and that was responsible for
sending the user to a home page if the password was
correct, or to a login page if the password wasn't
correct. So that's what a controller did.
However, it doesn't know itself, has no knowledge about
anything to do with actually authenticating the user.
In other words, the business of checking the password,
checking if the user exists, looking the user up
in a database, that kind of thing is not
the responsibility of the LoginController.
So what's going to happen is, and what normally
happens is that a controller will delegate to
a service in a different layer.
So let's suppose we have,
[No Audio]
an AuthenticationService
[No Audio]
which LoginController is going to use.
We just create this quickly
[No Audio]
in the same package. That's fine.
Typically these have been different packages, but
we don't worry about that for now. That's all fine.
And then go back to our LoginController.
So now we have an AuthenticationService which
LoginController can use. So let's use it then.
So we said before the if
[No Audio]
say authentication service
[No Audio]
which is authenticate username password.
So supposing we have a method
on AuthenticationService called authenticate.
It takes username takes a password, and if the
password matches for that user, after it's looked it
up in some database, presumably then it returns true,
otherwise it returns false.
So return false in the case of the password is not
the correct one or the user doesn't exist, for example.
So we can do this, return the home page.
Otherwise, if we're in the else block,
then the call to authenticate returned false.
So, we kick the user out and send
the user back to the login page.
[No Audio]
Let's just quickly create this method
[No Audio]
for the sake of demonstration.
Let's just hard quote a password
[No Audio]
abracadabra
[No Audio]
for now, so we won't bother about the username
for now. We'll just basically check a password.
So if the password is equal to the hard
quoted password "abracadabra", then you can get him.
[No Audio]
Obviously this is not secure and you wouldn't
do this in an application, but it's just
for the purpose of a demonstration, of course.
So the authentication service now will authenticate any
user as long as they type in the password abracadabra.
Also we can compact this as well.
So if we just get rid of those
comments there, we can basically turn this into
a term reoperator, which would be nicer.
So we can just return if
[No Audio]
if that's true.
[No Audio]
return home otherwise
[No Audio]
return login,
[No Audio]
just get rid of those.
[No Audio]
That's a little bit neater. So the LoginController
has an AuthenticationService which is passed
into it, and it delegates to this AuthenticationService
to check the credentials passed in.
In this case it's just a password.
So now you should start to see, this
layering that we've got in place, and the fact
that different responsibilities belong to the different
classes in the different layers. So
we're going to explore this further in the next video.
When we look at curling up the data layer.
Let's see that now.