rewrite code, add the login/logout fuction, but now can't redirect to refer url.