Moved authentication code for redirects