I'd really recommend pulling this chunk of code out into a method that you can call in your helpers. Check out thunder's `login_as` function for details.