• PL
  • EN

Algorytm logowania.

Aby stworzyć namiastkę prawdziwej aplikacji, należy przygotować specjalne algorytmy, które pozwolą użytkownikowi stworzyć konto - zarejestrować się. Po tym procesie użytkownik będzie mógł się zalogować. Właśnie tym - logowaniem - zajmiemy się tym razem.
Podstawowym założezniem jest to, że użytkownik wcześniej założył sobie konto. Dlatego wcześniej musisz stworzyć algorytm rejestracji. Jeśli masz to już za sobą, możesz budować algorytm logowania. W przeciwnym razie wróć do poprzedniej lekcji. Jeśli posiadasz pełną gotowość, uruchom poprzedni projekt z logowaniem i rejestracją i zapoznaj się z poniższą instrukcją.




Porównajmy kod z projektem graficznym. Odczytajmy identyfikatory poszczególnych elementów wykorzystanych w powyższym algorytmie. Przeanalizujmy jak działa uproszczony algorytm logowania.

WARTO WIEDZIEĆ!

Nowoczesne algorytmy logowania działają nieco inaczej. W dzisiejszych czasach bardzo ważne jest bezpieczeństwo logowania. Dlatego musimy pamiętać o dodatkowym szyfrowaniu. Kolejnym aspektem jest używanie API. Rejestracja i logowanie w prawdziwych aplikacjach webowych jest znacznie bardziej złożone, ponieważ wymaga tego coraz szybciej zmieniający się świat cyfrowy. Omawiane przez nas przykłady mają za zadanie zainteresować Cię tą tematyką. Dzięki swojej prostocie sprawią, że zbudowanie algorytmu korzystającego z dodatkowych zabezpieczeń nie powinno być dla trudnością w przyszłości.

Jak to działa?

Algorytm logowania jest stosunkowo prosty. Jedyne co może rodzić pytania, to zawartość bloczka readRecord - ma on za zadanie przeszukać wszystkich użytkowników w bazie danych w poszukiwaniu zgodnego loginu oraz hasła wpisanego przez użytkownika.
To w dużym uproszczeniu, ale jak to się dzieje?

Kluczową rolę odgrywa w tym miejscu PĘTLA FOR. To za jej sprawą stworzona przez nas aplikacja będzie szukać zgodności podanego loginu i hasła z bazą tak długo, aż sprawdzi wszystkie wpisy.

1 - var i = 0; w pierwszym kroku tworzymy zmienną i, której przyporządkowujemy wartość zero. Wiesz już, że w informatyce liczymy od zera. Traktujemy tą zmienną jako licznik.

2 - kolejnym etapem jest wyrażenie i < records.lenght;, które jest formą warunku istnienia pętli. Oznacza to, że pętla będzie wykonywała się tak długo dopóki zmienna i < ilości użytkowników, czyli ilości rekordów.

3 - za każdym razem kiedy pętla wykona zaprogramowane przez Ciebie instrukcje, wartość licznika zostaje zwiększona o 1.




instrukcje colsole.log("ok"); oraz console.log("error"); mają na celu sprawdzić, czy instrukcja warunkowa została skonstruowana prawidłowo oraz czy cały algorytm wykonuje swoje zadanie prawidłowo. Jeśli po wpisaniu prawidłowego loginu (adresu emali) oraz hasła, w konsoli powinien pojawić się komunikat ok, w przeciwnym razie error.

WYZWANIE!

Podobnie jak w algorytmie rejestracji, postaraj się dodać do algorymu logowania coś więcej niż tylko komunikat o błędnym loginie lub haśle. W tym celu koniecznie użyj funkcji!

Login algorithm.

In order to create a substitute for a real application, special algorithms need to be prepared that allow the user to create an account - register. After this process, the user will be able to log in. That's what we're going to deal with this time - logging.
The basic assumption is that the user has previously has set up an account for himself. Therefore, you need to create a registration algorithm beforehand. If you've got it down, you can build a logging algorithm. Otherwise, go back to the previous lesson. If you have full readiness, run the previous project with login and registration and read the following instructions.




Let's compare the code with the graphic design. Let's read the identifiers of the various elements used in the above algorithm. Let's analyze how the simplified logging algorithm works.

GOOD TO KNOW!

Modern logging algorithms work a little differently. Nowadays, it is very important security of logging. Therefore, we need to keep in mind additional encryption. Another aspect is the use of API. Registration and login in real web applications is much more complex, as it is required by the ever faster changing digital world. The examples we discuss are designed to get you interested in the subject. Thanks to their simplicity they will make it easy for you to build an algorithm that uses additional security features, it should not be a difficulty in the future.

How it works?

Algorytm logowania jest stosunkowo prosty. The only thing that may raise questions is the contents of the block readRecord - It is designed to search all users in the database for a matching login and password entered by the user.
That's in a very simplified way, but how does it work?

The key role here is played by the FOR Hub. It is thanks to it that the application created by us will look for compatibility of the given login and password with the database, until it checks all entries.

1 - var i = 0; in the first step we create the variable i, to which we assign the value zero. You already know that in computer science we count from zero. We treat this variable as counter.

2 - The next step is the expression i < records.lenght;, which is a form of condition for the existence of the loop. This means that the loop will keep executing as long as the variable i < number of users, i.e. the number of records.

3 - each time the loop executes the instructions you programmed, the counter value is increased by 1.




instructions colsole.log("ok"); and console.log("error"); are designed to verify that the conditional instruction was constructed correctly and that the entire algorithm performs its task correctly. If after entering the correct login (emala address) and password, a message should appear in the console ok, Otherwise error.

CHALLENGE!

As in the registration algorithm, try to add something more to the login algorithm than just a message about the wrong login or password. To do this, be sure to use the function!