refactor authentication and authsession code
refactor for rest server