Creating and applying authentication middleware