How to x for place login information ?

place login question regarding the best place to store fairly complicated application logic. Say I want to allow a user to log into the site. The process of.

From the form field, hash the user’s email

Look up the user’s email hash in the auth table to make sure a user exists (auth table stores only encrypted email, email hash, user_id, and password hash)
user is foun, validate password
Regenerate the session id
Store the new session in the database

Using the data mapper pattern, I have the following three model which are involve in this process


Thus a function which logs the user in would look something like this:

There are a few concerns here. First is the lack of dependency injection. The second is that this a cumbersome chunk of code to use every place I might want to provide login functionality.

So where should this logic live? a controller? the User domain object? the Auth domain object? That seems kind of circular – the whole point of the data mapper is so that the domain object doesn’t deal with persistence of even itself, let alone OTHER objects…. Should it be place in a User or Auth service layer within either the /User/ or /Auth/ models?

I’m a bit lost as to the best practice for this sort of thing.

Also keep in mind I’m going this for learning purposes, so I don’t want to just use something like Symfony.

To answer my own question, I’ve decided that the best place for this is to create an Account Controller that accepts a Login Handler Interface interface as a constructor argument.


The Account Controller then looks like this:

Then whichever Login Handler I end up using, has everything it needs to do all of the logging in (looking up the user, validating the password, updating the session etc). This keeps my Account Controller clean, flexible, and testable.

I inject the desired LoginHandler (and RegistrationHandler, which I haven’t shown here) via a configuration in an IoC container that auto-resolves constructor dependencies.

Auth should handle the login if it fails returns false, if it’s true do the session’s logic and returns true.

So in your controller you will do something like if(Auth->login($email,$password))

ps: For this type of workflow, i prefer to use Singleton pattern ( tho it ruins Unit Testing ), but i do think it will suit you better.

Leave a Comment