Let's add some sweet functionality to our program.
So when we enter a password here, we want to read it.
So we need any hook for, well, a great moment, right?
So let's create a button event.
So when we click on the login button, we will call a method in our code behind file so that we then
can read the password from the password box, read the environment variable, and then compare both
of them to see if the user has entered the correct password.
So we got our button.
Let's add a click event handler click there we go.
Right now, let's not call it new event handler like this right here.
Let's just give it a great name.
Something like on login button clicked.
There we go.
Let me just copy that over and then we can go to our login view code behind file.
So make sure that you go to the login view HTML here and then pick the code behind file.
This is the one we haven't touched before.
Now inside here we want to create this public void on login button clicked and in addition we want to
create the object sender rooted event args.
There we go.
Then we can grab our password from the password box.
So let's switch back.
Go to the password box here.
Let's set an X name.
I just call it password box.
Save it.
Go back to the login button, click event.
Now let's create a variable.
Let's call it var password entered equals to and then pick the password box dot password.
So you can see this is a string all.
Sure.
If you like, you can also skip var and replace it with a string instead.
Really depends on however you like it.
Right?
So in that way we have it as a string password entered.
So this is what the user enters right here.
Let's see if this works.
Fine.
Especially we could take a look at what happens if I do not enter any kind of password and simply smash
the login button.
Right.
So I do not enter something.
I will click login and it's fine.
Nothing happens.
We do not get any null reference exception or something like that.
Right?
So because this is a string and the string is by default, well not null, but like empty, right?
In that way we will not get any exception.
Cool.
And we can just keep in mind that maybe it would be great to disable the login button while no password
is entered.
Let's just keep that in mind for now.
Great.
So password entered.
There we go.
Now we want to read the password from the environment variable that we just set.
So in that way we can use the system environment class or the system environment namespace, right?
So system dot environment by default, we should be able to see this system right here so we do not
have to enter system.
But I just wanted to show you the namespace so we can say environment dot get environment variable,
not variables, just variable.
And then we can write down the key that we have used to store the value.
It was invoice management.
There we go.
Now for sure we want to store this in a variable too.
Let's see, it is a string.
It's a nullable string indicated by the question mark here.
So this value can be null even though it's a string because it's a nullable string.
In that way we can say string question mark and then we can say environment env PV password, for example,
environment password.
Great.
Now there are two scenarios.
If we cannot get the environment variable, it will be null.
It will not be empty, it will not be an empty string, it will be null, right?
Because it's a nullable string and if we get the value, we got it in environment password.
So let's just say that if we have an environment password, right?
So if environment password a null, let's just write it like this.
You can for sure only say it like that too.
But we will keep the is not null.
Then we want to check if the password if password entered equals to the environment password that will
be a success.
Right?
So we can say messagebox just for now show entered correct password.
And in another way we could say if password enter is equals null else right.
In that way we could show entered wrong password.
So login failed.
Basically we could also say login failed.
Right.
Really depends on what you prefer and what you want to show to the user.
And if we have a null environment password right here, let's just add another else block in that way,
right?
We can simply show to the user or for you right now as a developer, for example, environment variable
not found, right?
So that you know what's wrong in your application or with your setup.
Great.
So let's just make that clear Again, that one is checking if we have an environment variable for the
password.
If it's not null, we know we have it.
We can check if the user entered the correct stuff.
Right here is the password entered from the user.
And if we do not have an environment variable, we will tell the user, Great, let's hit start, let's
test and let's test our application.
Right?
We got our entered password.
Let's just click log in.
That should show entered wrong password makes sense because it's empty.
Let's say 123 hit log in entered wrong password.
Now I will write down the correct password which is test 123 with a capital t, I will hit log in entered.
Correct password.
Right.
So that works fine.
If I now adjust that and write down anything.
Log in entered wrong password.
Great.
So now we know that we were able to read the environment variable.
We know that we are able to read the password.
We saw the entered correct password.
We see the entered wrong password.
And if we would remove our environment variable, we would see that we were not able to grab the variable
from within our application.
Very, very cool.
Now let's take it to the next level in the next lecture.