add cookie login function and refactor session and cookie code.