Cookie authentication.

Please review, I'm happy to change anything if needed. It seems to work though.