Refactor authentication middleware into seperate file