How to implement a login system in a MVC standard?

Asked

Viewed 3,509 times

5

I’m studying about with , I managed to make a simple start system, only I was left with doubts in some points, how can I implement my login system? where I should "take" the values of POST and/or GET where I should treat values to avoid sql Injection and XSS, Model or Controller?

  • Are you using some framework or are doing it from scratch?

  • I’m doing it by hand,.

  • Then you are the one who should define the rules of your system, it can be in both controller and model, it depends only on which paradigm you intend to use

1 answer

4

The MVC standard has three layers

  • View: Your views are the forms of data presentation to the user. In short it would be an html, flash, air, or whatever type of interface you will adopt. In the view should have no business rule, except simple command blocks like a for or foreach to present a list of records.
  • Controller: The controller will handle communication between the view and the model. Here you will process data received from the request, send to model methods, and pass and present the proper view. Here you can also have watering like email sending, permission validation, etc.
  • Model: The model should get all its business rule related to the database. Whatever the query or manipulation of data should stay inside.

To be more specific with your question, the form would be in the view, the controller will receive the data, and record sessions and cookies, and to query the data in the database you will use your models.

Tips

There are some layers that can be added that increase the possibility of reusing code as a layer called service, which would be a layer where there would be rules unrelated to the data, but of great importance.

In the service layer could contain a class called Autenticação for example with methods that can verify if a user is logged in, because it uses the session or cookie and not the database, or even for the registration of users who in addition to registering in the database using the model, must send a confirmation email.

Using a service layer enables multiple rules to be reused across all controllers.

You can find more information about the MVC right here on Stack Overflow as well as, tips to create secure authentication systems.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.